Skip to content

Commit 975b50d

Browse files
authored
Rollup merge of #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 0ad71f3 + f6e6a15 commit 975b50d

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ macro_rules! CloneTypeFoldableImpls {
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,10 @@ impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
24772477
UserTypeProjection { base, projs }
24782478
}
24792479

2480-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<()> {
2480+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(
2481+
&self,
2482+
visitor: &mut Vs,
2483+
) -> ControlFlow<Vs::BreakTy> {
24812484
self.base.visit_with(visitor)
24822485
// Note: there's nothing in `self.proj` to visit.
24832486
}

compiler/rustc_middle/src/mir/type_foldable.rs

Lines changed: 14 additions & 11 deletions
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
}
@@ -166,7 +166,7 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
166166
folder.tcx().intern_place_elems(&v)
167167
}
168168

169-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
169+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
170170
self.iter().try_for_each(|t| t.visit_with(visitor))
171171
}
172172
}
@@ -216,7 +216,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
216216
}
217217
}
218218

219-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
219+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
220220
use crate::mir::Rvalue::*;
221221
match *self {
222222
Use(ref op) => op.visit_with(visitor),
@@ -271,7 +271,7 @@ impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
271271
}
272272
}
273273

274-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
274+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
275275
match *self {
276276
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
277277
Operand::Constant(ref c) => c.visit_with(visitor),
@@ -295,7 +295,10 @@ impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
295295
}
296296
}
297297

298-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<()> {
298+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(
299+
&self,
300+
visitor: &mut Vs,
301+
) -> ControlFlow<Vs::BreakTy> {
299302
use crate::mir::ProjectionElem::*;
300303

301304
match self {
@@ -310,7 +313,7 @@ impl<'tcx> TypeFoldable<'tcx> for Field {
310313
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
311314
*self
312315
}
313-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
316+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
314317
ControlFlow::CONTINUE
315318
}
316319
}
@@ -319,7 +322,7 @@ impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
319322
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
320323
*self
321324
}
322-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
325+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
323326
ControlFlow::CONTINUE
324327
}
325328
}
@@ -328,7 +331,7 @@ impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
328331
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
329332
self.clone()
330333
}
331-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<()> {
334+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
332335
ControlFlow::CONTINUE
333336
}
334337
}
@@ -341,7 +344,7 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
341344
literal: self.literal.fold_with(folder),
342345
}
343346
}
344-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<()> {
347+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
345348
self.literal.visit_with(visitor)
346349
}
347350
}

0 commit comments

Comments
 (0)