Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2c85b6f

Browse files
TypeVisitor: use std::ops::ControlFlow instead of bool
1 parent 8df58ae commit 2c85b6f

File tree

10 files changed

+242
-164
lines changed

10 files changed

+242
-164
lines changed

compiler/rustc_macros/src/type_foldable.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
1515
}
1616
})
1717
});
18-
let body_visit = s.fold(false, |acc, bind| {
19-
quote! { #acc || ::rustc_middle::ty::fold::TypeFoldable::visit_with(#bind, __folder) }
18+
19+
let body_visit = s.fold(quote!(), |acc, bind| {
20+
quote! {
21+
#acc
22+
::rustc_middle::ty::fold::TypeFoldable::visit_with(#bind, __folder)?;
23+
}
2024
});
2125

2226
s.bound_impl(
@@ -32,8 +36,9 @@ pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::
3236
fn super_visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>(
3337
&self,
3438
__folder: &mut __F
35-
) -> bool {
39+
) -> ::std::ops::ControlFlow<(), ()> {
3640
match *self { #body_visit }
41+
::std::ops::ControlFlow::CONTINUE
3742
}
3843
},
3944
)

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#![feature(int_error_matching)]
5050
#![feature(half_open_range_patterns)]
5151
#![feature(exclusive_range_pattern)]
52+
#![feature(control_flow_enum)]
5253
#![recursion_limit = "512"]
5354

5455
#[macro_use]

compiler/rustc_middle/src/macros.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ macro_rules! CloneTypeFoldableImpls {
6262
fn super_visit_with<F: $crate::ty::fold::TypeVisitor<$tcx>>(
6363
&self,
6464
_: &mut F)
65-
-> bool
65+
-> ::std::ops::ControlFlow<(), ()>
6666
{
67-
false
67+
::std::ops::ControlFlow::CONTINUE
6868
}
6969
}
7070
)+
@@ -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-
) -> bool {
108+
) -> ::std::ops::ControlFlow<(), ()> {
109109
EnumTypeFoldableImpl!(@VisitVariants(self, visitor) input($($variants)*) output())
110110
}
111111
}
@@ -179,9 +179,10 @@ macro_rules! EnumTypeFoldableImpl {
179179
input($($input)*)
180180
output(
181181
$variant ( $($variant_arg),* ) => {
182-
false $(|| $crate::ty::fold::TypeFoldable::visit_with(
182+
$($crate::ty::fold::TypeFoldable::visit_with(
183183
$variant_arg, $visitor
184-
))*
184+
)?;)*
185+
::std::ops::ControlFlow::CONTINUE
185186
}
186187
$($output)*
187188
)
@@ -196,9 +197,10 @@ macro_rules! EnumTypeFoldableImpl {
196197
input($($input)*)
197198
output(
198199
$variant { $($variant_arg),* } => {
199-
false $(|| $crate::ty::fold::TypeFoldable::visit_with(
200+
$($crate::ty::fold::TypeFoldable::visit_with(
200201
$variant_arg, $visitor
201-
))*
202+
)?;)*
203+
::std::ops::ControlFlow::CONTINUE
202204
}
203205
$($output)*
204206
)
@@ -212,7 +214,7 @@ macro_rules! EnumTypeFoldableImpl {
212214
@VisitVariants($this, $visitor)
213215
input($($input)*)
214216
output(
215-
$variant => { false }
217+
$variant => { ::std::ops::ControlFlow::CONTINUE }
216218
$($output)*
217219
)
218220
)

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_target::abi;
3232
use rustc_target::asm::InlineAsmRegOrRegClass;
3333
use std::borrow::Cow;
3434
use std::fmt::{self, Debug, Display, Formatter, Write};
35-
use std::ops::{Index, IndexMut};
35+
use std::ops::{ControlFlow, Index, IndexMut};
3636
use std::slice;
3737
use std::{iter, mem, option};
3838

@@ -2489,7 +2489,7 @@ impl<'tcx> TypeFoldable<'tcx> for UserTypeProjection {
24892489
UserTypeProjection { base, projs }
24902490
}
24912491

2492-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
2492+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<(), ()> {
24932493
self.base.visit_with(visitor)
24942494
// Note: there's nothing in `self.proj` to visit.
24952495
}

compiler/rustc_middle/src/mir/type_foldable.rs

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,46 @@ 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) -> bool {
90+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
9191
use crate::mir::TerminatorKind::*;
9292

9393
match self.kind {
9494
SwitchInt { ref discr, switch_ty, .. } => {
95-
discr.visit_with(visitor) || switch_ty.visit_with(visitor)
95+
discr.visit_with(visitor)?;
96+
switch_ty.visit_with(visitor)
9697
}
9798
Drop { ref place, .. } => place.visit_with(visitor),
9899
DropAndReplace { ref place, ref value, .. } => {
99-
place.visit_with(visitor) || value.visit_with(visitor)
100+
place.visit_with(visitor)?;
101+
value.visit_with(visitor)
100102
}
101103
Yield { ref value, .. } => value.visit_with(visitor),
102104
Call { ref func, ref args, ref destination, .. } => {
103-
let dest = if let Some((ref loc, _)) = *destination {
104-
loc.visit_with(visitor)
105-
} else {
106-
false
105+
if let Some((ref loc, _)) = *destination {
106+
loc.visit_with(visitor)?;
107107
};
108-
dest || func.visit_with(visitor) || args.visit_with(visitor)
108+
func.visit_with(visitor)?;
109+
args.visit_with(visitor)
109110
}
110111
Assert { ref cond, ref msg, .. } => {
111-
if cond.visit_with(visitor) {
112+
if cond.visit_with(visitor) == ControlFlow::BREAK {
112113
use AssertKind::*;
113114
match msg {
114115
BoundsCheck { ref len, ref index } => {
115-
len.visit_with(visitor) || index.visit_with(visitor)
116+
len.visit_with(visitor)?;
117+
index.visit_with(visitor)
118+
}
119+
Overflow(_, l, r) => {
120+
l.visit_with(visitor)?;
121+
r.visit_with(visitor)
116122
}
117-
Overflow(_, l, r) => l.visit_with(visitor) || r.visit_with(visitor),
118123
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
119124
op.visit_with(visitor)
120125
}
121-
ResumedAfterReturn(_) | ResumedAfterPanic(_) => false,
126+
ResumedAfterReturn(_) | ResumedAfterPanic(_) => ControlFlow::CONTINUE,
122127
}
123128
} else {
124-
false
129+
ControlFlow::CONTINUE
125130
}
126131
}
127132
InlineAsm { ref operands, .. } => operands.visit_with(visitor),
@@ -132,7 +137,7 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
132137
| GeneratorDrop
133138
| Unreachable
134139
| FalseEdge { .. }
135-
| FalseUnwind { .. } => false,
140+
| FalseUnwind { .. } => ControlFlow::CONTINUE,
136141
}
137142
}
138143
}
@@ -142,8 +147,8 @@ impl<'tcx> TypeFoldable<'tcx> for GeneratorKind {
142147
*self
143148
}
144149

145-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
146-
false
150+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<(), ()> {
151+
ControlFlow::CONTINUE
147152
}
148153
}
149154

@@ -152,8 +157,9 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
152157
Place { local: self.local.fold_with(folder), projection: self.projection.fold_with(folder) }
153158
}
154159

