Explicit TypeVisitable implementations
This commit is contained in:
@@ -11,6 +11,7 @@ use crate::ty::codec::{TyDecoder, TyEncoder};
|
||||
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable, TypeVisitor};
|
||||
use crate::ty::print::{FmtPrinter, Printer};
|
||||
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
|
||||
use crate::ty::visit::TypeVisitable;
|
||||
use crate::ty::{self, List, Ty, TyCtxt};
|
||||
use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex};
|
||||
|
||||
@@ -68,6 +69,7 @@ pub use terminator::*;
|
||||
|
||||
pub mod traversal;
|
||||
mod type_foldable;
|
||||
mod type_visitable;
|
||||
pub mod visit;
|
||||
|
||||
pub use self::generic_graph::graphviz_safe_def_name;
|
||||
@@ -2650,7 +2652,9 @@ impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
|
||||
projs: self.projs.try_fold_with(folder)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection {
|
||||
fn visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<Vs::BreakTy> {
|
||||
self.base.visit_with(visitor)
|
||||
// Note: there's nothing in `self.proj` to visit.
|
||||
|
||||
@@ -89,65 +89,12 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
|
||||
};
|
||||
Ok(Terminator { source_info: self.source_info, kind })
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
use crate::mir::TerminatorKind::*;
|
||||
|
||||
match self.kind {
|
||||
SwitchInt { ref discr, switch_ty, .. } => {
|
||||
discr.visit_with(visitor)?;
|
||||
switch_ty.visit_with(visitor)
|
||||
}
|
||||
Drop { ref place, .. } => place.visit_with(visitor),
|
||||
DropAndReplace { ref place, ref value, .. } => {
|
||||
place.visit_with(visitor)?;
|
||||
value.visit_with(visitor)
|
||||
}
|
||||
Yield { ref value, .. } => value.visit_with(visitor),
|
||||
Call { ref func, ref args, ref destination, .. } => {
|
||||
destination.visit_with(visitor)?;
|
||||
func.visit_with(visitor)?;
|
||||
args.visit_with(visitor)
|
||||
}
|
||||
Assert { ref cond, ref msg, .. } => {
|
||||
cond.visit_with(visitor)?;
|
||||
use AssertKind::*;
|
||||
match msg {
|
||||
BoundsCheck { ref len, ref index } => {
|
||||
len.visit_with(visitor)?;
|
||||
index.visit_with(visitor)
|
||||
}
|
||||
Overflow(_, l, r) => {
|
||||
l.visit_with(visitor)?;
|
||||
r.visit_with(visitor)
|
||||
}
|
||||
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
|
||||
op.visit_with(visitor)
|
||||
}
|
||||
ResumedAfterReturn(_) | ResumedAfterPanic(_) => ControlFlow::CONTINUE,
|
||||
}
|
||||
}
|
||||
InlineAsm { ref operands, .. } => operands.visit_with(visitor),
|
||||
Goto { .. }
|
||||
| Resume
|
||||
| Abort
|
||||
| Return
|
||||
| GeneratorDrop
|
||||
| Unreachable
|
||||
| FalseEdge { .. }
|
||||
| FalseUnwind { .. } => ControlFlow::CONTINUE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for GeneratorKind {
|
||||
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
|
||||
@@ -157,21 +104,12 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
|
||||
projection: self.projection.try_fold_with(folder)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
self.local.visit_with(visitor)?;
|
||||
self.projection.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
|
||||
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
|
||||
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_place_elems(v))
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
self.iter().try_for_each(|t| t.visit_with(visitor))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
|
||||
@@ -224,55 +162,6 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
use crate::mir::Rvalue::*;
|
||||
match *self {
|
||||
Use(ref op) => op.visit_with(visitor),
|
||||
Repeat(ref op, _) => op.visit_with(visitor),
|
||||
ThreadLocalRef(did) => did.visit_with(visitor),
|
||||
Ref(region, _, ref place) => {
|
||||
region.visit_with(visitor)?;
|
||||
place.visit_with(visitor)
|
||||
}
|
||||
AddressOf(_, ref place) => place.visit_with(visitor),
|
||||
Len(ref place) => place.visit_with(visitor),
|
||||
Cast(_, ref op, ty) => {
|
||||
op.visit_with(visitor)?;
|
||||
ty.visit_with(visitor)
|
||||
}
|
||||
BinaryOp(_, box (ref rhs, ref lhs)) | CheckedBinaryOp(_, box (ref rhs, ref lhs)) => {
|
||||
rhs.visit_with(visitor)?;
|
||||
lhs.visit_with(visitor)
|
||||
}
|
||||
UnaryOp(_, ref val) => val.visit_with(visitor),
|
||||
Discriminant(ref place) => place.visit_with(visitor),
|
||||
NullaryOp(_, ty) => ty.visit_with(visitor),
|
||||
Aggregate(ref kind, ref fields) => {
|
||||
match **kind {
|
||||
AggregateKind::Array(ty) => {
|
||||
ty.visit_with(visitor)?;
|
||||
}
|
||||
AggregateKind::Tuple => {}
|
||||
AggregateKind::Adt(_, _, substs, user_ty, _) => {
|
||||
substs.visit_with(visitor)?;
|
||||
user_ty.visit_with(visitor)?;
|
||||
}
|
||||
AggregateKind::Closure(_, substs) => {
|
||||
substs.visit_with(visitor)?;
|
||||
}
|
||||
AggregateKind::Generator(_, substs, _) => {
|
||||
substs.visit_with(visitor)?;
|
||||
}
|
||||
}
|
||||
fields.visit_with(visitor)
|
||||
}
|
||||
ShallowInitBox(ref op, ty) => {
|
||||
op.visit_with(visitor)?;
|
||||
ty.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
|
||||
@@ -283,13 +172,6 @@ impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
|
||||
Operand::Constant(c) => Operand::Constant(c.try_fold_with(folder)?),
|
||||
})
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
match *self {
|
||||
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
|
||||
Operand::Constant(ref c) => c.visit_with(visitor),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
|
||||
@@ -307,43 +189,24 @@ impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
|
||||
Subslice { from, to, from_end } => Subslice { from, to, from_end },
|
||||
})
|
||||
}
|
||||
|
||||
fn visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<Vs::BreakTy> {
|
||||
use crate::mir::ProjectionElem::*;
|
||||
|
||||
match self {
|
||||
Field(_, ty) => ty.visit_with(visitor),
|
||||
Index(v) => v.visit_with(visitor),
|
||||
_ => ControlFlow::CONTINUE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for Field {
|
||||
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
|
||||
Ok(self)
|
||||
}
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
|
||||
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
|
||||
Ok(self)
|
||||
}
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
|
||||
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
|
||||
Ok(self)
|
||||
}
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
|
||||
@@ -354,10 +217,6 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
|
||||
literal: self.literal.try_fold_with(folder)?,
|
||||
})
|
||||
}
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
self.literal.visit_with(visitor)?;
|
||||
self.user_ty.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFoldable<'tcx> for ConstantKind<'tcx> {
|
||||
@@ -365,10 +224,6 @@ impl<'tcx> TypeFoldable<'tcx> for ConstantKind<'tcx> {
|
||||
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
|
||||
folder.try_fold_mir_const(self)
|
||||
}
|
||||
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
visitor.visit_mir_const(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> {
|
||||
@@ -381,11 +236,4 @@ impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> {
|
||||
ConstantKind::Val(v, t) => Ok(ConstantKind::Val(v, t.try_fold_with(folder)?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
match *self {
|
||||
ConstantKind::Ty(c) => c.visit_with(visitor),
|
||||
ConstantKind::Val(_, t) => t.visit_with(visitor),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
186
compiler/rustc_middle/src/mir/type_visitable.rs
Normal file
186
compiler/rustc_middle/src/mir/type_visitable.rs
Normal file
@@ -0,0 +1,186 @@
|
||||
//! `TypeVisitable` implementations for MIR types
|
||||
|
||||
use super::*;
|
||||
use crate::ty;
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for Terminator<'tcx> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
use crate::mir::TerminatorKind::*;
|
||||
|
||||
match self.kind {
|
||||
SwitchInt { ref discr, switch_ty, .. } => {
|
||||
discr.visit_with(visitor)?;
|
||||
switch_ty.visit_with(visitor)
|
||||
}
|
||||
Drop { ref place, .. } => place.visit_with(visitor),
|
||||
DropAndReplace { ref place, ref value, .. } => {
|
||||
place.visit_with(visitor)?;
|
||||
value.visit_with(visitor)
|
||||
}
|
||||
Yield { ref value, .. } => value.visit_with(visitor),
|
||||
Call { ref func, ref args, ref destination, .. } => {
|
||||
destination.visit_with(visitor)?;
|
||||
func.visit_with(visitor)?;
|
||||
args.visit_with(visitor)
|
||||
}
|
||||
Assert { ref cond, ref msg, .. } => {
|
||||
cond.visit_with(visitor)?;
|
||||
use AssertKind::*;
|
||||
match msg {
|
||||
BoundsCheck { ref len, ref index } => {
|
||||
len.visit_with(visitor)?;
|
||||
index.visit_with(visitor)
|
||||
}
|
||||
Overflow(_, l, r) => {
|
||||
l.visit_with(visitor)?;
|
||||
r.visit_with(visitor)
|
||||
}
|
||||
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
|
||||
op.visit_with(visitor)
|
||||
}
|
||||
ResumedAfterReturn(_) | ResumedAfterPanic(_) => ControlFlow::CONTINUE,
|
||||
}
|
||||
}
|
||||
InlineAsm { ref operands, .. } => operands.visit_with(visitor),
|
||||
Goto { .. }
|
||||
| Resume
|
||||
| Abort
|
||||
| Return
|
||||
| GeneratorDrop
|
||||
| Unreachable
|
||||
| FalseEdge { .. }
|
||||
| FalseUnwind { .. } => ControlFlow::CONTINUE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for GeneratorKind {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for Place<'tcx> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
self.local.visit_with(visitor)?;
|
||||
self.projection.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
self.iter().try_for_each(|t| t.visit_with(visitor))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for Rvalue<'tcx> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
use crate::mir::Rvalue::*;
|
||||
match *self {
|
||||
Use(ref op) => op.visit_with(visitor),
|
||||
Repeat(ref op, _) => op.visit_with(visitor),
|
||||
ThreadLocalRef(did) => did.visit_with(visitor),
|
||||
Ref(region, _, ref place) => {
|
||||
region.visit_with(visitor)?;
|
||||
place.visit_with(visitor)
|
||||
}
|
||||
AddressOf(_, ref place) => place.visit_with(visitor),
|
||||
Len(ref place) => place.visit_with(visitor),
|
||||
Cast(_, ref op, ty) => {
|
||||
op.visit_with(visitor)?;
|
||||
ty.visit_with(visitor)
|
||||
}
|
||||
BinaryOp(_, box (ref rhs, ref lhs)) | CheckedBinaryOp(_, box (ref rhs, ref lhs)) => {
|
||||
rhs.visit_with(visitor)?;
|
||||
lhs.visit_with(visitor)
|
||||
}
|
||||
UnaryOp(_, ref val) => val.visit_with(visitor),
|
||||
Discriminant(ref place) => place.visit_with(visitor),
|
||||
NullaryOp(_, ty) => ty.visit_with(visitor),
|
||||
Aggregate(ref kind, ref fields) => {
|
||||
match **kind {
|
||||
AggregateKind::Array(ty) => {
|
||||
ty.visit_with(visitor)?;
|
||||
}
|
||||
AggregateKind::Tuple => {}
|
||||
AggregateKind::Adt(_, _, substs, user_ty, _) => {
|
||||
substs.visit_with(visitor)?;
|
||||
user_ty.visit_with(visitor)?;
|
||||
}
|
||||
AggregateKind::Closure(_, substs) => {
|
||||
substs.visit_with(visitor)?;
|
||||
}
|
||||
AggregateKind::Generator(_, substs, _) => {
|
||||
substs.visit_with(visitor)?;
|
||||
}
|
||||
}
|
||||
fields.visit_with(visitor)
|
||||
}
|
||||
ShallowInitBox(ref op, ty) => {
|
||||
op.visit_with(visitor)?;
|
||||
ty.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for Operand<'tcx> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
match *self {
|
||||
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
|
||||
Operand::Constant(ref c) => c.visit_with(visitor),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for PlaceElem<'tcx> {
|
||||
fn visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<Vs::BreakTy> {
|
||||
use crate::mir::ProjectionElem::*;
|
||||
|
||||
match self {
|
||||
Field(_, ty) => ty.visit_with(visitor),
|
||||
Index(v) => v.visit_with(visitor),
|
||||
_ => ControlFlow::CONTINUE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for Field {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for GeneratorSavedLocal {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, R: Idx, C: Idx> TypeVisitable<'tcx> for BitMatrix<R, C> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
ControlFlow::CONTINUE
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for Constant<'tcx> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
self.literal.visit_with(visitor)?;
|
||||
self.user_ty.visit_with(visitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitable<'tcx> for ConstantKind<'tcx> {
|
||||
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
visitor.visit_mir_const(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> TypeSuperVisitable<'tcx> for ConstantKind<'tcx> {
|
||||
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
|
||||
match *self {
|
||||
ConstantKind::Ty(c) => c.visit_with(visitor),
|
||||
ConstantKind::Val(_, t) => t.visit_with(visitor),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user