Skip to content

Commit 2b0120f

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 4fc3765 commit 2b0120f

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

@@ -1481,10 +1493,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14811493

14821494
Scope::Binder { ref lifetimes, s } => {
14831495
if let Some(&def) = lifetimes.get(&lifetime.name) {
1496+
let node_id = self.hir_map
1497+
.as_local_node_id(def.id().unwrap())
1498+
.unwrap();
1499+
14841500
signal_shadowing_problem(
14851501
self.sess,
14861502
lifetime.name,
1487-
original_lifetime(self.hir_map.span(def.id().unwrap())),
1503+
original_lifetime(self.hir_map.span(node_id)),
14881504
shadower_lifetime(&lifetime));
14891505
return;
14901506
}

src/librustc_typeck/astconv.rs

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

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

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

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

125129
Some(&rl::Region::Free(scope, id)) => {
126-
let name = tcx.hir.name(id);
130+
let name = lifetime_name(id);
127131
tcx.mk_region(ty::ReFree(ty::FreeRegion {
128132
scope,
129-
bound_region: ty::BrNamed(tcx.hir.local_def_id(id), name)
133+
bound_region: ty::BrNamed(id, name)
130134
}))
131135

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

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,8 @@ impl Clean<Type> for hir::Ty {
18051805
if let Some(lt) = provided_params.lifetimes().get(i).cloned()
18061806
.cloned() {
18071807
if !lt.is_elided() {
1808-
lt_substs.insert(lt_param.lifetime.id, lt.clean(cx));
1808+
let lt_def_id = cx.tcx.hir.local_def_id(lt_param.lifetime.id);
1809+
lt_substs.insert(lt_def_id, lt.clean(cx));
18091810
}
18101811
}
18111812
}

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)