155-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
156-
self.local.visit_with(visitor) || self.projection.visit_with(visitor)
160+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
161+
self.local.visit_with(visitor)?;
162+
self.projection.visit_with(visitor)
157163
}
158164
}
159165

@@ -163,8 +169,8 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
163169
folder.tcx().intern_place_elems(&v)
164170
}
165171

166-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
167-
self.iter().any(|t| t.visit_with(visitor))
172+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
173+
self.iter().try_for_each(|t| t.visit_with(visitor))
168174
}
169175
}
170176

@@ -213,32 +219,47 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
213219
}
214220
}
215221

216-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
222+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
217223
use crate::mir::Rvalue::*;
218224
match *self {
219225
Use(ref op) => op.visit_with(visitor),
220226
Repeat(ref op, _) => op.visit_with(visitor),
221227
ThreadLocalRef(did) => did.visit_with(visitor),
222-
Ref(region, _, ref place) => region.visit_with(visitor) || place.visit_with(visitor),
228+
Ref(region, _, ref place) => {
229+
region.visit_with(visitor)?;
230+
place.visit_with(visitor)
231+
}
223232
AddressOf(_, ref place) => place.visit_with(visitor),
224233
Len(ref place) => place.visit_with(visitor),
225-
Cast(_, ref op, ty) => op.visit_with(visitor) || ty.visit_with(visitor),
234+
Cast(_, ref op, ty) => {
235+
op.visit_with(visitor)?;
236+
ty.visit_with(visitor)
237+
}
226238
BinaryOp(_, ref rhs, ref lhs) | CheckedBinaryOp(_, ref rhs, ref lhs) => {
227-
rhs.visit_with(visitor) || lhs.visit_with(visitor)
239+
rhs.visit_with(visitor)?;
240+
lhs.visit_with(visitor)
228241
}
229242
UnaryOp(_, ref val) => val.visit_with(visitor),
230243
Discriminant(ref place) => place.visit_with(visitor),
231244
NullaryOp(_, ty) => ty.visit_with(visitor),
232245
Aggregate(ref kind, ref fields) => {
233-
(match **kind {
234-
AggregateKind::Array(ty) => ty.visit_with(visitor),
235-
AggregateKind::Tuple => false,
246+
match **kind {
247+
AggregateKind::Array(ty) => {
248+
ty.visit_with(visitor)?;
249+
}
250+
AggregateKind::Tuple => {}
236251
AggregateKind::Adt(_, _, substs, user_ty, _) => {
237-
substs.visit_with(visitor) || user_ty.visit_with(visitor)
252+
substs.visit_with(visitor)?;
253+
user_ty.visit_with(visitor)?;
254+
}
255+
AggregateKind::Closure(_, substs) => {
256+
substs.visit_with(visitor)?;
238257
}
239-
AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
240-
AggregateKind::Generator(_, substs, _) => substs.visit_with(visitor),
241-
}) || fields.visit_with(visitor)
258+
AggregateKind::Generator(_, substs, _) => {
259+
substs.visit_with(visitor)?;
260+
}
261+
}
262+
fields.visit_with(visitor)
242263
}
243264
}
244265
}
@@ -253,7 +274,7 @@ impl<'tcx> TypeFoldable<'tcx> for Operand<'tcx> {
253274
}
254275
}
255276

