Skip to content

Commit

Permalink
Use DefId instead of NodeId as identifier in resolve_lifetime::Region.
Browse files Browse the repository at this point in the history
These Region values end up in crate metadata so they should not use NodeId.
  • Loading branch information
michaelwoerister committed Sep 4, 2017
1 parent a36d93d commit 773736c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
50 changes: 33 additions & 17 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ use hir::intravisit::{self, Visitor, NestedVisitorMap};
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
pub enum Region {
Static,
EarlyBound(/* index */ u32, /* lifetime decl */ ast::NodeId),
LateBound(ty::DebruijnIndex, /* lifetime decl */ ast::NodeId),
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
LateBound(ty::DebruijnIndex, /* lifetime decl */ DefId),
LateBoundAnon(ty::DebruijnIndex, /* anon index */ u32),
Free(DefId, /* lifetime decl */ ast::NodeId),
Free(DefId, /* lifetime decl */ DefId),
}

impl Region {
fn early(index: &mut u32, def: &hir::LifetimeDef) -> (ast::Name, Region) {
fn early(hir_map: &Map, index: &mut u32, def: &hir::LifetimeDef) -> (ast::Name, Region) {
let i = *index;
*index += 1;
(def.lifetime.name, Region::EarlyBound(i, def.lifetime.id))
let def_id = hir_map.local_def_id(def.lifetime.id);
(def.lifetime.name, Region::EarlyBound(i, def_id))
}

fn late(def: &hir::LifetimeDef) -> (ast::Name, Region) {
fn late(hir_map: &Map, def: &hir::LifetimeDef) -> (ast::Name, Region) {
let depth = ty::DebruijnIndex::new(1);
(def.lifetime.name, Region::LateBound(depth, def.lifetime.id))
let def_id = hir_map.local_def_id(def.lifetime.id);
(def.lifetime.name, Region::LateBound(depth, def_id))
}

fn late_anon(index: &Cell<u32>) -> Region {
Expand All @@ -63,7 +65,7 @@ impl Region {
Region::LateBoundAnon(depth, i)
}

fn id(&self) -> Option<ast::NodeId> {
fn id(&self) -> Option<DefId> {
match *self {
Region::Static |
Region::LateBoundAnon(..) => None,
Expand Down Expand Up @@ -333,7 +335,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
0
};
let lifetimes = generics.lifetimes.iter().map(|def| {
Region::early(&mut index, def)
Region::early(self.hir_map, &mut index, def)
}).collect();
let scope = Scope::Binder {
lifetimes,
Expand Down Expand Up @@ -364,7 +366,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
match ty.node {
hir::TyBareFn(ref c) => {
let scope = Scope::Binder {
lifetimes: c.lifetimes.iter().map(Region::late).collect(),
lifetimes: c.lifetimes.iter().map(|def| {
Region::late(self.hir_map, def)
}).collect(),
s: self.scope
};
self.with(scope, |old_scope, this| {
Expand Down Expand Up @@ -463,7 +467,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
if !bound_lifetimes.is_empty() {
self.trait_ref_hack = true;
let scope = Scope::Binder {
lifetimes: bound_lifetimes.iter().map(Region::late).collect(),
lifetimes: bound_lifetimes.iter().map(|def| {
Region::late(self.hir_map, def)
}).collect(),
s: self.scope
};
let result = self.with(scope, |old_scope, this| {
Expand Down Expand Up @@ -508,7 +514,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
"nested quantification of lifetimes");
}
let scope = Scope::Binder {
lifetimes: trait_ref.bound_lifetimes.iter().map(Region::late).collect(),
lifetimes: trait_ref.bound_lifetimes.iter().map(|def| {
Region::late(self.hir_map, def)
}).collect(),
s: self.scope
};
self.with(scope, |old_scope, this| {
Expand Down Expand Up @@ -643,10 +651,13 @@ fn extract_labels(ctxt: &mut LifetimeContext, body: &hir::Body) {
Scope::Binder { ref lifetimes, s } => {
// FIXME (#24278): non-hygienic comparison
if let Some(def) = lifetimes.get(&label) {
let node_id = hir_map.as_local_node_id(def.id().unwrap())
.unwrap();

signal_shadowing_problem(
sess,
label,
original_lifetime(hir_map.span(def.id().unwrap())),
original_lifetime(hir_map.span(node_id)),
shadower_label(label_span));
return;
}
Expand Down Expand Up @@ -745,7 +756,8 @@ fn object_lifetime_defaults_for_item(hir_map: &Map, generics: &hir::Generics)
generics.lifetimes.iter().enumerate().find(|&(_, def)| {
def.lifetime.name == name
}).map_or(Set1::Many, |(i, def)| {
Set1::One(Region::EarlyBound(i as u32, def.lifetime.id))
let def_id = hir_map.local_def_id(def.lifetime.id);
Set1::One(Region::EarlyBound(i as u32, def_id))
})
}
}
Expand Down Expand Up @@ -830,9 +842,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {

let lifetimes = generics.lifetimes.iter().map(|def| {
if self.map.late_bound.contains(&def.lifetime.id) {
Region::late(def)
Region::late(self.hir_map, def)
} else {
Region::early(&mut index, def)
Region::early(self.hir_map, &mut index, def)
}
}).collect();

Expand Down Expand Up @@ -1478,10 +1490,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {

Scope::Binder { ref lifetimes, s } => {
if let Some(&def) = lifetimes.get(&lifetime.name) {
let node_id = self.hir_map
.as_local_node_id(def.id().unwrap())
.unwrap();

signal_shadowing_problem(
self.sess,
lifetime.name,
original_lifetime(self.hir_map.span(def.id().unwrap())),
original_lifetime(self.hir_map.span(node_id)),
shadower_lifetime(&lifetime));
return;
}
Expand Down
16 changes: 10 additions & 6 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,35 +96,39 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
-> ty::Region<'tcx>
{
let tcx = self.tcx();
let lifetime_name = |def_id| {
tcx.hir.name(tcx.hir.as_local_node_id(def_id).unwrap())
};

let r = match tcx.named_region_map.defs.get(&lifetime.id) {
Some(&rl::Region::Static) => {
tcx.types.re_static
}

Some(&rl::Region::LateBound(debruijn, id)) => {
let name = tcx.hir.name(id);
let name = lifetime_name(id);
tcx.mk_region(ty::ReLateBound(debruijn,
ty::BrNamed(tcx.hir.local_def_id(id), name)))
ty::BrNamed(id, name)))
}

Some(&rl::Region::LateBoundAnon(debruijn, index)) => {
tcx.mk_region(ty::ReLateBound(debruijn, ty::BrAnon(index)))
}

Some(&rl::Region::EarlyBound(index, id)) => {
let name = tcx.hir.name(id);
let name = lifetime_name(id);
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
def_id: tcx.hir.local_def_id(id),
def_id: id,
index,
name,
}))
}

Some(&rl::Region::Free(scope, id)) => {
let name = tcx.hir.name(id);
let name = lifetime_name(id);
tcx.mk_region(ty::ReFree(ty::FreeRegion {
scope,
bound_region: ty::BrNamed(tcx.hir.local_def_id(id), name)
bound_region: ty::BrNamed(id, name)
}))

// (*) -- not late-bound, won't change
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,8 @@ impl Clean<Type> for hir::Ty {
for (i, lt_param) in generics.lifetimes.iter().enumerate() {
if let Some(lt) = provided_params.lifetimes.get(i).cloned() {
if !lt.is_elided() {
lt_substs.insert(lt_param.lifetime.id, lt.clean(cx));
let lt_def_id = cx.tcx.hir.local_def_id(lt_param.lifetime.id);
lt_substs.insert(lt_def_id, lt.clean(cx));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_trans::back::link;
use rustc_resolve as resolve;
use rustc_metadata::cstore::CStore;

use syntax::{ast, codemap};
use syntax::codemap;
use syntax::feature_gate::UnstableFeatures;
use syntax::fold::Folder;
use errors;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub struct DocContext<'a, 'tcx: 'a> {
/// Table type parameter definition -> substituted type
pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
/// Table node id of lifetime parameter definition -> substituted lifetime
pub lt_substs: RefCell<FxHashMap<ast::NodeId, clean::Lifetime>>,
pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
}

impl<'a, 'tcx> DocContext<'a, 'tcx> {
Expand All @@ -78,7 +78,7 @@ impl<'a, 'tcx> DocContext<'a, 'tcx> {
/// the substitutions for a type alias' RHS.
pub fn enter_alias<F, R>(&self,
ty_substs: FxHashMap<Def, clean::Type>,
lt_substs: FxHashMap<ast::NodeId, clean::Lifetime>,
lt_substs: FxHashMap<DefId, clean::Lifetime>,
f: F) -> R
where F: FnOnce() -> R {
let (old_tys, old_lts) =
Expand Down

0 comments on commit 773736c

Please sign in to comment.