Skip to content

Commit f66c06f

Browse files
committed
Explicit TypeVisitable implementations
1 parent 9ffdc2d commit f66c06f

File tree

7 files changed

+269
-152
lines changed

7 files changed

+269
-152
lines changed

compiler/rustc_infer/src/traits/structural_impls.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::traits;
22
use crate::traits::project::Normalized;
33
use rustc_middle::ty;
44
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
5+
use rustc_middle::ty::visit::TypeVisitable;
56

67
use std::fmt;
78
use std::ops::ControlFlow;
@@ -68,7 +69,9 @@ impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx
6869
param_env: self.param_env.try_fold_with(folder)?,
6970
})
7071
}
72+
}
7173

74+
impl<'tcx, O: TypeVisitable<'tcx>> TypeVisitable<'tcx> for traits::Obligation<'tcx, O> {
7275
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
7376
self.predicate.visit_with(visitor)?;
7477
self.param_env.visit_with(visitor)

compiler/rustc_middle/src/mir/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::ty::codec::{TyDecoder, TyEncoder};
1111
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable, TypeVisitor};
1212
use crate::ty::print::{FmtPrinter, Printer};
1313
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
14+
use crate::ty::visit::TypeVisitable;
1415
use crate::ty::{self, List, Ty, TyCtxt};
1516
use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex};
1617

@@ -68,6 +69,7 @@ pub use terminator::*;
6869

6970
pub mod traversal;
7071
mod type_foldable;
72+
mod type_visitable;
7173
pub mod visit;
7274

7375
pub use self::generic_graph::graphviz_safe_def_name;
@@ -2650,7 +2652,9 @@ impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
26502652
projs: self.projs.try_fold_with(folder)?,
26512653
})
26522654
}
2655+
}
26532656

2657+
impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection {
26542658
fn visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<Vs::BreakTy> {
26552659
self.base.visit_with(visitor)
26562660
// Note: there's nothing in `self.proj` to visit.

compiler/rustc_middle/src/mir/type_foldable.rs

-152
Original file line numberDiff line numberDiff line change
@@ -89,65 +89,12 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
8989
};
9090
Ok(Terminator { source_info: self.source_info, kind })
9191
}
92-
93-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
94-
use crate::mir::TerminatorKind::*;
95-
96-
match self.kind {
97-
SwitchInt { ref discr, switch_ty, .. } => {
98-
discr.visit_with(visitor)?;
99-
switch_ty.visit_with(visitor)
100-
}
101-
Drop { ref place, .. } => place.visit_with(visitor),
102-
DropAndReplace { ref place, ref value, .. } => {
103-
place.visit_with(visitor)?;
104-
value.visit_with(visitor)
105-
}
106-
Yield { ref value, .. } => value.visit_with(visitor),
107-
Call { ref func, ref args, ref destination, .. } => {
108-
destination.visit_with(visitor)?;
109-
func.visit_with(visitor)?;
110-
args.visit_with(visitor)
111-
}
112-
Assert { ref cond, ref msg, .. } => {
113-
cond.visit_with(visitor)?;
114-
use AssertKind::*;
115-
match msg {
116-
BoundsCheck { ref len, ref index } => {
117-
len.visit_with(visitor)?;
118-
index.visit_with(visitor)
119-
}
120-
Overflow(_, l, r) => {
121-
l.visit_with(visitor)?;
122-
r.visit_with(visitor)
123-
}
124-
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
125-
op.visit_with(visitor)
126-
}
127-
ResumedAfterReturn(_) | ResumedAfterPanic(_) => ControlFlow::CONTINUE,
128-
}
129-
}
130-
InlineAsm { ref operands, .. } => operands.visit_with(visitor),
131-
Goto { .. }
132-
| Resume
133-
| Abort
134-
| Return
135-
| GeneratorDrop
136-
| Unreachable
137-
| FalseEdge { .. }
138-
| FalseUnwind { .. } => ControlFlow::CONTINUE,
139-
}
140-
}
14192
}
14293

14394
impl<'tcx> TypeFoldable<'tcx> for GeneratorKind {
14495
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
14596
Ok(self)
14697
}
147-
148-
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
149-
ControlFlow::CONTINUE
150-
}
15198
}
15299

153100
impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
@@ -157,21 +104,12 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
157104
projection: self.projection.try_fold_with(folder)?,
158105
})
159106
}
160-
161-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
162-
self.local.visit_with(visitor)?;
163-
self.projection.visit_with(visitor)
164-
}
165107
}
166108

167109
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
168110
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
169111
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_place_elems(v))
170112
}
171-
172-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
173-
self.iter().try_for_each(|t| t.visit_with(visitor))
174-
}
175113
}
176114

