Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0682a75

Browse files
committedJan 28, 2017
rustc: clean up the style of middle::resolve_lifetime.
1 parent 7a2a669 commit 0682a75

File tree

4 files changed

+109
-79
lines changed

4 files changed

+109
-79
lines changed
 

‎src/librustc/middle/resolve_lifetime.rs

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
//! used between functions, and they operate in a purely top-down
1616
//! way. Therefore we break lifetime name resolution into a separate pass.
1717
18-
pub use self::DefRegion::*;
19-
use self::ScopeChain::*;
20-
2118
use dep_graph::DepNode;
2219
use hir::map::Map;
2320
use session::Session;
@@ -36,22 +33,19 @@ use hir;
3633
use hir::intravisit::{self, Visitor, FnKind, NestedVisitorMap};
3734

3835
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
39-
pub enum DefRegion {
40-
DefStaticRegion,
41-
DefEarlyBoundRegion(/* index */ u32,
42-
/* lifetime decl */ ast::NodeId),
43-
DefLateBoundRegion(ty::DebruijnIndex,
44-
/* lifetime decl */ ast::NodeId),
45-
DefFreeRegion(region::CallSiteScopeData,
46-
/* lifetime decl */ ast::NodeId),
36+
pub enum Region {
37+
Static,
38+
EarlyBound(/* index */ u32, /* lifetime decl */ ast::NodeId),
39+
LateBound(ty::DebruijnIndex, /* lifetime decl */ ast::NodeId),
40+
Free(region::CallSiteScopeData, /* lifetime decl */ ast::NodeId),
4741
}
4842

4943
// Maps the id of each lifetime reference to the lifetime decl
5044
// that it corresponds to.
5145
pub struct NamedRegionMap {
5246
// maps from every use of a named (not anonymous) lifetime to a
53-
// `DefRegion` describing how that region is bound
54-
pub defs: NodeMap<DefRegion>,
47+
// `Region` describing how that region is bound
48+
pub defs: NodeMap<Region>,
5549

5650
// the set of lifetime def ids that are late-bound; late-bound ids
5751
// are named regions appearing in fn arguments that do not appear
@@ -63,7 +57,7 @@ struct LifetimeContext<'a, 'tcx: 'a> {
6357
sess: &'a Session,
6458
hir_map: &'a Map<'tcx>,
6559
map: &'a mut NamedRegionMap,
66-
scope: Scope<'a>,
60+
scope: ScopeRef<'a>,
6761
// Deep breath. Our representation for poly trait refs contains a single
6862
// binder and thus we only allow a single level of quantification. However,
6963
// the syntax of Rust permits quantification in two places, e.g., `T: for <'a> Foo<'a>`
@@ -86,25 +80,36 @@ struct LifetimeContext<'a, 'tcx: 'a> {
8680
}
8781

8882
#[derive(PartialEq, Debug)]
89-
enum ScopeChain<'a> {
90-
/// EarlyScope(['a, 'b, ...], start, s) extends s with early-bound
91-
/// lifetimes, with consecutive parameter indices from `start`.
92-
/// That is, 'a has index `start`, 'b has index `start + 1`, etc.
83+
enum Scope<'a> {
84+
/// Extends `s` with early-bound `lifetimes`, having consecutive parameter
85+
/// indices from `start`, i.e. `lifetimes[i]` has index `start + i`.
9386
/// Indices before `start` correspond to other generic parameters
9487
/// of a parent item (trait/impl of a method), or `Self` in traits.
95-
EarlyScope(&'a [hir::LifetimeDef], u32, Scope<'a>),
96-
/// LateScope(['a, 'b, ...], s) extends s with late-bound
97-
/// lifetimes introduced by the declaration binder_id.
98-
LateScope(&'a [hir::LifetimeDef], Scope<'a>),
99-
100-
/// lifetimes introduced by a fn are scoped to the call-site for that fn.
101-
FnScope { fn_id: ast::NodeId, body_id: ast::NodeId, s: Scope<'a> },
102-
RootScope
88+
Early {
89+
lifetimes: &'a [hir::LifetimeDef],
90+
start: u32,
91+
s: ScopeRef<'a>
92+
},
93+
94+
/// Extends `s` with late-bound `lifetimes`.
95+
Late {
96+
lifetimes: &'a [hir::LifetimeDef],
97+
s: ScopeRef<'a>
98+
},
99+
100+
/// Lifetimes introduced by a fn are scoped to the call-site for that fn.
101+
Fn {
102+
fn_id: ast::NodeId,
103+
body_id: ast::NodeId,
104+
s: ScopeRef<'a>
105+
},
106+
107+
Root
103108
}
104109

105-
type Scope<'a> = &'a ScopeChain<'a>;
110+
type ScopeRef<'a> = &'a Scope<'a>;
106111

107-
static ROOT_SCOPE: ScopeChain<'static> = RootScope;
112+
const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;
108113

109114
pub fn krate(sess: &Session,
110115
hir_map: &Map)
@@ -120,7 +125,7 @@ pub fn krate(sess: &Session,
120125
sess: sess,
121126
hir_map: hir_map,
122127
map: &mut map,
123-
scope: &ROOT_SCOPE,
128+
scope: ROOT_SCOPE,
124129
trait_ref_hack: false,
125130
labels_in_fn: vec![],
126131
}, krate);
@@ -140,7 +145,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
140145
let saved_labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
141146

