Skip to content

Commit 3cf28f3

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 caad256 commit 3cf28f3

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

src/librustc/infer/error_reporting/different_lifetimes.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
245245
// region at the right depth with the same index
246246
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
247247
debug!("EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \
248-
def_id={:?}",
249-
self.infcx.tcx.hir.local_def_id(id),
250-
def_id);
251-
if self.infcx.tcx.hir.local_def_id(id) == def_id {
248+
def_id={:?}", id, def_id);
249+
if id == def_id {
252250
self.found_type = Some(arg);
253251
return; // we can stop visiting now
254252
}
@@ -260,11 +258,9 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
260258
(Some(rl::Region::LateBound(debruijn_index, id)), ty::BrNamed(def_id, _)) => {
261259
debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
262260
debruijn_index.depth);
263-
debug!("self.infcx.tcx.hir.local_def_id(id)={:?}",
264-
self.infcx.tcx.hir.local_def_id(id));
261+
debug!("self.infcx.tcx.hir.local_def_id(id)={:?}", id);
265262
debug!("def_id={:?}", def_id);
266-
if debruijn_index.depth == self.depth &&
267-
self.infcx.tcx.hir.local_def_id(id) == def_id {
263+
if debruijn_index.depth == self.depth && id == def_id {
268264
self.found_type = Some(arg);
269265
return; // we can stop visiting now
270266
}

src/librustc/middle/resolve_lifetime.rs

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

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

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

6062
fn late_anon(index: &Cell<u32>) -> Region {
@@ -64,7 +66,7 @@ impl Region {
6466
Region::LateBoundAnon(depth, i)
6567
}
6668

67-
fn id(&self) -> Option<ast::NodeId> {
69+
fn id(&self) -> Option<DefId> {
6870
match *self {
6971
Region::Static |
7072
Region::LateBoundAnon(..) => None,
@@ -337,7 +339,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
337339
0
338340
};
339341
let lifetimes = generics.lifetimes.iter().map(|def| {
340-
Region::early(&mut index, def)
342+
Region::early(self.hir_map, &mut index, def)
341343
}).collect();
342344
let scope = Scope::Binder {
343345
lifetimes,
@@ -368,7 +370,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
368370
match ty.node {
369371
hir::TyBareFn(ref c) => {
370372
let scope = Scope::Binder {
371-
lifetimes: c.lifetimes.iter().map(Region::late).collect(),
373+
lifetimes: c.lifetimes.iter().map(|def| {
374+
Region::late(self.hir_map, def)
375+
}).collect(),
372376
s: self.scope
373377
};
374378
self.with(scope, |old_scope, this| {
@@ -467,7 +471,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
467471
if !bound_lifetimes.is_empty() {
468472
self.trait_ref_hack = true;
469473
let scope = Scope::Binder {
470-
lifetimes: bound_lifetimes.iter().map(Region::late).collect(),
474+
lifetimes: bound_lifetimes.iter().map(|def| {
475+
Region::late(self.hir_map, def)
476+
}).collect(),
471477
s: self.scope
472478
};
473479
let result = self.with(scope, |old_scope, this| {
@@ -512,7 +518,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
512518
"nested quantification of lifetimes");
513519
}
514520
let scope = Scope::Binder {
515-
lifetimes: trait_ref.bound_lifetimes.iter().map(Region::late).collect(),
521+
lifetimes: trait_ref.bound_lifetimes.iter().map(|def| {
522+
Region::late(self.hir_map, def)
523+
}).collect(),
516524
s: self.scope
517525
};
518526
self.with(scope, |old_scope, this| {
@@ -647,10 +655,13 @@ fn extract_labels(ctxt: &mut LifetimeContext, body: &hir::Body) {
647655
Scope::Binder { ref lifetimes, s } => {
648656
// FIXME (#24278): non-hygienic comparison
649657
if let Some(def) = lifetimes.get(&label) {
658+
let node_id = hir_map.as_local_node_id(def.id().unwrap())
659+
.unwrap();
660+
650661
signal_shadowing_problem(
651662
sess,
652663
label,
653-
original_lifetime(hir_map.span(def.id().unwrap())),
664+
original_lifetime(hir_map.span(node_id)),
654665
shadower_label(label_span));
655666
return;
656667
}
@@ -749,7 +760,8 @@ fn object_lifetime_defaults_for_item(hir_map: &Map, generics: &hir::Generics)
749760
generics.lifetimes.iter().enumerate().find(|&(_, def)| {
750761
def.lifetime.name == name
751762
}).map_or(Set1::Many, |(i, def)| {
752-
Set1::One(Region::EarlyBound(i as u32, def.lifetime.id))
763+
let def_id = hir_map.local_def_id(def.lifetime.id);
764+
Set1::One(Region::EarlyBound(i as u32, def_id))
753765
})
754766
}
755767
}
@@ -835,9 +847,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
835847

836848
let lifetimes = generics.lifetimes.iter().map(|def| {
837849
if self.map.late_bound.contains(&def.lifetime.id) {
838-
Region::late(def)
850+
Region::late(self.hir_map, def)
839851
} else {
840-
Region::early(&mut index, def)
852+
Region::early(self.hir_map, &mut index, def)
841853
}
842854
}).collect();
843855

@@ -1483,10 +1495,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14831495

14841496
Scope::Binder { ref lifetimes, s } => {
14851497
if let Some(&def) = lifetimes.get(&lifetime.name) {
1498+
let node_id = self.hir_map
1499+
.as_local_node_id(def.id().unwrap())
1500+
.unwrap();
1501+
14861502
signal_shadowing_problem(
14871503
self.sess,
14881504
lifetime.name,
1489-
original_lifetime(self.hir_map.span(def.id().unwrap())),
1505+
original_lifetime(self.hir_map.span(node_id)),
14901506
shadower_lifetime(&lifetime));
14911507
return;
14921508
}

src/librustc_typeck/astconv.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -96,36 +96,40 @@ 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 hir_id = tcx.hir.node_to_hir_id(lifetime.id);
100104
let r = match tcx.named_region(hir_id) {
101105
Some(rl::Region::Static) => {
102106
tcx.types.re_static
103107
}
104108

105109
Some(rl::Region::LateBound(debruijn, id)) => {
106-
let name = tcx.hir.name(id);
110+
let name = lifetime_name(id);
107111
tcx.mk_region(ty::ReLateBound(debruijn,
108-
ty::BrNamed(tcx.hir.local_def_id(id), name)))
112+
ty::BrNamed(id, name)))
109113
}
110114

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

115119
Some(rl::Region::EarlyBound(index, id)) => {
116-
let name = tcx.hir.name(id);
120+
let name = lifetime_name(id);
117121
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
118-
def_id: tcx.hir.local_def_id(id),
122+
def_id: id,
119123
index,
120124
name,
121125
}))
122126
}
123127

124128
Some(rl::Region::Free(scope, id)) => {
125-
let name = tcx.hir.name(id);
129+
let name = lifetime_name(id);
126130
tcx.mk_region(ty::ReFree(ty::FreeRegion {
127131
scope,
128-
bound_region: ty::BrNamed(tcx.hir.local_def_id(id), name)
132+
bound_region: ty::BrNamed(id, name)
129133
}))
130134

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

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,8 @@ impl Clean<Type> for hir::Ty {
18351835
for (i, lt_param) in generics.lifetimes.iter().enumerate() {
18361836
if let Some(lt) = provided_params.lifetimes.get(i).cloned() {
18371837
if !lt.is_elided() {
1838-
lt_substs.insert(lt_param.lifetime.id, lt.clean(cx));
1838+
let lt_def_id = cx.tcx.hir.local_def_id(lt_param.lifetime.id);
1839+
lt_substs.insert(lt_def_id, lt.clean(cx));
18391840
}
18401841
}
18411842
}

src/librustdoc/core.rs

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

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

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

0 commit comments

Comments
 (0)