177115
impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
@@ -224,55 +162,6 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
224162
}
225163
})
226164
}
227-
228-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
229-
use crate::mir::Rvalue::*;
230-
match *self {
231-
Use(ref op) => op.visit_with(visitor),
232-
Repeat(ref op, _) => op.visit_with(visitor),
233-
ThreadLocalRef(did) => did.visit_with(visitor),
234-
Ref(region, _, ref place) => {
235-
region.visit_with(visitor)?;
236-
place.visit_with(visitor)
237-
}
238-
AddressOf(_, ref place) => place.visit_with(visitor),
239-
Len(ref place) => place.visit_with(visitor),
240-
Cast(_, ref op, ty) => {
241-
op.visit_with(visitor)?;
242-
ty.visit_with(visitor)
243-
}
244-
BinaryOp(_, box (ref rhs, ref lhs)) | CheckedBinaryOp(_, box (ref rhs, ref lhs)) => {
245-
rhs.visit_with(visitor)?;
246-
lhs.visit_with(visitor)
247-
}
248-
UnaryOp(_, ref val) => val.visit_with(visitor),
249-
Discriminant(ref place) => place.visit_with(visitor),
250-
NullaryOp(_, ty) => ty.visit_with(visitor),
251-
Aggregate(ref kind, ref fields) => {
252-
match **kind {
253-
AggregateKind::Array(ty) => {
254-
ty.visit_with(visitor)?;
255-
}
256-
AggregateKind::Tuple => {}
257-
AggregateKind::Adt(_, _, substs, user_ty, _) => {
258-
substs.visit_with(visitor)?;
259-
user_ty.visit_with(visitor)?;
260-
}
261-
AggregateKind::Closure(_, substs) => {
262-
substs.visit_with(visitor)?;
263-
}
264-
AggregateKind::Generator(_, substs, _) => {
265-
substs.visit_with(visitor)?;
266-
}
267-
}
268-
fields.visit_with(visitor)
269-
}
270-
ShallowInitBox(ref op, ty) => {
271-
op.visit_with(visitor)?;
272-
ty.visit_with(visitor)
273-
}
274-
}
275-
}
276165
}
277166

278167
impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
@@ -283,13 +172,6 @@ impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
283172
Operand::Constant(c) => Operand::Constant(c.try_fold_with(folder)?),
284173
})
285174
}
286-
287-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
288-
match *self {
289-
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
290-
Operand::Constant(ref c) => c.visit_with(visitor),
291-
}
292-
}
293175
}
294176

295177
impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
@@ -307,43 +189,24 @@ impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
307189
Subslice { from, to, from_end } => Subslice { from, to, from_end },
308190
})
309191
}
310-
311-
fn visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<Vs::BreakTy> {
312-
use crate::mir::ProjectionElem::*;
313-
314-
match self {
315-
Field(_, ty) => ty.visit_with(visitor),
316-
Index(v) => v.visit_with(visitor),
317-
_ => ControlFlow::CONTINUE,
318-
}
319-
}
320192
}
321193

322194
impl<'tcx> TypeFoldable<'tcx> for Field {
323195
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
324196
Ok(self)
325197
}
326-
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
327-
ControlFlow::CONTINUE
328-
}
329198
}
330199

331200
impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
332201
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
333202
Ok(self)
334203
}
335-
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
336-
ControlFlow::CONTINUE
337-
}
338204
}
339205

340206
impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
341207
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
342208
Ok(self)
343209
}
344-
fn visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
345-
ControlFlow::CONTINUE
346-
}
347210
}
348211

349212
impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
@@ -354,21 +217,13 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
354217
literal: self.literal.try_fold_with(folder)?,
355218
})
356219
}
357-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
358-
self.literal.visit_with(visitor)?;
359-
self.user_ty.visit_with(visitor)
360-
}
361220
}
362221

363222
impl<'tcx> TypeFoldable<'tcx> for ConstantKind<'tcx> {
364223
#[inline(always)]
365224
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
366225
folder.try_fold_mir_const(self)
367226
}
368-
369-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
370-
visitor.visit_mir_const(*self)
371-
}
372227
}
373228

374229
impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> {
@@ -381,11 +236,4 @@ impl<'tcx> TypeSuperFoldable<'tcx> for ConstantKind<'tcx> {
381236
ConstantKind::Val(v, t) => Ok(ConstantKind::Val(v, t.try_fold_with(folder)?)),
382237
}
383238
}
384-
385-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
386-
match *self {
387-
ConstantKind::Ty(c) => c.visit_with(visitor),
388-
ConstantKind::Val(_, t) => t.visit_with(visitor),
389-
}
390-
}
391239
}

0 commit comments

Comments
 (0)