142147
// Items always introduce a new root scope
143-
self.with(RootScope, |_, this| {
148+
self.with(Scope::Root, |_, this| {
144149
match item.node {
145150
hir::ItemFn(..) => {
146151
// Fn lifetimes get added in visit_fn below:
@@ -169,7 +174,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
169174
} else {
170175
0
171176
};
172-
this.with(EarlyScope(lifetimes, start, &ROOT_SCOPE), |old_scope, this| {
177+
let early = Scope::Early {
178+
lifetimes: lifetimes,
179+
start: start,
180+
s: ROOT_SCOPE
181+
};
182+
this.with(early, |old_scope, this| {
173183
this.check_lifetime_defs(old_scope, lifetimes);
174184
intravisit::walk_item(this, item);
175185
});
@@ -187,7 +197,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
187197
let saved = replace(&mut self.labels_in_fn, vec![]);
188198

189199
// Items always introduce a new root scope
190-
self.with(RootScope, |_, this| {
200+
self.with(Scope::Root, |_, this| {
191201
match item.node {
192202
hir::ForeignItemFn(ref decl, _, ref generics) => {
193203
this.visit_early_late(item.id, decl, generics, |this| {
@@ -233,7 +243,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
233243
fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
234244
match ty.node {
235245
hir::TyBareFn(ref c) => {
236-
self.with(LateScope(&c.lifetimes, self.scope), |old_scope, this| {
246+
let late = Scope::Late {
247+
lifetimes: &c.lifetimes,
248+
s: self.scope
249+
};
250+
self.with(late, |old_scope, this| {
237251
// a bare fn has no bounds, so everything
238252
// contained within is scoped within its binder.
239253
this.check_lifetime_defs(old_scope, &c.lifetimes);
@@ -245,7 +259,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
245259
// a trait ref, which introduces a binding scope.
246260
match path.def {
247261
Def::Trait(..) => {
248-
self.with(LateScope(&[], self.scope), |_, this| {
262+
self.with(Scope::Late { lifetimes: &[], s: self.scope }, |_, this| {
249263
this.visit_path(path, ty.id);
250264
});
251265
}
@@ -283,7 +297,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
283297
return;
284298
}
285299
if lifetime_ref.name == keywords::StaticLifetime.name() {
286-
self.insert_lifetime(lifetime_ref, DefStaticRegion);
300+
self.insert_lifetime(lifetime_ref, Region::Static);
287301
return;
288302
}
289303
self.resolve_lifetime_ref(lifetime_ref);
@@ -304,8 +318,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
304318
.. }) => {
305319
if !bound_lifetimes.is_empty() {
306320
self.trait_ref_hack = true;
307-
let result = self.with(LateScope(bound_lifetimes, self.scope),
308-
|old_scope, this| {
321+
let late = Scope::Late {
322+
lifetimes: bound_lifetimes,
323+
s: self.scope
324+
};
325+
let result = self.with(late, |old_scope, this| {
309326
this.check_lifetime_defs(old_scope, bound_lifetimes);
310327
this.visit_ty(&bounded_ty);
311328
walk_list!(this, visit_ty_param_bound, bounds);
@@ -346,7 +363,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
346363
span_err!(self.sess, trait_ref.span, E0316,
347364
"nested quantification of lifetimes");
348365
}
349-
self.with(LateScope(&trait_ref.bound_lifetimes, self.scope), |old_scope, this| {
366+
let late = Scope::Late {
367+
lifetimes: &trait_ref.bound_lifetimes,
368+
s: self.scope
369+
};
370+
self.with(late, |old_scope, this| {
350371
this.check_lifetime_defs(old_scope, &trait_ref.bound_lifetimes);
351372
for lifetime in &trait_ref.bound_lifetimes {
352373
this.visit_lifetime_def(lifetime);
@@ -412,7 +433,7 @@ fn signal_shadowing_problem(sess: &Session, name: ast::Name, orig: Original, sha
412433
fn extract_labels(ctxt: &mut LifetimeContext, b: hir::BodyId) {
413434
struct GatherLabels<'a> {
414435
sess: &'a Session,
415-
scope: Scope<'a>,
436+
scope: ScopeRef<'a>,
416437
labels_in_fn: &'a mut Vec<(ast::Name, Span)>,
417438
}
418439

@@ -471,16 +492,16 @@ fn extract_labels(ctxt: &mut LifetimeContext, b: hir::BodyId) {
471492
}
472493

473494
fn check_if_label_shadows_lifetime<'a>(sess: &'a Session,
474-
mut scope: Scope<'a>,
495+
mut scope: ScopeRef<'a>,
475496
label: ast::Name,
476497
label_span: Span) {
477498
loop {
478499
match *scope {
479-
FnScope { s, .. } => { scope = s; }
480-
RootScope => { return; }
500+
Scope::Fn { s, .. } => { scope = s; }
501+
Scope::Root => { return; }
481502

482-
EarlyScope(lifetimes, _, s) |
483-
LateScope(lifetimes, s) => {
503+
Scope::Early { lifetimes, s, .. } |
504+
Scope::Late { lifetimes, s } => {
484505
for lifetime_def in lifetimes {
485506
// FIXME (#24278): non-hygienic comparison
486507
if label == lifetime_def.lifetime.name {
@@ -524,7 +545,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
524545
// `self.labels_in_fn`.
525546
extract_labels(self, fb);
526547

527-
self.with(FnScope { fn_id: fn_id, body_id: fb.node_id, s: self.scope },
548+
self.with(Scope::Fn { fn_id: fn_id, body_id: fb.node_id, s: self.scope },
528549
|_old_scope, this| this.visit_nested_body(fb))
529550
}
530551

@@ -535,8 +556,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
535556
f(self)
536557
}
537558

538-
fn with<F>(&mut self, wrap_scope: ScopeChain, f: F) where
539-
F: for<'b> FnOnce(Scope, &mut LifetimeContext<'b, 'tcx>),
559+
fn with<F>(&mut self, wrap_scope: Scope, f: F) where
560+
F: for<'b> FnOnce(ScopeRef, &mut LifetimeContext<'b, 'tcx>),
540561
{
541562
let LifetimeContext {sess, hir_map, ref mut map, ..} = *self;
542563
let mut this = LifetimeContext {
@@ -591,7 +612,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
591612

592613
// Find the start of nested early scopes, e.g. in methods.
593614
let mut start = 0;
594-
if let EarlyScope(..) = *self.scope {
615+
if let Scope::Early {..} = *self.scope {
595616
let parent = self.hir_map.expect_item(self.hir_map.get_parent(fn_id));
596617
if let hir::ItemTrait(..) = parent.node {
597618
start += 1; // Self comes first.
@@ -605,8 +626,17 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
605626
}
606627
}
607628

608-
self.with(EarlyScope(&early, start as u32, self.scope), move |old_scope, this| {
609-
this.with(LateScope(&late, this.scope), move |_, this| {
629+
let early = Scope::Early {
630+
lifetimes: &early,
631+
start: start as u32,
632+
s: self.scope
633+
};
634+
self.with(early, move |old_scope, this| {
635+
let late = Scope::Late {
636+
lifetimes: &late,
637+
s: this.scope
638+
};
639+
this.with(late, move |_, this| {
610640
this.check_lifetime_defs(old_scope, &generics.lifetimes);
611641
this.hack(walk); // FIXME(#37666) workaround in place of `walk(this)`
612642
});
@@ -624,22 +654,22 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
624654
let mut scope = self.scope;
625655
loop {
626656
match *scope {
627-
FnScope {fn_id, body_id, s } => {
657+
Scope::Fn { fn_id, body_id, s } => {
628658
return self.resolve_free_lifetime_ref(
629659
region::CallSiteScopeData { fn_id: fn_id, body_id: body_id },
630660
lifetime_ref,
631661
s);
632662
}
633663

634-
RootScope => {
664+
Scope::Root => {
635665
break;
636666
}
637667

638-
EarlyScope(lifetimes, start, s) => {
668+
Scope::Early { lifetimes, start, s } => {
639669
match search_lifetimes(lifetimes, lifetime_ref) {
640670
Some((index, lifetime_def)) => {
641671
let decl_id = lifetime_def.id;
642-
let def = DefEarlyBoundRegion(start + index, decl_id);
672+
let def = Region::EarlyBound(start + index, decl_id);
643673
self.insert_lifetime(lifetime_ref, def);
644674
return;
645675
}
@@ -649,12 +679,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
649679
}
650680
}
651681

652-
LateScope(lifetimes, s) => {
682+
Scope::Late { lifetimes, s } => {
653683
match search_lifetimes(lifetimes, lifetime_ref) {
654684
Some((_index, lifetime_def)) => {
655685
let decl_id = lifetime_def.id;
656686
let debruijn = ty::DebruijnIndex::new(late_depth + 1);
657-
let def = DefLateBoundRegion(debruijn, decl_id);
687+
let def = Region::LateBound(debruijn, decl_id);
658688
self.insert_lifetime(lifetime_ref, def);
659689
return;
660690
}
@@ -674,7 +704,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
674704
fn resolve_free_lifetime_ref(&mut self,
675705
scope_data: region::CallSiteScopeData,
676706
lifetime_ref: &hir::Lifetime,
677-
scope: Scope) {
707+
scope: ScopeRef) {
678708
debug!("resolve_free_lifetime_ref \
679709
scope_data: {:?} lifetime_ref: {:?} scope: {:?}",
680710
scope_data, lifetime_ref, scope);
@@ -690,19 +720,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
690720
scope_data: {:?} scope: {:?} search_result: {:?}",
691721
scope_data, scope, search_result);
692722
match *scope {
693-
FnScope { fn_id, body_id, s } => {
723+
Scope::Fn { fn_id, body_id, s } => {
694724
scope_data = region::CallSiteScopeData {
695725
fn_id: fn_id, body_id: body_id
696726
};
697727
scope = s;
698728
}
699729

700-
RootScope => {
730+
Scope::Root => {
701731
break;
702732
}
703733

704-
EarlyScope(lifetimes, _, s) |
705-
LateScope(lifetimes, s) => {
734+
Scope::Early { lifetimes, s, .. } |
735+
Scope::Late { lifetimes, s } => {
706736
search_result = search_lifetimes(lifetimes, lifetime_ref);
707737
if search_result.is_some() {
708738
break;
@@ -714,7 +744,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
714744

715745
match search_result {
716746
Some((_depth, lifetime)) => {
717-
let def = DefFreeRegion(scope_data, lifetime.id);
747+
let def = Region::Free(scope_data, lifetime.id);
718748
self.insert_lifetime(lifetime_ref, def);
719749
}
720750

@@ -732,7 +762,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
732762
.emit();
733763
}
734764

735-
fn check_lifetime_defs(&mut self, old_scope: Scope, lifetimes: &[hir::LifetimeDef]) {
765+
fn check_lifetime_defs(&mut self, old_scope: ScopeRef, lifetimes: &[hir::LifetimeDef]) {
736766
for i in 0..lifetimes.len() {
737767
let lifetime_i = &lifetimes[i];
738768

@@ -773,7 +803,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
773803
}
774804

775805
fn check_lifetime_def_for_shadowing(&self,
776-
mut old_scope: Scope,
806+
mut old_scope: ScopeRef,
777807
lifetime: &hir::Lifetime)
778808
{
779809
for &(label, label_span) in &self.labels_in_fn {
@@ -789,16 +819,16 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
789819

790820
loop {
791821
match *old_scope {
792-
FnScope { s, .. } => {
822+
Scope::Fn { s, .. } => {
793823
old_scope = s;
794824
}
795825

796-
RootScope => {
826+
Scope::Root => {
797827
return;
798828
}
799829

800-
EarlyScope(lifetimes, _, s) |
801-
LateScope(lifetimes, s) => {
830+
Scope::Early { lifetimes, s, .. } |
831+
Scope::Late { lifetimes, s } => {
802832
if let Some((_, lifetime_def)) = search_lifetimes(lifetimes, lifetime) {
803833
signal_shadowing_problem(
804834
self.sess,
@@ -816,7 +846,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
816846

817847
fn insert_lifetime(&mut self,
818848
lifetime_ref: &hir::Lifetime,
819-
def: DefRegion) {
849+
def: Region) {
820850
if lifetime_ref.id == ast::DUMMY_NODE_ID {
821851
span_bug!(lifetime_ref.span,
822852
"lifetime reference not renumbered, \

‎src/librustc_typeck/astconv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
247247
let name = opt_lifetime.map(|l| l.name);
248248
let resolved = opt_lifetime.and_then(|l| tcx.named_region_map.defs.get(&l.id));
249249
let r = tcx.mk_region(match resolved {
250-
Some(&rl::DefStaticRegion) => {
250+
Some(&rl::Region::Static) => {
251251
ty::ReStatic
252252
}
253253

254-
Some(&rl::DefLateBoundRegion(debruijn, id)) => {
254+
Some(&rl::Region::LateBound(debruijn, id)) => {
255255
// If this region is declared on a function, it will have
256256
// an entry in `late_bound`, but if it comes from
257257
// `for<'a>` in some type or something, it won't
@@ -268,15 +268,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
268268
issue_32330))
269269
}
270270

271-
Some(&rl::DefEarlyBoundRegion(index, _)) => {
271+
Some(&rl::Region::EarlyBound(index, _)) => {
272272
ty::ReEarlyBound(ty::EarlyBoundRegion {
273273
index: index,
274274
name: name.unwrap()
275275
})
276276
}
277277

278-
Some(&rl::DefFreeRegion(scope, id)) => {
279-
// As in DefLateBoundRegion above, could be missing for some late-bound
278+
Some(&rl::Region::Free(scope, id)) => {
279+
// As in Region::LateBound above, could be missing for some late-bound
280280
// regions, but also for early-bound regions.
281281
let issue_32330 = tcx.named_region_map
282282
.late_bound

‎src/librustc_typeck/variance/constraints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
154154
let tcx = self.terms_cx.tcx;
155155
assert!(is_lifetime(&tcx.hir, param_id));
156156
match tcx.named_region_map.defs.get(&param_id) {
157-
Some(&rl::DefEarlyBoundRegion(_, lifetime_decl_id)) => lifetime_decl_id,
157+
Some(&rl::Region::EarlyBound(_, lifetime_decl_id)) => lifetime_decl_id,
158158
Some(_) => bug!("should not encounter non early-bound cases"),
159159

160160
// The lookup should only fail when `param_id` is

‎src/librustdoc/clean/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use syntax::symbol::keywords;
2828
use syntax_pos::{self, DUMMY_SP, Pos};
2929

3030
use rustc::middle::privacy::AccessLevels;
31-
use rustc::middle::resolve_lifetime::DefRegion::*;
31+
use rustc::middle::resolve_lifetime as rl;
3232
use rustc::middle::lang_items;
3333
use rustc::hir::def::{Def, CtorKind};
3434
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -765,9 +765,9 @@ impl Clean<Lifetime> for hir::Lifetime {
765765
fn clean(&self, cx: &DocContext) -> Lifetime {
766766
let def = cx.tcx.named_region_map.defs.get(&self.id).cloned();
767767
match def {
768-
Some(DefEarlyBoundRegion(_, node_id)) |
769-
Some(DefLateBoundRegion(_, node_id)) |
770-
Some(DefFreeRegion(_, node_id)) => {
768+
Some(rl::Region::EarlyBound(_, node_id)) |
769+
Some(rl::Region::LateBound(_, node_id)) |
770+
Some(rl::Region::Free(_, node_id)) => {
771771
if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
772772
return lt;
773773
}

0 commit comments

Comments
 (0)
Please sign in to comment.