From 059a9a16d54f7a8e6399810893c9988f409d3025 Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Tue, 18 Jul 2017 22:31:38 -0400 Subject: [PATCH 1/2] Provide positional information when visiting ty, substs and closure_substs in MIR --- src/librustc/mir/visit.rs | 39 +++++++++++++-------- src/librustc_mir/build/mod.rs | 6 ++-- src/librustc_mir/transform/erase_regions.rs | 9 ++--- src/librustc_passes/mir_stats.rs | 3 +- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs index ac1c0306f70..2e7dfa8f70d 100644 --- a/src/librustc/mir/visit.rs +++ b/src/librustc/mir/visit.rs @@ -210,17 +210,20 @@ fn visit_source_info(&mut self, } fn visit_ty(&mut self, - ty: & $($mutability)* Ty<'tcx>) { + ty: & $($mutability)* Ty<'tcx>, + _: PositionalInfo) { self.super_ty(ty); } fn visit_substs(&mut self, - substs: & $($mutability)* &'tcx Substs<'tcx>) { + substs: & $($mutability)* &'tcx Substs<'tcx>, + _: Location) { self.super_substs(substs); } fn visit_closure_substs(&mut self, - substs: & $($mutability)* ClosureSubsts<'tcx>) { + substs: & $($mutability)* ClosureSubsts<'tcx>, + _: Location) { self.super_closure_substs(substs); } @@ -266,7 +269,7 @@ fn super_mir(&mut self, self.visit_visibility_scope_data(scope); } - self.visit_ty(&$($mutability)* mir.return_ty); + self.visit_ty(&$($mutability)* mir.return_ty, PositionalInfo::Span(mir.span)); for local_decl in &$($mutability)* mir.local_decls { self.visit_local_decl(local_decl); @@ -385,7 +388,7 @@ fn super_terminator_kind(&mut self, ref values, ref targets } => { self.visit_operand(discr, source_location); - self.visit_ty(switch_ty); + self.visit_ty(switch_ty, PositionalInfo::Location(source_location)); for value in &values[..] { self.visit_const_int(value, source_location); } @@ -489,7 +492,7 @@ fn super_rvalue(&mut self, ref $($mutability)* operand, ref $($mutability)* ty) => { self.visit_operand(operand, location); - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::Location(location)); } Rvalue::BinaryOp(_bin_op, @@ -511,7 +514,7 @@ fn super_rvalue(&mut self, } Rvalue::NullaryOp(_op, ref $($mutability)* ty) => { - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::Location(location)); } Rvalue::Aggregate(ref $($mutability)* kind, @@ -519,7 +522,7 @@ fn super_rvalue(&mut self, let kind = &$($mutability)* **kind; match *kind { AggregateKind::Array(ref $($mutability)* ty) => { - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::Location(location)); } AggregateKind::Tuple => { } @@ -527,12 +530,12 @@ fn super_rvalue(&mut self, _variant_index, ref $($mutability)* substs, _active_field_index) => { - self.visit_substs(substs); + self.visit_substs(substs, location); } AggregateKind::Closure(ref $($mutability)* def_id, ref $($mutability)* closure_substs) => { self.visit_def_id(def_id, location); - self.visit_closure_substs(closure_substs); + self.visit_closure_substs(closure_substs, location); } } @@ -581,7 +584,7 @@ fn super_static(&mut self, ref $($mutability)* ty, } = *static_; self.visit_def_id(def_id, location); - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::Location(location)); } fn super_projection(&mut self, @@ -611,7 +614,7 @@ fn super_projection_elem(&mut self, ProjectionElem::Subslice { from: _, to: _ } => { } ProjectionElem::Field(_field, ref $($mutability)* ty) => { - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::Location(location)); } ProjectionElem::Index(ref $($mutability)* operand) => { self.visit_operand(operand, location); @@ -635,7 +638,7 @@ fn super_local_decl(&mut self, is_user_variable: _, } = *local_decl; - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::SourceInfo(*source_info)); self.visit_source_info(source_info); } @@ -658,7 +661,7 @@ fn super_constant(&mut self, } = *constant; self.visit_span(span); - self.visit_ty(ty); + self.visit_ty(ty, PositionalInfo::Location(location)); self.visit_literal(literal, location); } @@ -669,7 +672,7 @@ fn super_literal(&mut self, Literal::Item { ref $($mutability)* def_id, ref $($mutability)* substs } => { self.visit_def_id(def_id, location); - self.visit_substs(substs); + self.visit_substs(substs, location); } Literal::Value { ref $($mutability)* value } => { self.visit_const_val(value, location); @@ -734,6 +737,12 @@ fn visit_location(&mut self, mir: & $($mutability)* Mir<'tcx>, location: Locatio make_mir_visitor!(Visitor,); make_mir_visitor!(MutVisitor,mut); +pub enum PositionalInfo { + Location(Location), + SourceInfo(SourceInfo), + Span(Span), +} + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum LvalueContext<'tcx> { // Appears as LHS of an assignment diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index eb1414d42e1..abb431f70eb 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -17,7 +17,7 @@ use rustc::middle::region::CodeExtent; use rustc::mir::*; use rustc::mir::transform::MirSource; -use rustc::mir::visit::MutVisitor; +use rustc::mir::visit::{MutVisitor, PositionalInfo}; use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::subst::Substs; use rustc::util::nodemap::NodeMap; @@ -143,7 +143,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>) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: PositionalInfo) { if let Some(lifted) = self.tcx.lift(ty) { *ty = lifted; } else { @@ -153,7 +153,7 @@ fn visit_ty(&mut self, ty: &mut Ty<'tcx>) { } } - fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>) { + fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, _: Location) { if let Some(lifted) = self.tcx.lift(substs) { *substs = lifted; } else { diff --git a/src/librustc_mir/transform/erase_regions.rs b/src/librustc_mir/transform/erase_regions.rs index eb283df869f..45a0fab7b62 100644 --- a/src/librustc_mir/transform/erase_regions.rs +++ b/src/librustc_mir/transform/erase_regions.rs @@ -15,7 +15,7 @@ use rustc::ty::subst::Substs; use rustc::ty::{Ty, TyCtxt, ClosureSubsts}; use rustc::mir::*; -use rustc::mir::visit::MutVisitor; +use rustc::mir::visit::{MutVisitor, PositionalInfo}; use rustc::mir::transform::{MirPass, MirSource}; struct EraseRegionsVisitor<'a, 'tcx: 'a> { @@ -31,12 +31,12 @@ pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self { } impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> { - fn visit_ty(&mut self, ty: &mut Ty<'tcx>) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: PositionalInfo) { let old_ty = *ty; *ty = self.tcx.erase_regions(&old_ty); } - fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>) { + fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, _: Location) { *substs = self.tcx.erase_regions(&{*substs}); } @@ -62,7 +62,8 @@ fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) { } fn visit_closure_substs(&mut self, - substs: &mut ClosureSubsts<'tcx>) { + substs: &mut ClosureSubsts<'tcx>, + _: Location) { *substs = self.tcx.erase_regions(substs); } diff --git a/src/librustc_passes/mir_stats.rs b/src/librustc_passes/mir_stats.rs index 4dd38cc515c..9895802700e 100644 --- a/src/librustc_passes/mir_stats.rs +++ b/src/librustc_passes/mir_stats.rs @@ -279,7 +279,8 @@ fn visit_source_info(&mut self, } fn visit_closure_substs(&mut self, - substs: &ClosureSubsts<'tcx>) { + substs: &ClosureSubsts<'tcx>, + _: Location) { self.record("ClosureSubsts", substs); self.super_closure_substs(substs); } From 4b9acad7c66c9e9cb8f585ff7446872eec5c33de Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Thu, 27 Jul 2017 19:43:05 -0400 Subject: [PATCH 2/2] Squash Span into SourceInfo and rename enum and its inner types --- src/librustc/mir/visit.rs | 31 +++++++++++---------- src/librustc_mir/build/mod.rs | 4 +-- src/librustc_mir/transform/erase_regions.rs | 4 +-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs index 2e7dfa8f70d..fd3a9f8cd2d 100644 --- a/src/librustc/mir/visit.rs +++ b/src/librustc/mir/visit.rs @@ -211,7 +211,7 @@ fn visit_source_info(&mut self, fn visit_ty(&mut self, ty: & $($mutability)* Ty<'tcx>, - _: PositionalInfo) { + _: Lookup) { self.super_ty(ty); } @@ -269,7 +269,11 @@ fn super_mir(&mut self, self.visit_visibility_scope_data(scope); } - self.visit_ty(&$($mutability)* mir.return_ty, PositionalInfo::Span(mir.span)); + let lookup = Lookup::Src(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); @@ -388,7 +392,7 @@ fn super_terminator_kind(&mut self, ref values, ref targets } => { self.visit_operand(discr, source_location); - self.visit_ty(switch_ty, PositionalInfo::Location(source_location)); + self.visit_ty(switch_ty, Lookup::Loc(source_location)); for value in &values[..] { self.visit_const_int(value, source_location); } @@ -492,7 +496,7 @@ fn super_rvalue(&mut self, ref $($mutability)* operand, ref $($mutability)* ty) => { self.visit_operand(operand, location); - self.visit_ty(ty, PositionalInfo::Location(location)); + self.visit_ty(ty, Lookup::Loc(location)); } Rvalue::BinaryOp(_bin_op, @@ -514,7 +518,7 @@ fn super_rvalue(&mut self, } Rvalue::NullaryOp(_op, ref $($mutability)* ty) => { - self.visit_ty(ty, PositionalInfo::Location(location)); + self.visit_ty(ty, Lookup::Loc(location)); } Rvalue::Aggregate(ref $($mutability)* kind, @@ -522,7 +526,7 @@ fn super_rvalue(&mut self, let kind = &$($mutability)* **kind; match *kind { AggregateKind::Array(ref $($mutability)* ty) => { - self.visit_ty(ty, PositionalInfo::Location(location)); + self.visit_ty(ty, Lookup::Loc(location)); } AggregateKind::Tuple => { } @@ -584,7 +588,7 @@ fn super_static(&mut self, ref $($mutability)* ty, } = *static_; self.visit_def_id(def_id, location); - self.visit_ty(ty, PositionalInfo::Location(location)); + self.visit_ty(ty, Lookup::Loc(location)); } fn super_projection(&mut self, @@ -614,7 +618,7 @@ fn super_projection_elem(&mut self, ProjectionElem::Subslice { from: _, to: _ } => { } ProjectionElem::Field(_field, ref $($mutability)* ty) => { - self.visit_ty(ty, PositionalInfo::Location(location)); + self.visit_ty(ty, Lookup::Loc(location)); } ProjectionElem::Index(ref $($mutability)* operand) => { self.visit_operand(operand, location); @@ -638,7 +642,7 @@ fn super_local_decl(&mut self, is_user_variable: _, } = *local_decl; - self.visit_ty(ty, PositionalInfo::SourceInfo(*source_info)); + self.visit_ty(ty, Lookup::Src(*source_info)); self.visit_source_info(source_info); } @@ -661,7 +665,7 @@ fn super_constant(&mut self, } = *constant; self.visit_span(span); - self.visit_ty(ty, PositionalInfo::Location(location)); + self.visit_ty(ty, Lookup::Loc(location)); self.visit_literal(literal, location); } @@ -737,10 +741,9 @@ fn visit_location(&mut self, mir: & $($mutability)* Mir<'tcx>, location: Locatio make_mir_visitor!(Visitor,); make_mir_visitor!(MutVisitor,mut); -pub enum PositionalInfo { - Location(Location), - SourceInfo(SourceInfo), - Span(Span), +pub enum Lookup { + Loc(Location), + Src(SourceInfo), } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs index abb431f70eb..74101abd5d7 100644 --- a/src/librustc_mir/build/mod.rs +++ b/src/librustc_mir/build/mod.rs @@ -17,7 +17,7 @@ use rustc::middle::region::CodeExtent; use rustc::mir::*; use rustc::mir::transform::MirSource; -use rustc::mir::visit::{MutVisitor, PositionalInfo}; +use rustc::mir::visit::{MutVisitor, Lookup}; use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::subst::Substs; use rustc::util::nodemap::NodeMap; @@ -143,7 +143,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>, _: PositionalInfo) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) { if let Some(lifted) = self.tcx.lift(ty) { *ty = lifted; } else { diff --git a/src/librustc_mir/transform/erase_regions.rs b/src/librustc_mir/transform/erase_regions.rs index 45a0fab7b62..da9032685e0 100644 --- a/src/librustc_mir/transform/erase_regions.rs +++ b/src/librustc_mir/transform/erase_regions.rs @@ -15,7 +15,7 @@ use rustc::ty::subst::Substs; use rustc::ty::{Ty, TyCtxt, ClosureSubsts}; use rustc::mir::*; -use rustc::mir::visit::{MutVisitor, PositionalInfo}; +use rustc::mir::visit::{MutVisitor, Lookup}; use rustc::mir::transform::{MirPass, MirSource}; struct EraseRegionsVisitor<'a, 'tcx: 'a> { @@ -31,7 +31,7 @@ pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self { } impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> { - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: PositionalInfo) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _: Lookup) { let old_ty = *ty; *ty = self.tcx.erase_regions(&old_ty); }