Skip to content

Commit 4fe735b

Browse files
TypeVisitor: use ControlFlow in rustc_{infer,lint,trait_selection}
1 parent 2c85b6f commit 4fe735b

File tree

13 files changed

+106
-82
lines changed

13 files changed

+106
-82
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use rustc_middle::ty::{
7171
};
7272
use rustc_span::{BytePos, DesugaringKind, Pos, Span};
7373
use rustc_target::spec::abi;
74+
use std::ops::ControlFlow;
7475
use std::{cmp, fmt};
7576

7677
mod note;
@@ -1497,7 +1498,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14971498
}
14981499

14991500
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
1500-
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
1501+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<(), ()> {
15011502
if let Some((kind, def_id)) = TyCategory::from_ty(t) {
15021503
let span = self.tcx.def_span(def_id);
15031504
// 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use rustc_middle::ty::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, T
1515
use rustc_span::symbol::Ident;
1616
use rustc_span::{MultiSpan, Span};
1717

18+
use std::ops::ControlFlow;
19+
1820
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
1921
/// Print the error message for lifetime errors when the return type is a static `impl Trait`,
2022
/// `dyn Trait` or if a method call on a trait object introduces a static requirement.
@@ -472,13 +474,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
472474
struct TraitObjectVisitor(Vec<DefId>);
473475

474476
impl TypeVisitor<'_> for TraitObjectVisitor {
475-
fn visit_ty(&mut self, t: Ty<'_>) -> bool {
477+
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<(), ()> {
476478
match t.kind() {
477479
ty::Dynamic(preds, RegionKind::ReStatic) => {
478480
if let Some(def_id) = preds.principal_def_id() {
479481
self.0.push(def_id);
480482
}
481-
false
483+
ControlFlow::CONTINUE
482484
}
483485
_ => t.super_visit_with(self),
484486
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_middle::ty::fold::{TypeFoldable, TypeVisitor};
3030
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
3131
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt};
3232
use std::fmt::Debug;
33+
use std::ops::ControlFlow;
3334

3435
#[derive(PartialEq)]
3536
pub enum NormalizationStrategy {
@@ -740,15 +741,15 @@ struct ScopeInstantiator<'me, 'tcx> {
740741
}
741742

742743
impl<'me, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'tcx> {
743-
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> bool {
744+
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ControlFlow<(), ()> {
744745
self.target_index.shift_in(1);
745746
t.super_visit_with(self);
746747
self.target_index.shift_out(1);
747748

748-
false
749+
ControlFlow::CONTINUE
749750
}
750751

751-
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
752+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<(), ()> {
752753
let ScopeInstantiator { bound_region_scope, next_region, .. } = self;
753754

754755
match r {
@@ -759,7 +760,7 @@ impl<'me, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'me, 'tcx> {
759760
_ => {}
760761
}
761762

762-
false
763+
ControlFlow::CONTINUE
763764
}
764765
}
765766

compiler/rustc_infer/src/infer/resolve.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use super::{FixupError, FixupResult, InferCtxt, Span};
33
use rustc_middle::ty::fold::{TypeFolder, TypeVisitor};
44
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
55

6+
use std::ops::ControlFlow;
7+
68
///////////////////////////////////////////////////////////////////////////
79
// OPPORTUNISTIC VAR RESOLVER
810

@@ -121,7 +123,7 @@ impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
121123
}
122124

123125
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
124-
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
126+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<(), ()> {
125127
let t = self.infcx.shallow_resolve(t);
126128
if t.has_infer_types() {
127129
if let ty::Infer(infer_ty) = *t.kind() {
@@ -143,15 +145,15 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
143145
None
144146
};
145147
self.first_unresolved = Some((t, ty_var_span));
146-
true // Halt visiting.
148+
ControlFlow::BREAK
147149
} else {
148150
// Otherwise, visit its contents.
149151
t.super_visit_with(self)
150152
}
151153
} else {
152154
// All type variables in inference types must already be resolved,
153155
// - no need to visit the contents, continue visiting.
154-
false
156+
ControlFlow::CONTINUE
155157
}
156158
}
157159
}

compiler/rustc_infer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#![feature(never_type)]
2323
#![feature(or_patterns)]
2424
#![feature(in_band_lifetimes)]
25+
#![feature(control_flow_enum)]
2526
#![recursion_limit = "512"] // For rustdoc
2627

2728
#[macro_use]

compiler/rustc_infer/src/traits/structural_impls.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_middle::ty;
44
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
55

66
use std::fmt;
7+
use std::ops::ControlFlow;
78

89
// Structural impls for the structs in `traits`.
910

@@ -68,7 +69,7 @@ impl<'tcx, O: TypeFoldable<'tcx>> TypeFoldable<'tcx> for traits::Obligation<'tcx
6869
}
6970
}
7071

71-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
72+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
7273
self.predicate.visit_with(visitor)
7374
}
7475
}

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#![feature(or_patterns)]
3838
#![feature(half_open_range_patterns)]
3939
#![feature(exclusive_range_pattern)]
40+
#![feature(control_flow_enum)]
4041
#![recursion_limit = "256"]
4142

