rename Lookup to TyContext and pass more info when visiting tys

This commit is contained in:
Niko Matsakis 2017-10-26 19:53:31 -04:00
parent 2379faa933
commit 81f6ce5ce3
8 changed files with 61 additions and 44 deletions

View File

@ -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)]

View File

@ -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 {

View File

@ -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 {

View File

@ -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);
}

View File

@ -60,7 +60,6 @@ impl RegionInferenceContext {
}
}
/// Returns an iterator over all the region indices.
pub fn regions(&self) -> impl Iterator<Item = RegionIndex> {
self.definitions.indices()

View File

@ -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<RegionVid, Lookup>,
lookup_map: HashMap<RegionVid, TyContext>,
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);
}
}

View File

@ -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);
}

View File

@ -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,