From 81f6ce5ce3e9c71619f260b95e03f51567252805 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 26 Oct 2017 19:53:31 -0400 Subject: [PATCH] rename `Lookup` to `TyContext` and pass more info when visiting tys --- src/librustc/mir/visit.rs | 51 ++++++++++++------- src/librustc_mir/build/mod.rs | 4 +- .../transform/clean_end_regions.rs | 4 +- src/librustc_mir/transform/erase_regions.rs | 4 +- .../transform/nll/region_infer.rs | 1 - src/librustc_mir/transform/nll/renumber.rs | 32 ++++++------ src/librustc_mir/transform/type_check.rs | 4 +- src/librustc_passes/mir_stats.rs | 5 +- 8 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs index 63652980f9b..2d9d3643ff6 100644 --- a/src/librustc/mir/visit.rs +++ b/src/librustc/mir/visit.rs @@ -209,7 +209,7 @@ macro_rules! make_mir_visitor { fn visit_ty(&mut self, ty: & $($mutability)* Ty<'tcx>, - _: Lookup) { + _: TyContext) { self.super_ty(ty); } @@ -256,8 +256,9 @@ macro_rules! make_mir_visitor { } fn visit_local_decl(&mut self, + local: Local, local_decl: & $($mutability)* LocalDecl<'tcx>) { - self.super_local_decl(local_decl); + self.super_local_decl(local, local_decl); } fn visit_local(&mut self, @@ -291,14 +292,14 @@ macro_rules! make_mir_visitor { self.visit_visibility_scope_data(scope); } - let lookup = Lookup::Src(SourceInfo { + let lookup = TyContext::SourceInfo(SourceInfo { span: mir.span, scope: ARGUMENT_VISIBILITY_SCOPE, }); self.visit_ty(&$($mutability)* mir.return_ty, lookup); - for local_decl in &$($mutability)* mir.local_decls { - self.visit_local_decl(local_decl); + for local in mir.local_decls.indices() { + self.visit_local_decl(local, & $($mutability)* mir.local_decls[local]); } self.visit_span(&$($mutability)* mir.span); @@ -359,7 +360,8 @@ macro_rules! make_mir_visitor { for operand in lvalues { self.visit_lvalue(& $($mutability)* operand.lval, LvalueContext::Validate, location); - self.visit_ty(& $($mutability)* operand.ty, Lookup::Loc(location)); + self.visit_ty(& $($mutability)* operand.ty, + TyContext::Location(location)); } } StatementKind::SetDiscriminant{ ref $($mutability)* lvalue, .. } => { @@ -421,7 +423,7 @@ macro_rules! make_mir_visitor { ref values, ref targets } => { self.visit_operand(discr, source_location); - self.visit_ty(switch_ty, Lookup::Loc(source_location)); + self.visit_ty(switch_ty, TyContext::Location(source_location)); for value in &values[..] { self.visit_const_int(value, source_location); } @@ -538,7 +540,7 @@ macro_rules! make_mir_visitor { ref $($mutability)* operand, ref $($mutability)* ty) => { self.visit_operand(operand, location); - self.visit_ty(ty, Lookup::Loc(location)); + self.visit_ty(ty, TyContext::Location(location)); } Rvalue::BinaryOp(_bin_op, @@ -560,7 +562,7 @@ macro_rules! make_mir_visitor { } Rvalue::NullaryOp(_op, ref $($mutability)* ty) => { - self.visit_ty(ty, Lookup::Loc(location)); + self.visit_ty(ty, TyContext::Location(location)); } Rvalue::Aggregate(ref $($mutability)* kind, @@ -568,7 +570,7 @@ macro_rules! make_mir_visitor { let kind = &$($mutability)* **kind; match *kind { AggregateKind::Array(ref $($mutability)* ty) => { - self.visit_ty(ty, Lookup::Loc(location)); + self.visit_ty(ty, TyContext::Location(location)); } AggregateKind::Tuple => { } @@ -638,7 +640,7 @@ macro_rules! make_mir_visitor { ref $($mutability)* ty, } = *static_; self.visit_def_id(def_id, location); - self.visit_ty(ty, Lookup::Loc(location)); + self.visit_ty(ty, TyContext::Location(location)); } fn super_projection(&mut self, @@ -668,7 +670,7 @@ macro_rules! make_mir_visitor { ProjectionElem::Subslice { from: _, to: _ } => { } ProjectionElem::Field(_field, ref $($mutability)* ty) => { - self.visit_ty(ty, Lookup::Loc(location)); + self.visit_ty(ty, TyContext::Location(location)); } ProjectionElem::Index(ref $($mutability)* local) => { self.visit_local(local, LvalueContext::Consume, location); @@ -683,6 +685,7 @@ macro_rules! make_mir_visitor { } fn super_local_decl(&mut self, + local: Local, local_decl: & $($mutability)* LocalDecl<'tcx>) { let LocalDecl { mutability: _, @@ -694,7 +697,10 @@ macro_rules! make_mir_visitor { is_user_variable: _, } = *local_decl; - self.visit_ty(ty, Lookup::Src(*source_info)); + self.visit_ty(ty, TyContext::LocalDecl { + local, + source_info: *source_info, + }); self.visit_source_info(source_info); self.visit_visibility_scope(lexical_scope); } @@ -718,7 +724,7 @@ macro_rules! make_mir_visitor { } = *constant; self.visit_span(span); - self.visit_ty(ty, Lookup::Loc(location)); + self.visit_ty(ty, TyContext::Location(location)); self.visit_literal(literal, location); } @@ -796,10 +802,21 @@ macro_rules! make_mir_visitor { make_mir_visitor!(Visitor,); make_mir_visitor!(MutVisitor,mut); +/// Extra information passed to `visit_ty` and friends to give context +/// about where the type etc appears. #[derive(Copy, Clone, Debug)] -pub enum Lookup { - Loc(Location), - Src(SourceInfo), +pub enum TyContext { + LocalDecl { + /// The index of the local variable we are visiting. + local: Local, + + /// The source location where this local variable was declared. + source_info: SourceInfo, + }, + + Location(Location), + + SourceInfo(SourceInfo), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index 77496c7b8f2..2073d495300 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -17,7 +17,7 @@ use rustc::hir::def_id::DefId; use rustc::middle::region; use rustc::mir::*; use rustc::mir::transform::MirSource; -use rustc::mir::visit::{MutVisitor, Lookup}; +use rustc::mir::visit::{MutVisitor, TyContext}; use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::subst::Substs; use rustc::util::nodemap::NodeMap; @@ -165,7 +165,7 @@ struct GlobalizeMir<'a, 'gcx: 'a> { } impl<'a, 'gcx: 'tcx, 'tcx> MutVisitor<'tcx> for GlobalizeMir<'a, 'gcx> { - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) { if let Some(lifted) = self.tcx.lift(ty) { *ty = lifted; } else { diff --git a/src/librustc_mir/transform/clean_end_regions.rs b/src/librustc_mir/transform/clean_end_regions.rs index a6750f400ba..d356d3b5a85 100644 --- a/src/librustc_mir/transform/clean_end_regions.rs +++ b/src/librustc_mir/transform/clean_end_regions.rs @@ -24,7 +24,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc::middle::region; use rustc::mir::transform::{MirPass, MirSource}; use rustc::mir::{BasicBlock, Location, Mir, Rvalue, Statement, StatementKind}; -use rustc::mir::visit::{MutVisitor, Visitor, Lookup}; +use rustc::mir::visit::{MutVisitor, Visitor, TyContext}; use rustc::ty::{Ty, RegionKind, TyCtxt}; pub struct CleanEndRegions; @@ -67,7 +67,7 @@ impl<'tcx> Visitor<'tcx> for GatherBorrowedRegions { self.super_rvalue(rvalue, location); } - fn visit_ty(&mut self, ty: &Ty<'tcx>, _: Lookup) { + fn visit_ty(&mut self, ty: &Ty<'tcx>, _: TyContext) { // Gather regions that occur in types for re in ty.walk().flat_map(|t| t.regions()) { match *re { diff --git a/src/librustc_mir/transform/erase_regions.rs b/src/librustc_mir/transform/erase_regions.rs index dc18cdd8f0d..08ca20e50eb 100644 --- a/src/librustc_mir/transform/erase_regions.rs +++ b/src/librustc_mir/transform/erase_regions.rs @@ -17,7 +17,7 @@ use rustc::ty::subst::Substs; use rustc::ty::{self, Ty, TyCtxt}; use rustc::mir::*; -use rustc::mir::visit::{MutVisitor, Lookup}; +use rustc::mir::visit::{MutVisitor, TyContext}; use rustc::mir::transform::{MirPass, MirSource}; struct EraseRegionsVisitor<'a, 'tcx: 'a> { @@ -35,7 +35,7 @@ impl<'a, 'tcx> EraseRegionsVisitor<'a, 'tcx> { } impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> { - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: TyContext) { if !self.in_validation_statement { *ty = self.tcx.erase_regions(ty); } diff --git a/src/librustc_mir/transform/nll/region_infer.rs b/src/librustc_mir/transform/nll/region_infer.rs index c23d73e784a..b825528b1bc 100644 --- a/src/librustc_mir/transform/nll/region_infer.rs +++ b/src/librustc_mir/transform/nll/region_infer.rs @@ -60,7 +60,6 @@ impl RegionInferenceContext { } } - /// Returns an iterator over all the region indices. pub fn regions(&self) -> impl Iterator { self.definitions.indices() diff --git a/src/librustc_mir/transform/nll/renumber.rs b/src/librustc_mir/transform/nll/renumber.rs index 589179c2066..40ce44bae9d 100644 --- a/src/librustc_mir/transform/nll/renumber.rs +++ b/src/librustc_mir/transform/nll/renumber.rs @@ -12,7 +12,7 @@ use rustc::ty::TypeFoldable; use rustc::ty::subst::{Kind, Substs}; use rustc::ty::{Ty, ClosureSubsts, RegionVid, RegionKind}; use rustc::mir::{Mir, Location, Rvalue, BasicBlock, Statement, StatementKind}; -use rustc::mir::visit::{MutVisitor, Lookup}; +use rustc::mir::visit::{MutVisitor, TyContext}; use rustc::infer::{self as rustc_infer, InferCtxt}; use syntax_pos::DUMMY_SP; use std::collections::HashMap; @@ -29,7 +29,7 @@ pub fn renumber_mir<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>, } struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { - lookup_map: HashMap, + lookup_map: HashMap, num_region_variables: usize, infcx: &'a InferCtxt<'a, 'gcx, 'tcx>, } @@ -50,39 +50,39 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> { }) } - fn store_region(&mut self, region: &RegionKind, lookup: Lookup) { + fn store_region(&mut self, region: &RegionKind, lookup: TyContext) { if let RegionKind::ReVar(rid) = *region { self.lookup_map.entry(rid).or_insert(lookup); } } - fn store_ty_regions(&mut self, ty: &Ty<'tcx>, lookup: Lookup) { + fn store_ty_regions(&mut self, ty: &Ty<'tcx>, ty_context: TyContext) { for region in ty.regions() { - self.store_region(region, lookup); + self.store_region(region, ty_context); } } - fn store_kind_regions(&mut self, kind: &'tcx Kind, lookup: Lookup) { + fn store_kind_regions(&mut self, kind: &'tcx Kind, ty_context: TyContext) { if let Some(ty) = kind.as_type() { - self.store_ty_regions(&ty, lookup); + self.store_ty_regions(&ty, ty_context); } else if let Some(region) = kind.as_region() { - self.store_region(region, lookup); + self.store_region(region, ty_context); } } } impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> { - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, lookup: Lookup) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) { let old_ty = *ty; *ty = self.renumber_regions(&old_ty); - self.store_ty_regions(ty, lookup); + self.store_ty_regions(ty, ty_context); } fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, location: Location) { *substs = self.renumber_regions(&{*substs}); - let lookup = Lookup::Loc(location); + let ty_context = TyContext::Location(location); for kind in *substs { - self.store_kind_regions(kind, lookup); + self.store_kind_regions(kind, ty_context); } } @@ -91,8 +91,8 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> { Rvalue::Ref(ref mut r, _, _) => { let old_r = *r; *r = self.renumber_regions(&old_r); - let lookup = Lookup::Loc(location); - self.store_region(r, lookup); + let ty_context = TyContext::Location(location); + self.store_region(r, ty_context); } Rvalue::Use(..) | Rvalue::Repeat(..) | @@ -114,9 +114,9 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> { substs: &mut ClosureSubsts<'tcx>, location: Location) { *substs = self.renumber_regions(substs); - let lookup = Lookup::Loc(location); + let ty_context = TyContext::Location(location); for kind in substs.substs { - self.store_kind_regions(kind, lookup); + self.store_kind_regions(kind, ty_context); } } diff --git a/src/librustc_mir/transform/type_check.rs b/src/librustc_mir/transform/type_check.rs index ab5998a3480..d238b145d42 100644 --- a/src/librustc_mir/transform/type_check.rs +++ b/src/librustc_mir/transform/type_check.rs @@ -92,8 +92,8 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> { self.sanitize_type(rvalue, rval_ty); } - fn visit_local_decl(&mut self, local_decl: &LocalDecl<'tcx>) { - self.super_local_decl(local_decl); + fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) { + self.super_local_decl(local, local_decl); self.sanitize_type(local_decl, local_decl.ty); } diff --git a/src/librustc_passes/mir_stats.rs b/src/librustc_passes/mir_stats.rs index 1fa49614580..3f75c638522 100644 --- a/src/librustc_passes/mir_stats.rs +++ b/src/librustc_passes/mir_stats.rs @@ -14,7 +14,7 @@ use rustc_const_math::{ConstUsize}; use rustc::mir::{AggregateKind, AssertMessage, BasicBlock, BasicBlockData}; -use rustc::mir::{Constant, Literal, Location, LocalDecl}; +use rustc::mir::{Constant, Literal, Location, Local, LocalDecl}; use rustc::mir::{Lvalue, LvalueElem, LvalueProjection}; use rustc::mir::{Mir, Operand, ProjectionElem}; use rustc::mir::{Rvalue, SourceInfo, Statement, StatementKind}; @@ -269,9 +269,10 @@ impl<'a, 'tcx> mir_visit::Visitor<'tcx> for StatCollector<'a, 'tcx> { } fn visit_local_decl(&mut self, + local: Local, local_decl: &LocalDecl<'tcx>) { self.record("LocalDecl", local_decl); - self.super_local_decl(local_decl); + self.super_local_decl(local, local_decl); } fn visit_visibility_scope(&mut self,