4243
#[macro_use]

compiler/rustc_lint/src/types.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_target::abi::{Integer, LayoutOf, TagEncoding, VariantIdx, Variants};
1818
use rustc_target::spec::abi::Abi as SpecAbi;
1919

2020
use std::cmp;
21+
use std::ops::ControlFlow;
2122
use tracing::debug;
2223

2324
declare_lint! {
@@ -1135,11 +1136,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11351136
};
11361137

11371138
impl<'a, 'tcx> ty::fold::TypeVisitor<'tcx> for ProhibitOpaqueTypes<'a, 'tcx> {
1138-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
1139+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
11391140
match ty.kind() {
11401141
ty::Opaque(..) => {
11411142
self.ty = Some(ty);
1142-
true
1143+
ControlFlow::BREAK
11431144
}
11441145
// Consider opaque types within projections FFI-safe if they do not normalize
11451146
// to more opaque types.
@@ -1148,7 +1149,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11481149

11491150
// If `ty` is a opaque type directly then `super_visit_with` won't invoke
11501151
// this function again.
1151-
if ty.has_opaque_types() { self.visit_ty(ty) } else { false }
1152+
if ty.has_opaque_types() {
1153+
self.visit_ty(ty)
1154+
} else {
1155+
ControlFlow::CONTINUE
1156+
}
11521157
}
11531158
_ => ty.super_visit_with(self),
11541159
}

compiler/rustc_trait_selection/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(never_type)]
2020
#![feature(crate_visibility_modifier)]
2121
#![feature(or_patterns)]
22+
#![feature(control_flow_enum)]
2223
#![recursion_limit = "512"] // For rustdoc
2324

2425
#[macro_use]

compiler/rustc_trait_selection/src/opaque_types.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
1515
use rustc_session::config::nightly_options;
1616
use rustc_span::Span;
1717

18+
use std::ops::ControlFlow;
19+
1820
pub type OpaqueTypeMap<'tcx> = DefIdMap<OpaqueTypeDecl<'tcx>>;
1921

