Skip to content

Commit 81f6ce5

Browse files
committed
rename Lookup to TyContext and pass more info when visiting tys
1 parent 2379faa commit 81f6ce5

File tree

8 files changed

+61
-44
lines changed

8 files changed

+61
-44
lines changed

src/librustc/mir/visit.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ macro_rules! make_mir_visitor {
209209

210210
fn visit_ty(&mut self,
211211
ty: & $($mutability)* Ty<'tcx>,
212-
_: Lookup) {
212+
_: TyContext) {
213213
self.super_ty(ty);
214214
}
215215

@@ -256,8 +256,9 @@ macro_rules! make_mir_visitor {
256256
}
257257

258258
fn visit_local_decl(&mut self,
259+
local: Local,
259260
local_decl: & $($mutability)* LocalDecl<'tcx>) {
260-
self.super_local_decl(local_decl);
261+
self.super_local_decl(local, local_decl);
261262
}
262263

263264
fn visit_local(&mut self,
@@ -291,14 +292,14 @@ macro_rules! make_mir_visitor {
291292
self.visit_visibility_scope_data(scope);
292293
}
293294

294-
let lookup = Lookup::Src(SourceInfo {
295+
let lookup = TyContext::SourceInfo(SourceInfo {
295296
span: mir.span,
296297
scope: ARGUMENT_VISIBILITY_SCOPE,
297298
});
298299
self.visit_ty(&$($mutability)* mir.return_ty, lookup);
299300

300-
for local_decl in &$($mutability)* mir.local_decls {
301-
self.visit_local_decl(local_decl);
301+
for local in mir.local_decls.indices() {
302+
self.visit_local_decl(local, & $($mutability)* mir.local_decls[local]);
302303
}
303304

304305
self.visit_span(&$($mutability)* mir.span);
@@ -359,7 +360,8 @@ macro_rules! make_mir_visitor {
359360
for operand in lvalues {
360361
self.visit_lvalue(& $($mutability)* operand.lval,
361362
LvalueContext::Validate, location);
362-
self.visit_ty(& $($mutability)* operand.ty, Lookup::Loc(location));
363+
self.visit_ty(& $($mutability)* operand.ty,
364+
TyContext::Location(location));
363365
}
364366
}
365367
StatementKind::SetDiscriminant{ ref $($mutability)* lvalue, .. } => {
@@ -421,7 +423,7 @@ macro_rules! make_mir_visitor {
421423
ref values,
422424
ref targets } => {
423425
self.visit_operand(discr, source_location);
424-
self.visit_ty(switch_ty, Lookup::Loc(source_location));
426+
self.visit_ty(switch_ty, TyContext::Location(source_location));
425427
for value in &values[..] {
426428
self.visit_const_int(value, source_location);
427429
}
@@ -538,7 +540,7 @@ macro_rules! make_mir_visitor {
538540
ref $($mutability)* operand,
539541
ref $($mutability)* ty) => {
540542
self.visit_operand(operand, location);
541-
self.visit_ty(ty, Lookup::Loc(location));
543+
self.visit_ty(ty, TyContext::Location(location));
542544
}
543545

544546
Rvalue::BinaryOp(_bin_op,
@@ -560,15 +562,15 @@ macro_rules! make_mir_visitor {
560562
}
561563

562564
Rvalue::NullaryOp(_op, ref $($mutability)* ty) => {
563-
self.visit_ty(ty, Lookup::Loc(location));
565+
self.visit_ty(ty, TyContext::Location(location));
564566
}
565567

566568
Rvalue::Aggregate(ref $($mutability)* kind,
567569
ref $($mutability)* operands) => {
568570
let kind = &$($mutability)* **kind;
569571
match *kind {
570572
AggregateKind::Array(ref $($mutability)* ty) => {
571-
self.visit_ty(ty, Lookup::Loc(location));
573+
self.visit_ty(ty, TyContext::Location(location));
572574
}
573575
AggregateKind::Tuple => {
574576
}
@@ -638,7 +640,7 @@ macro_rules! make_mir_visitor {
638640
ref $($mutability)* ty,
639641
} = *static_;
640642
self.visit_def_id(def_id, location);
641-
self.visit_ty(ty, Lookup::Loc(location));
643+
self.visit_ty(ty, TyContext::Location(location));
642644
}
643645

644646
fn super_projection(&mut self,
@@ -668,7 +670,7 @@ macro_rules! make_mir_visitor {
668670
ProjectionElem::Subslice { from: _, to: _ } => {
669671
}
670672
ProjectionElem::Field(_field, ref $($mutability)* ty) => {
671-
self.visit_ty(ty, Lookup::Loc(location));
673+
self.visit_ty(ty, TyContext::Location(location));
672674
}
673675
ProjectionElem::Index(ref $($mutability)* local) => {
674676
self.visit_local(local, LvalueContext::Consume, location);
@@ -683,6 +685,7 @@ macro_rules! make_mir_visitor {
683685
}
684686

685687
fn super_local_decl(&mut self,
688+
local: Local,
686689
local_decl: & $($mutability)* LocalDecl<'tcx>) {
687690
let LocalDecl {
688691
mutability: _,
@@ -694,7 +697,10 @@ macro_rules! make_mir_visitor {
694697
is_user_variable: _,
695698
} = *local_decl;
696699

697-
self.visit_ty(ty, Lookup::Src(*source_info));
700+
self.visit_ty(ty, TyContext::LocalDecl {
701+
local,
702+
source_info: *source_info,
703+
});
698704
self.visit_source_info(source_info);
699705
self.visit_visibility_scope(lexical_scope);
700706
}
@@ -718,7 +724,7 @@ macro_rules! make_mir_visitor {
718724
} = *constant;
719725

720726
self.visit_span(span);
721-
self.visit_ty(ty, Lookup::Loc(location));
727+
self.visit_ty(ty, TyContext::Location(location));
722728
self.visit_literal(literal, location);
723729
}
724730

@@ -796,10 +802,21 @@ macro_rules! make_mir_visitor {
796802
make_mir_visitor!(Visitor,);
797803
make_mir_visitor!(MutVisitor,mut);
798804

805+
/// Extra information passed to `visit_ty` and friends to give context
806+
/// about where the type etc appears.
799807
#[derive(Copy, Clone, Debug)]
800-
pub enum Lookup {
801-
Loc(Location),
802-
Src(SourceInfo),
808+
pub enum TyContext {
809+
LocalDecl {
810+
/// The index of the local variable we are visiting.
811+
local: Local,
812+
813+
/// The source location where this local variable was declared.
814+
source_info: SourceInfo,
815+
},
816+
817+
Location(Location),
818+
819+
SourceInfo(SourceInfo),
803820
}
804821

805822
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

src/librustc_mir/build/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::hir::def_id::DefId;
1717
use rustc::middle::region;
1818
use rustc::mir::*;
1919
use rustc::mir::transform::MirSource;
20-
use rustc::mir::visit::{MutVisitor, Lookup};
20+
use rustc::mir::visit::{MutVisitor, TyContext};
2121
use rustc::ty::{self, Ty, TyCtxt};
2222
use rustc::ty::subst::Substs;
2323
use rustc::util::nodemap::NodeMap;
@@ -165,7 +165,7 @@ struct GlobalizeMir<'a, 'gcx: 'a> {
165165
}
166166

167167
impl<'a, 'gcx: 'tcx, 'tcx> MutVisitor<'tcx> for GlobalizeMir<'a, 'gcx> {
168-
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) {
168+
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
169169
if let Some(lifted) = self.tcx.lift(ty) {
170170
*ty = lifted;
171171
} else {

src/librustc_mir/transform/clean_end_regions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_data_structures::fx::FxHashSet;
2424
use rustc::middle::region;
2525
use rustc::mir::transform::{MirPass, MirSource};
2626
use rustc::mir::{BasicBlock, Location, Mir, Rvalue, Statement, StatementKind};
27-
use rustc::mir::visit::{MutVisitor, Visitor, Lookup};
27+
use rustc::mir::visit::{MutVisitor, Visitor, TyContext};
2828
use rustc::ty::{Ty, RegionKind, TyCtxt};
2929

3030
pub struct CleanEndRegions;
@@ -67,7 +67,7 @@ impl<'tcx> Visitor<'tcx> for GatherBorrowedRegions {
6767
self.super_rvalue(rvalue, location);
6868
}
6969

70-
fn visit_ty(&mut self, ty: &Ty<'tcx>, _: Lookup) {
70+
fn visit_ty(&mut self, ty: &Ty<'tcx>, _: TyContext) {
7171
// Gather regions that occur in types
7272
for re in ty.walk().flat_map(|t| t.regions()) {
7373
match *re {

src/librustc_mir/transform/erase_regions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use rustc::ty::subst::Substs;
1818
use rustc::ty::{self, Ty, TyCtxt};
1919
use rustc::mir::*;
20-
use rustc::mir::visit::{MutVisitor, Lookup};
20+
use rustc::mir::visit::{MutVisitor, TyContext};
2121
use rustc::mir::transform::{MirPass, MirSource};
2222

2323
struct EraseRegionsVisitor<'a, 'tcx: 'a> {
@@ -35,7 +35,7 @@ impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> {
3535
}
3636

3737
impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
38-
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) {
38+
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) {
3939
if !self.in_validation_statement {
4040
*ty = self.tcx.erase_regions(ty);
4141
}

src/librustc_mir/transform/nll/region_infer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ impl RegionInferenceContext {
6060
}
6161
}
6262

63-
6463
/// Returns an iterator over all the region indices.
6564
pub fn regions(&self) -> impl Iterator<Item = RegionIndex> {
6665
self.definitions.indices()

src/librustc_mir/transform/nll/renumber.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc::ty::TypeFoldable;
1212
use rustc::ty::subst::{Kind, Substs};
1313
use rustc::ty::{Ty, ClosureSubsts, RegionVid, RegionKind};
1414
use rustc::mir::{Mir, Location, Rvalue, BasicBlock, Statement, StatementKind};
15-
use rustc::mir::visit::{MutVisitor, Lookup};
15+
use rustc::mir::visit::{MutVisitor, TyContext};
1616
use rustc::infer::{self as rustc_infer, InferCtxt};
1717
use syntax_pos::DUMMY_SP;
1818
use std::collections::HashMap;
@@ -29,7 +29,7 @@ pub fn renumber_mir<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
2929
}
3030

3131
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
32-
lookup_map: HashMap<RegionVid, Lookup>,
32+
lookup_map: HashMap<RegionVid, TyContext>,
3333
num_region_variables: usize,
3434
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
3535
}
@@ -50,39 +50,39 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
5050
})
5151
}
5252

53-
fn store_region(&mut self, region: &RegionKind, lookup: Lookup) {
53+
fn store_region(&mut self, region: &RegionKind, lookup: TyContext) {
5454
if let RegionKind::ReVar(rid) = *region {
5555
self.lookup_map.entry(rid).or_insert(lookup);
5656
}
5757
}
5858

59-
fn store_ty_regions(&mut self, ty: &Ty<'tcx>, lookup: Lookup) {
59+
fn store_ty_regions(&mut self, ty: &Ty<'tcx>, ty_context: TyContext) {
6060
for region in ty.regions() {
61-
self.store_region(region, lookup);
61+
self.store_region(region, ty_context);
6262
}
6363
}
6464

65-
fn store_kind_regions(&mut self, kind: &'tcx Kind, lookup: Lookup) {
65+
fn store_kind_regions(&mut self, kind: &'tcx Kind, ty_context: TyContext) {
6666
if let Some(ty) = kind.as_type() {
67-
self.store_ty_regions(&ty, lookup);
67+
self.store_ty_regions(&ty, ty_context);
6868
} else if let Some(region) = kind.as_region() {
69-
self.store_region(region, lookup);
69+
self.store_region(region, ty_context);
7070
}
7171
}
7272
}
7373

7474
impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
75-
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, lookup: Lookup) {
75+
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) {
7676
let old_ty = *ty;
7777
*ty = self.renumber_regions(&old_ty);
78-
self.store_ty_regions(ty, lookup);
78+
self.store_ty_regions(ty, ty_context);
7979
}
8080

8181
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, location: Location) {
8282
*substs = self.renumber_regions(&{*substs});
83-
let lookup = Lookup::Loc(location);
83+
let ty_context = TyContext::Location(location);
8484
for kind in *substs {
85-
self.store_kind_regions(kind, lookup);
85+
self.store_kind_regions(kind, ty_context);
8686
}
8787
}
8888

@@ -91,8 +91,8 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
9191
Rvalue::Ref(ref mut r, _, _) => {
9292
let old_r = *r;
9393
*r = self.renumber_regions(&old_r);
94-
let lookup = Lookup::Loc(location);
95-
self.store_region(r, lookup);
94+
let ty_context = TyContext::Location(location);
95+
self.store_region(r, ty_context);
9696
}
9797
Rvalue::Use(..) |
9898
Rvalue::Repeat(..) |
@@ -114,9 +114,9 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
114114
substs: &mut ClosureSubsts<'tcx>,
115115
location: Location) {
116116
*substs = self.renumber_regions(substs);
117-
let lookup = Lookup::Loc(location);
117+
let ty_context = TyContext::Location(location);
118118
for kind in substs.substs {
119-
self.store_kind_regions(kind, lookup);
119+
self.store_kind_regions(kind, ty_context);
120120
}
121121
}
122122

src/librustc_mir/transform/type_check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
9292
self.sanitize_type(rvalue, rval_ty);
9393
}
9494

95-
fn visit_local_decl(&mut self, local_decl: &LocalDecl<'tcx>) {
96-
self.super_local_decl(local_decl);
95+
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
96+
self.super_local_decl(local, local_decl);
9797
self.sanitize_type(local_decl, local_decl.ty);
9898
}
9999

src/librustc_passes/mir_stats.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use rustc_const_math::{ConstUsize};
1616
use rustc::mir::{AggregateKind, AssertMessage, BasicBlock, BasicBlockData};
17-
use rustc::mir::{Constant, Literal, Location, LocalDecl};
17+
use rustc::mir::{Constant, Literal, Location, Local, LocalDecl};
1818
use rustc::mir::{Lvalue, LvalueElem, LvalueProjection};
1919
use rustc::mir::{Mir, Operand, ProjectionElem};
2020
use rustc::mir::{Rvalue, SourceInfo, Statement, StatementKind};
@@ -269,9 +269,10 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> {
269269
}
270270

271271
fn visit_local_decl(&mut self,
272+
local: Local,
272273
local_decl: &LocalDecl<'tcx>) {
273274
self.record("LocalDecl", local_decl);
274-
self.super_local_decl(local_decl);
275+
self.super_local_decl(local, local_decl);
275276
}
276277

277278
fn visit_visibility_scope(&mut self,

0 commit comments

Comments
 (0)