Skip to content

Commit e0ef0fc

Browse files
committed
Auto merge of rust-lang#78779 - LeSeulArtichaut:ty-visitor-return, r=oli-obk
Introduce `TypeVisitor::BreakTy` Implements MCP rust-lang/compiler-team#383. r? `@ghost` cc `@lcnr` `@oli-obk` ~~Blocked on FCP in rust-lang/compiler-team#383.~~
2 parents efcb3b3 + f6e6a15 commit e0ef0fc

File tree

32 files changed

+232
-213
lines changed

32 files changed

+232
-213
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14981498
}
14991499

15001500
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
1501-
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<()> {
1501+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
15021502
if let Some((kind, def_id)) = TyCategory::from_ty(t) {
15031503
let span = self.tcx.def_span(def_id);
15041504
// Avoid cluttering the output when the "found" and error span overlap:

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
474474
struct TraitObjectVisitor(Vec<DefId>);
475475

476476
impl TypeVisitor<'_> for TraitObjectVisitor {
477-
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<()> {
477+
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<Self::BreakTy> {
478478
match t.kind() {
479479
ty::Dynamic(preds, RegionKind::ReStatic) => {
480480
if let Some(def_id) = preds.principal_def_id() {

compiler/rustc_infer/src/infer/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! See the Book for more information.
2-
31
pub use self::freshen::TypeFreshener;
42
pub use self::LateBoundRegionConversionTime::*;
53
pub use self::RegionVariableOrigin::*;
@@ -1334,9 +1332,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13341332
where
13351333
T: TypeFoldable<'tcx>,
13361334
{
1337-
let mut r = resolve::UnresolvedTypeFinder::new(self);
1338-
value.visit_with(&mut r);
1339-
r.first_unresolved
1335+
value.visit_with(&mut resolve::UnresolvedTypeFinder::new(self)).break_value()
13401336
}
13411337

13421338
pub fn probe_const_var(

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -741,15 +741,18 @@ struct ScopeInstantiator<'me, 'tcx> {
741741
}
742742

743743
impl<'me, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'tcx> {
744-
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ControlFlow<()> {
744+
fn visit_binder<T: TypeFoldable<'tcx>>(
745+
&mut self,
746+
t: &ty::Binder<T>,
747+
) -> ControlFlow<Self::BreakTy> {
745748
self.target_index.shift_in(1);
746749
t.super_visit_with(self);
747750
self.target_index.shift_out(1);
748751

749752
ControlFlow::CONTINUE
750753
}
751754

752-
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<()> {
755+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
753756
let ScopeInstantiator { bound_region_scope, next_region, .. } = self;
754757

755758
match r {

compiler/rustc_infer/src/infer/resolve.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,17 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> {
111111
/// involve some hashing and so forth).
112112
pub struct UnresolvedTypeFinder<'a, 'tcx> {
113113
infcx: &'a InferCtxt<'a, 'tcx>,
114-
115-
/// Used to find the type parameter name and location for error reporting.
116-
pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>,
117114
}
118115

119116
impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
120117
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
121-
UnresolvedTypeFinder { infcx, first_unresolved: None }
118+
UnresolvedTypeFinder { infcx }
122119
}
123120
}
124121

125122
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
126-
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<()> {
123+
type BreakTy = (Ty<'tcx>, Option<Span>);
124+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
127125
let t = self.infcx.shallow_resolve(t);
128126
if t.has_infer_types() {
129127
if let ty::Infer(infer_ty) = *t.kind() {
@@ -144,8 +142,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
144142
} else {
145143
None
146144
};
147-
self.first_unresolved = Some((t, ty_var_span));
148-
ControlFlow::BREAK
145+
ControlFlow::Break((t, ty_var_span))
149146
} else {
150147
// Otherwise, visit its contents.
151148
t.super_visit_with(self)

compiler/rustc_infer/src/traits/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx
6969
}
7070
}
7171

72-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
72+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
7373
self.predicate.visit_with(visitor)
7474
}
7575
}

compiler/rustc_lint/src/types.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1131,16 +1131,14 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11311131
fn check_for_opaque_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {
11321132
struct ProhibitOpaqueTypes<'a, 'tcx> {
11331133
cx: &'a LateContext<'tcx>,
1134-
ty: Option<Ty<'tcx>>,
11351134
};
11361135

11371136
impl<'a, 'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueTypes<'a, 'tcx> {
1138-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<()> {
1137+
type BreakTy = Ty<'tcx>;
1138+
1139+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
11391140
match ty.kind() {
1140-
ty::Opaque(..) => {
1141-
self.ty = Some(ty);
1142-
ControlFlow::BREAK
1143-
}
1141+
ty::Opaque(..) => ControlFlow::Break(ty),
11441142
// Consider opaque types within projections FFI-safe if they do not normalize
11451143
// to more opaque types.
11461144
ty::Projection(..) => {
@@ -1159,9 +1157,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11591157
}
11601158
}
11611159

1162-
let mut visitor = ProhibitOpaqueTypes { cx: self.cx, ty: None };
1163-
ty.visit_with(&mut visitor);
1164-
if let Some(ty) = visitor.ty {
1160+
if let Some(ty) = ty.visit_with(&mut ProhibitOpaqueTypes { cx: self.cx }).break_value() {
11651161
self.emit_ffi_unsafe_type_lint(ty, sp, "opaque types have no C equivalent", None);
11661162
true
11671163
} else {

compiler/rustc_macros/src/type_foldable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
3535
fn super_visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>(
3636
&self,
3737
__folder: &mut __F
38-
) -> ::std::ops::ControlFlow<()> {
38+
) -> ::std::ops::ControlFlow<__F::BreakTy> {
3939
match *self { #body_visit }
4040
::std::ops::ControlFlow::CONTINUE
4141
}

compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#![feature(half_open_range_patterns)]
5252
#![feature(exclusive_range_pattern)]
5353
#![feature(control_flow_enum)]
54+
#![feature(associated_type_defaults)]
5455
#![recursion_limit = "512"]
5556

5657
#[macro_use]

compiler/rustc_middle/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ macro_rules! TrivialTypeFoldableImpls {
6262
fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
6363
&self,
6464
_: &mut F)
65-
-> ::std::ops::ControlFlow<()>
65+
-> ::std::ops::ControlFlow<F::BreakTy>
6666
{
6767
::std::ops::ControlFlow::CONTINUE
6868
}
@@ -105,7 +105,7 @@ macro_rules! EnumTypeFoldableImpl {
105105
fn super_visit_with<V: $crate::ty::fold::TypeVisitor<$tcx>>(
106106
&self,
107107
visitor: &mut V,
108-
) -> ::std::ops::ControlFlow<()> {
108+
) -> ::std::ops::ControlFlow<V::BreakTy> {
109109
EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
110110
}
111111
}

compiler/rustc_middle/src/mir/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,10 @@ impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
24642464
}
24652465
}
24662466

2467-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<()> {
2467+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(
2468+
&self,
2469+
visitor: &mut Vs,
2470+
) -> ControlFlow<Vs::BreakTy> {
24682471
self.base.visit_with(visitor)
24692472
// Note: there's nothing in `self.proj` to visit.
24702473
}

compiler/rustc_middle/src/mir/type_foldable.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
8787
Terminator { source_info: self.source_info, kind }
8888
}
8989

90-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
90+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
9191
use crate::mir::TerminatorKind::*;
9292

9393
match self.kind {
@@ -144,7 +144,7 @@ impl<'tcx> TypeFoldable<'tcx> for GeneratorKind {
144144
self
145145
}
146146

147-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
147+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
148148
ControlFlow::CONTINUE
149149
}
150150
}
@@ -154,7 +154,7 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
154154
Place { local: self.local.fold_with(folder), projection: self.projection.fold_with(folder) }
155155
}
156156

157-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
157+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
158158
self.local.visit_with(visitor)?;
159159
self.projection.visit_with(visitor)
160160
}
@@ -165,7 +165,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
165165
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_place_elems(v))
166166
}
167167

168-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
168+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
169169
self.iter().try_for_each(|t| t.visit_with(visitor))
170170
}
171171
}
@@ -211,7 +211,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
211211
}
212212
}
213213

214-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
214+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
215215
use crate::mir::Rvalue::*;
216216
match *self {
217217
Use(ref op) => op.visit_with(visitor),
@@ -266,7 +266,7 @@ impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
266266
}
267267
}
268268

269-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
269+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
270270
match *self {
271271
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
272272
Operand::Constant(ref c) => c.visit_with(visitor),
@@ -290,7 +290,10 @@ impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
290290
}
291291
}
292292

293-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<()> {
293+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(
294+
&self,
295+
visitor: &mut Vs,
296+
) -> ControlFlow<Vs::BreakTy> {
294297
use crate::mir::ProjectionElem::*;
295298

296299
match self {
@@ -305,7 +308,7 @@ impl<'tcx> TypeFoldable<'tcx> for Field {
305308
fn super_fold_with<F: TypeFolder<'tcx>>(self, _: &mut F) -> Self {
306309
self
307310
}
308-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
311+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
309312
ControlFlow::CONTINUE
310313
}
311314
}
@@ -314,7 +317,7 @@ impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
314317
fn super_fold_with<F: TypeFolder<'tcx>>(self, _: &mut F) -> Self {
315318
self
316319
}
317-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
320+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
318321
ControlFlow::CONTINUE
319322
}
320323
}
@@ -323,7 +326,7 @@ impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
323326
fn super_fold_with<F: TypeFolder<'tcx>>(self, _: &mut F) -> Self {
324327
self
325328
}
326-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
329+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
327330
ControlFlow::CONTINUE
328331
}
329332
}
@@ -336,7 +339,7 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
336339
literal: self.literal.fold_with(folder),
337340
}
338341
}
339-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
342+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
340343
self.literal.visit_with(visitor)
341344
}
342345
}

0 commit comments

Comments
 (0)