Skip to content

Commit 975b50d

Browse files
authored
Rollup 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 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
}

0 commit comments

Comments
 (0)