2022
/// Information about the opaque types whose values we
@@ -691,26 +693,26 @@ impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<OP>
691693
where
692694
OP: FnMut(ty::Region<'tcx>),
693695
{
694-
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> bool {
696+
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ControlFlow<(), ()> {
695697
t.as_ref().skip_binder().visit_with(self);
696-
false // keep visiting
698+
ControlFlow::CONTINUE
697699
}
698700

699-
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
701+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<(), ()> {
700702
match *r {
701703
// ignore bound regions, keep visiting
702-
ty::ReLateBound(_, _) => false,
704+
ty::ReLateBound(_, _) => ControlFlow::CONTINUE,
703705
_ => {
704706
(self.op)(r);
705-
false
707+
ControlFlow::CONTINUE
706708
}
707709
}
708710
}
709711

710-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
712+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
711713
// We're only interested in types involving regions
712714
if !ty.flags().intersects(ty::TypeFlags::HAS_FREE_REGIONS) {
713-
return false; // keep visiting
715+
return ControlFlow::CONTINUE;
714716
}
715717

716718
match ty.kind() {
@@ -745,7 +747,7 @@ where
745747
}
746748
}
747749

748-
false
750+
ControlFlow::CONTINUE
749751
}
750752
}
751753

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_span::def_id::{DefId, LocalDefId};
2424
use rustc_span::Span;
2525

2626
use std::cmp;
27+
use std::ops::ControlFlow;
2728

2829
/// Check if a given constant can be evaluated.
2930
pub fn is_const_evaluatable<'cx, 'tcx>(
@@ -86,9 +87,11 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
8687
failure_kind = cmp::min(failure_kind, FailureKind::MentionsParam);
8788
}
8889

89-
false
90+
ControlFlow::CONTINUE
91+
}
92+
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => {
93+
ControlFlow::CONTINUE
9094
}
91-
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => false,
9295
});
9396

9497
match failure_kind {
@@ -564,29 +567,33 @@ pub(super) fn try_unify_abstract_consts<'tcx>(
564567
// on `ErrorReported`.
565568
}
566569

567-
// FIXME: Use `std::ops::ControlFlow` instead of `bool` here.
568-
pub fn walk_abstract_const<'tcx, F>(tcx: TyCtxt<'tcx>, ct: AbstractConst<'tcx>, mut f: F) -> bool
570+
pub fn walk_abstract_const<'tcx, F>(
571+
tcx: TyCtxt<'tcx>,
572+
ct: AbstractConst<'tcx>,
573+
mut f: F,
574+
) -> ControlFlow<(), ()>
569575
where
570-
F: FnMut(Node<'tcx>) -> bool,
576+
F: FnMut(Node<'tcx>) -> ControlFlow<(), ()>,
571577
{
572578
fn recurse<'tcx>(
573579
tcx: TyCtxt<'tcx>,
574580
ct: AbstractConst<'tcx>,
575-
f: &mut dyn FnMut(Node<'tcx>) -> bool,
576-
) -> bool {
581+
f: &mut dyn FnMut(Node<'tcx>) -> ControlFlow<(), ()>,
582+
) -> ControlFlow<(), ()> {
577583
let root = ct.root();
578-
f(root)
579-
|| match root {
580-
Node::Leaf(_) => false,
581-
Node::Binop(_, l, r) => {
582-
recurse(tcx, ct.subtree(l), f) || recurse(tcx, ct.subtree(r), f)
583-
}
584-
Node::UnaryOp(_, v) => recurse(tcx, ct.subtree(v), f),
585-
Node::FunctionCall(func, args) => {
586-
recurse(tcx, ct.subtree(func), f)
587-
|| args.iter().any(|&arg| recurse(tcx, ct.subtree(arg), f))
588-
}
584+
f(root)?;
585+
match root {
586+
Node::Leaf(_) => ControlFlow::CONTINUE,
587+
Node::Binop(_, l, r) => {
588+
recurse(tcx, ct.subtree(l), f)?;
589+
recurse(tcx, ct.subtree(r), f)
590+
}
591+
Node::UnaryOp(_, v) => recurse(tcx, ct.subtree(v), f),
592+
Node::FunctionCall(func, args) => {
593+
recurse(tcx, ct.subtree(func), f)?;
594+
args.iter().try_for_each(|&arg| recurse(tcx, ct.subtree(arg), f))
589595
}
596+
}
590597
}
591598

592599
recurse(tcx, ct, &mut f)

0 commit comments

Comments
 (0)