256-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
277+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
257278
match *self {
258279
Operand::Copy(ref place) | Operand::Move(ref place) => place.visit_with(visitor),
259280
Operand::Constant(ref c) => c.visit_with(visitor),
@@ -277,13 +298,13 @@ impl<'tcx> TypeFoldable<'tcx> for PlaceElem<'tcx> {
277298
}
278299
}
279300

280-
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> bool {
301+
fn super_visit_with<Vs: TypeVisitor<'tcx>>(&self, visitor: &mut Vs) -> ControlFlow<(), ()> {
281302
use crate::mir::ProjectionElem::*;
282303

283304
match self {
284305
Field(_, ty) => ty.visit_with(visitor),
285306
Index(v) => v.visit_with(visitor),
286-
_ => false,
307+
_ => ControlFlow::CONTINUE,
287308
}
288309
}
289310
}
@@ -292,26 +313,26 @@ impl<'tcx> TypeFoldable<'tcx> for Field {
292313
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
293314
*self
294315
}
295-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
296-
false
316+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<(), ()> {
317+
ControlFlow::CONTINUE
297318
}
298319
}
299320

300321
impl<'tcx> TypeFoldable<'tcx> for GeneratorSavedLocal {
301322
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
302323
*self
303324
}
304-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
305-
false
325+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<(), ()> {
326+
ControlFlow::CONTINUE
306327
}
307328
}
308329

309330
impl<'tcx, R: Idx, C: Idx> TypeFoldable<'tcx> for BitMatrix<R, C> {
310331
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
311332
self.clone()
312333
}
313-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
314-
false
334+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<(), ()> {
335+
ControlFlow::CONTINUE
315336
}
316337
}
317338

@@ -323,7 +344,7 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
323344
literal: self.literal.fold_with(folder),
324345
}
325346
}
326-
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
347+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<(), ()> {
327348
self.literal.visit_with(visitor)
328349
}
329350
}

0 commit comments

Comments
 (0)