Skip to content

Commit a862955

Browse files
Use DefId instead of NodeId as identifier in resolve_lifetime::Region.
These Region values end up in crate metadata so they should not use NodeId.
1 parent f83d20e commit a862955

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

src/librustc/middle/resolve_lifetime.rs

+33-17
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,24 @@ use hir::intravisit::{self, Visitor, NestedVisitorMap};
3838
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
3939
pub enum Region {
4040
Static,
41-
EarlyBound(/* index */ u32, /* lifetime decl */ ast::NodeId),
42-
LateBound(ty::DebruijnIndex, /* lifetime decl */ ast::NodeId),
41+
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
42+
LateBound(ty::DebruijnIndex, /* lifetime decl */ DefId),
4343
LateBoundAnon(ty::DebruijnIndex, /* anon index */ u32),
44-
Free(DefId, /* lifetime decl */ ast::NodeId),
44+
Free(DefId, /* lifetime decl */ DefId),
4545
}
4646

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

54-
fn late(def: &hir::LifetimeDef) -> (ast::Name, Region) {
55+
fn late(hir_map: &Map, def: &hir::LifetimeDef) -> (ast::Name, Region) {
5556
let depth = ty::DebruijnIndex::new(1);
56-
(def.lifetime.name, Region::LateBound(depth, def.lifetime.id))
57+
let def_id = hir_map.local_def_id(def.lifetime.id);
58+
(def.lifetime.name, Region::LateBound(depth, def_id))
5759
}
5860

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

66-
fn id(&self) -> Option<ast::NodeId> {
68+
fn id(&self) -> Option<DefId> {
6769
match *self {
6870
Region::Static |
6971
Region::LateBoundAnon(..) => None,
@@ -333,7 +335,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
333335
0
334336
};
335337
let lifetimes = generics.lifetimes.iter().map(|def| {
336-
Region::early(&mut index, def)
338+
Region::early(self.hir_map, &mut index, def)
337339
}).collect();
338340
let scope = Scope::Binder {
339341
lifetimes,
@@ -364,7 +366,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
364366
match ty.node {
365367
hir::TyBareFn(ref c) => {
366368
let scope = Scope::Binder {
367-
lifetimes: c.lifetimes.iter().map(Region::late).collect(),
369+
lifetimes: c.lifetimes.iter().map(|def| {
370+
Region::late(self.hir_map, def)
371+
}).collect(),
368372
s: self.scope
369373
};
370374
self.with(scope, |old_scope, this| {
@@ -463,7 +467,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
463467
if !bound_lifetimes.is_empty() {
464468
self.trait_ref_hack = true;
465469
let scope = Scope::Binder {
466-
lifetimes: bound_lifetimes.iter().map(Region::late).collect(),
470+
lifetimes: bound_lifetimes.iter().map(|def| {
471+
Region::late(self.hir_map, def)
472+
}).collect(),
467473
s: self.scope
468474
};
469475
let result = self.with(scope, |old_scope, this| {
@@ -508,7 +514,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
508514
"nested quantification of lifetimes");
509515
}
510516
let scope = Scope::Binder {
511-
lifetimes: trait_ref.bound_lifetimes.iter().map(Region::late).collect(),
517+
lifetimes: trait_ref.bound_lifetimes.iter().map(|def| {
518+
Region::late(self.hir_map, def)
519+
}).collect(),
512520
s: self.scope
513521
};
514522
self.with(scope, |old_scope, this| {
@@ -643,10 +651,13 @@ fn extract_labels(ctxt: &mut LifetimeContext, body: &hir::Body) {
643651
Scope::Binder { ref lifetimes, s } => {
644652
// FIXME (#24278): non-hygienic comparison
645653
if let Some(def) = lifetimes.get(&label) {
654+
let node_id = hir_map.as_local_node_id(def.id().unwrap())
655+
.unwrap();
656+
646657
signal_shadowing_problem(
647658
sess,
648659
label,
649-
original_lifetime(hir_map.span(def.id().unwrap())),
660+
original_lifetime(hir_map.span(node_id)),
650661
shadower_label(label_span));
651662
return;
652663
}
@@ -745,7 +756,8 @@ fn object_lifetime_defaults_for_item(hir_map: &Map, generics: &hir::Generics)
745756
generics.lifetimes.iter().enumerate().find(|&(_, def)| {
746757
def.lifetime.name == name
747758
}).map_or(Set1::Many, |(i, def)| {
748-
Set1::One(Region::EarlyBound(i as u32, def.lifetime.id))
759+
let def_id = hir_map.local_def_id(def.lifetime.id);
760+
Set1::One(Region::EarlyBound(i as u32, def_id))
749761
})
750762
}
751763
}
@@ -830,9 +842,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
830842

831843
let lifetimes = generics.lifetimes.iter().map(|def| {
832844
if self.map.late_bound.contains(&def.lifetime.id) {
833-
Region::late(def)
845+
Region::late(self.hir_map, def)
834846
} else {
835-
Region::early(&mut index, def)
847+
Region::early(self.hir_map, &mut index, def)
836848
}
837849
}).collect();
838850

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

14791491
Scope::Binder { ref lifetimes, s } => {
14801492
if let Some(&def) = lifetimes.get(&lifetime.name) {
1493+
let node_id = self.hir_map
1494+
.as_local_node_id(def.id().unwrap())
1495+
.unwrap();
1496+
14811497
signal_shadowing_problem(
14821498
self.sess,
14831499
lifetime.name,
1484-
original_lifetime(self.hir_map.span(def.id().unwrap())),
1500+
original_lifetime(self.hir_map.span(node_id)),
14851501
shadower_lifetime(&lifetime));
14861502
return;
14871503
}

src/librustc_typeck/astconv.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -96,35 +96,39 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
9696
-> ty::Region<'tcx>
9797
{
9898
let tcx = self.tcx();
99+
let lifetime_name = |def_id| {
100+
tcx.hir.name(tcx.hir.as_local_node_id(def_id).unwrap())
101+
};
102+
99103
let r = match tcx.named_region_map.defs.get(&lifetime.id) {
100104
Some(&rl::Region::Static) => {
101105
tcx.types.re_static
102106
}
103107

104108
Some(&rl::Region::LateBound(debruijn, id)) => {
105-
let name = tcx.hir.name(id);
109+
let name = lifetime_name(id);
106110
tcx.mk_region(ty::ReLateBound(debruijn,
107-
ty::BrNamed(tcx.hir.local_def_id(id), name)))
111+
ty::BrNamed(id, name)))
108112
}
109113

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

114118
Some(&rl::Region::EarlyBound(index, id)) => {
115-
let name = tcx.hir.name(id);
119+
let name = lifetime_name(id);
116120
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
117-
def_id: tcx.hir.local_def_id(id),
121+
def_id: id,
118122
index,
119123
name,
120124
}))
121125
}
122126

123127
Some(&rl::Region::Free(scope, id)) => {
124-
let name = tcx.hir.name(id);
128+
let name = lifetime_name(id);
125129
tcx.mk_region(ty::ReFree(ty::FreeRegion {
126130
scope,
127-
bound_region: ty::BrNamed(tcx.hir.local_def_id(id), name)
131+
bound_region: ty::BrNamed(id, name)
128132
}))
129133

130134
// (*) -- not late-bound, won't change

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,8 @@ impl Clean<Type> for hir::Ty {
18071807
for (i, lt_param) in generics.lifetimes.iter().enumerate() {
18081808
if let Some(lt) = provided_params.lifetimes.get(i).cloned() {
18091809
if !lt.is_elided() {
1810-
lt_substs.insert(lt_param.lifetime.id, lt.clean(cx));
1810+
let lt_def_id = cx.tcx.hir.local_def_id(lt_param.lifetime.id);
1811+
lt_substs.insert(lt_def_id, lt.clean(cx));
18111812
}
18121813
}
18131814
}

src/librustdoc/core.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_trans::back::link;
2525
use rustc_resolve as resolve;
2626
use rustc_metadata::cstore::CStore;
2727

28-
use syntax::{ast, codemap};
28+
use syntax::codemap;
2929
use syntax::feature_gate::UnstableFeatures;
3030
use syntax::fold::Folder;
3131
use errors;
@@ -66,7 +66,7 @@ pub struct DocContext<'a, 'tcx: 'a> {
6666
/// Table type parameter definition -> substituted type
6767
pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
6868
/// Table node id of lifetime parameter definition -> substituted lifetime
69-
pub lt_substs: RefCell<FxHashMap<ast::NodeId, clean::Lifetime>>,
69+
pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
7070
}
7171

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

0 commit comments

Comments
 (0)