Skip to content

Commit c518108

Browse files
committed
Replace ControlFlow with version in std
- Since Rust 1.55, `ControlFlow` is now available in std - Some functions are not available in std yet, and the functional equivalents have been used (e.g. `ControlFlow::Break(())` instead of `ControlFlow::BREAK`, etc.) Closes #725
1 parent e76879d commit c518108

File tree

13 files changed

+69
-105
lines changed

13 files changed

+69
-105
lines changed

chalk-derive/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ fn derive_any_visit(
181181
&self,
182182
visitor: &mut dyn ::chalk_ir::visit::Visitor < 'i, #interner, BreakTy = B >,
183183
outer_binder: ::chalk_ir::DebruijnIndex,
184-
) -> ::chalk_ir::visit::ControlFlow<B>
184+
) -> std::ops::ControlFlow<B>
185185
where
186186
#interner: 'i
187187
{
188188
match *self {
189189
#body
190190
}
191-
::chalk_ir::visit::ControlFlow::CONTINUE
191+
std::ops::ControlFlow::Continue(())
192192
}
193193
},
194194
)

chalk-engine/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ use std::usize;
5858

5959
use chalk_derive::{Fold, HasInterner, Visit};
6060
use chalk_ir::interner::Interner;
61-
use chalk_ir::visit::ControlFlow;
6261
use chalk_ir::{
6362
AnswerSubst, Canonical, ConstrainedSubst, Constraint, DebruijnIndex, Goal, InEnvironment,
6463
Substitution,
6564
};
65+
use std::ops::ControlFlow;
6666

6767
pub mod context;
6868
mod derived;

chalk-ir/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ extern crate self as chalk_ir;
99
use crate::cast::{Cast, CastTo, Caster};
1010
use crate::fold::shift::Shift;
1111
use crate::fold::{Fold, Folder, Subst, SuperFold};
12-
use crate::visit::{ControlFlow, SuperVisit, Visit, VisitExt, Visitor};
12+
use crate::visit::{SuperVisit, Visit, VisitExt, Visitor};
1313
use chalk_derive::{Fold, HasInterner, SuperVisit, Visit, Zip};
1414
use std::marker::PhantomData;
15+
use std::ops::ControlFlow;
1516

1617
pub use crate::debug::SeparatorTraitRef;
1718
#[macro_use(bitflags)]

chalk-ir/src/visit.rs

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Traits for visiting bits of IR.
22
use std::fmt::Debug;
3+
use std::ops::ControlFlow;
34

45
use crate::{
56
BoundVar, Const, ConstValue, DebruijnIndex, DomainGoal, Goal, InferenceVar, Interner, Lifetime,
@@ -12,59 +13,15 @@ pub mod visitors;
1213

1314
pub use visitors::VisitExt;
1415

15-
/// An copy of the unstable `std::ops::ControlFlow` for use in Chalk visitors.
16-
pub enum ControlFlow<B, C = ()> {
17-
/// Continue in the loop, using the given value for the next iteration
18-
Continue(C),
19-
/// Exit the loop, yielding the given value
20-
Break(B),
21-
}
22-
23-
impl<B, C> ControlFlow<B, C> {
24-
/// Returns `true` if this is a `Break` variant.
25-
#[inline]
26-
pub fn is_break(&self) -> bool {
27-
matches!(*self, ControlFlow::Break(_))
28-
}
29-
30-
/// Returns `true` if this is a `Continue` variant.
31-
#[inline]
32-
pub fn is_continue(&self) -> bool {
33-
matches!(*self, ControlFlow::Continue(_))
34-
}
35-
36-
/// Converts the `ControlFlow` into an `Option` which is `Some`
37-
/// if the `ControlFlow` was `Break` and `None` otherwise.
38-
#[inline]
39-
pub fn break_value(self) -> Option<B> {
40-
match self {
41-
ControlFlow::Continue(..) => None,
42-
ControlFlow::Break(x) => Some(x),
43-
}
44-
}
45-
}
46-
47-
impl<B> ControlFlow<B, ()> {
48-
/// It's frequently the case that there's no value needed with `Continue`,
49-
/// so this provides a way to avoid typing `(())`, if you prefer it.
50-
pub const CONTINUE: Self = ControlFlow::Continue(());
51-
}
52-
53-
impl<C> ControlFlow<(), C> {
54-
/// APIs like `try_for_each` don't need values with `Break`,
55-
/// so this provides a way to avoid typing `(())`, if you prefer it.
56-
pub const BREAK: Self = ControlFlow::Break(());
57-
}
58-
5916
/// Unwraps a `ControlFlow` or propagates its `Break` value.
6017
/// This replaces the `Try` implementation that would be used
6118
/// with `std::ops::ControlFlow`.
6219
#[macro_export]
6320
macro_rules! try_break {
6421
($expr:expr) => {
6522
match $expr {
66-
$crate::visit::ControlFlow::Continue(c) => c,
67-
$crate::visit::ControlFlow::Break(b) => return $crate::visit::ControlFlow::Break(b),
23+
std::ops::ControlFlow::Continue(c) => c,
24+
std::ops::ControlFlow::Break(b) => return std::ops::ControlFlow::Break(b),
6825
}
6926
};
7027
}
@@ -174,7 +131,7 @@ where
174131
bound_var, outer_binder
175132
)
176133
} else {
177-
ControlFlow::CONTINUE
134+
ControlFlow::Continue(())
178135
}
179136
}
180137

@@ -194,7 +151,7 @@ where
194151
if self.forbid_free_placeholders() {
195152
panic!("unexpected placeholder type `{:?}`", universe)
196153
} else {
197-
ControlFlow::CONTINUE
154+
ControlFlow::Continue(())
198155
}
199156
}
200157

@@ -224,7 +181,7 @@ where
224181
if self.forbid_inference_vars() {
225182
panic!("unexpected inference type `{:?}`", var)
226183
} else {
227-
ControlFlow::CONTINUE
184+
ControlFlow::Continue(())
228185
}
229186
}
230187

@@ -298,7 +255,7 @@ where
298255
if let Some(_) = bound_var.shifted_out_to(outer_binder) {
299256
visitor.visit_free_var(*bound_var, outer_binder)
300257
} else {
301-
ControlFlow::CONTINUE
258+
ControlFlow::Continue(())
302259
}
303260
}
304261
TyKind::Dyn(clauses) => clauses.visit_with(visitor, outer_binder),
@@ -311,7 +268,7 @@ where
311268
substitution.visit_with(visitor, outer_binder)
312269
}
313270
TyKind::Scalar(scalar) => scalar.visit_with(visitor, outer_binder),
314-
TyKind::Str => ControlFlow::CONTINUE,
271+
TyKind::Str => ControlFlow::Continue(()),
315272
TyKind::Tuple(arity, substitution) => {
316273
try_break!(arity.visit_with(visitor, outer_binder));
317274
substitution.visit_with(visitor, outer_binder)
@@ -334,7 +291,7 @@ where
334291
try_break!(mutability.visit_with(visitor, outer_binder));
335292
ty.visit_with(visitor, outer_binder)
336293
}
337-
TyKind::Never => ControlFlow::CONTINUE,
294+
TyKind::Never => ControlFlow::Continue(()),
338295
TyKind::Array(ty, const_) => {
339296
try_break!(ty.visit_with(visitor, outer_binder));
340297
const_.visit_with(visitor, outer_binder)
@@ -352,7 +309,7 @@ where
352309
substitution.visit_with(visitor, outer_binder)
353310
}
354311
TyKind::Foreign(foreign_ty) => foreign_ty.visit_with(visitor, outer_binder),
355-
TyKind::Error => ControlFlow::CONTINUE,
312+
TyKind::Error => ControlFlow::Continue(()),
356313
}
357314
}
358315
}
@@ -385,15 +342,15 @@ impl<I: Interner> SuperVisit<I> for Lifetime<I> {
385342
if let Some(_) = bound_var.shifted_out_to(outer_binder) {
386343
visitor.visit_free_var(*bound_var, outer_binder)
387344
} else {
388-
ControlFlow::CONTINUE
345+
ControlFlow::Continue(())
389346
}
390347
}
391348
LifetimeData::InferenceVar(var) => visitor.visit_inference_var(*var, outer_binder),
392349
LifetimeData::Placeholder(universe) => {
393350
visitor.visit_free_placeholder(*universe, outer_binder)
394351
}
395352
LifetimeData::Static | LifetimeData::Empty(_) | LifetimeData::Erased => {
396-
ControlFlow::CONTINUE
353+
ControlFlow::Continue(())
397354
}
398355
LifetimeData::Phantom(void, ..) => match *void {},
399356
}
@@ -428,14 +385,14 @@ impl<I: Interner> SuperVisit<I> for Const<I> {
428385
if let Some(_) = bound_var.shifted_out_to(outer_binder) {
429386
visitor.visit_free_var(*bound_var, outer_binder)
430387
} else {
431-
ControlFlow::CONTINUE
388+
ControlFlow::Continue(())
432389
}
433390
}
434391
ConstValue::InferenceVar(var) => visitor.visit_inference_var(*var, outer_binder),
435392
ConstValue::Placeholder(universe) => {
436393
visitor.visit_free_placeholder(*universe, outer_binder)
437394
}
438-
ConstValue::Concrete(_) => ControlFlow::CONTINUE,
395+
ConstValue::Concrete(_) => ControlFlow::Continue(()),
439396
}
440397
}
441398
}

chalk-ir/src/visit/boring_impls.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ where
2626
for e in it {
2727
try_break!(e.visit_with(visitor, outer_binder));
2828
}
29-
ControlFlow::CONTINUE
29+
ControlFlow::Continue(())
3030
}
3131

3232
impl<T: Visit<I>, I: Interner> Visit<I> for &T {
@@ -104,7 +104,7 @@ macro_rules! tuple_visit {
104104
$(
105105
try_break!($n.visit_with(visitor, outer_binder));
106106
)*
107-
ControlFlow::CONTINUE
107+
ControlFlow::Continue(())
108108
}
109109
}
110110
}
@@ -126,7 +126,7 @@ impl<T: Visit<I>, I: Interner> Visit<I> for Option<T> {
126126
{
127127
match self {
128128
Some(e) => e.visit_with(visitor, outer_binder),
129-
None => ControlFlow::CONTINUE,
129+
None => ControlFlow::Continue(()),
130130
}
131131
}
132132
}
@@ -186,7 +186,7 @@ macro_rules! const_visit {
186186
where
187187
I: 'i,
188188
{
189-
ControlFlow::CONTINUE
189+
ControlFlow::Continue(())
190190
}
191191
}
192192
};
@@ -220,7 +220,7 @@ macro_rules! id_visit {
220220
where
221221
I: 'i,
222222
{
223-
ControlFlow::CONTINUE
223+
ControlFlow::Continue(())
224224
}
225225
}
226226
};
@@ -305,6 +305,6 @@ impl<I: Interner> Visit<I> for PhantomData<I> {
305305
where
306306
I: 'i,
307307
{
308-
ControlFlow::CONTINUE
308+
ControlFlow::Continue(())
309309
}
310310
}

chalk-ir/src/visit/visitors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use crate::{BoundVar, ControlFlow, DebruijnIndex, Interner, Visit, Visitor};
66
pub trait VisitExt<I: Interner>: Visit<I> {
77
/// Check whether there are free (non-bound) variables.
88
fn has_free_vars(&self, interner: &I) -> bool {
9-
self.visit_with(
9+
let flow = self.visit_with(
1010
&mut FindFreeVarsVisitor { interner },
1111
DebruijnIndex::INNERMOST,
12-
)
13-
.is_break()
12+
);
13+
matches!(flow, ControlFlow::Break(_))
1414
}
1515
}
1616

@@ -36,6 +36,6 @@ impl<'i, I: Interner> Visitor<'i, I> for FindFreeVarsVisitor<'i, I> {
3636
_bound_var: BoundVar,
3737
_outer_binder: DebruijnIndex,
3838
) -> ControlFlow<()> {
39-
ControlFlow::BREAK
39+
ControlFlow::Break(())
4040
}
4141
}

chalk-solve/src/clauses/builtin_traits/unsize.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::collections::HashSet;
22
use std::iter;
3+
use std::ops::ControlFlow;
34

45
use crate::clauses::ClauseBuilder;
56
use crate::rust_ir::AdtKind;
67
use crate::{Interner, RustIrDatabase, TraitRef, WellKnownTrait};
78
use chalk_ir::{
89
cast::Cast,
910
interner::HasInterner,
10-
visit::{ControlFlow, SuperVisit, Visit, Visitor},
11+
visit::{SuperVisit, Visit, Visitor},
1112
Binders, Const, ConstValue, DebruijnIndex, DomainGoal, DynTy, EqGoal, Goal, LifetimeOutlives,
1213
QuantifiedWhereClauses, Substitution, TraitId, Ty, TyKind, TypeOutlives, WhereClause,
1314
};
@@ -34,7 +35,7 @@ impl<'a, I: Interner> Visitor<'a, I> for UnsizeParameterCollector<'a, I> {
3435
if bound_var.debruijn.shifted_in() == outer_binder {
3536
self.parameters.insert(bound_var.index);
3637
}
37-
ControlFlow::CONTINUE
38+
ControlFlow::Continue(())
3839
}
3940
_ => ty.super_visit_with(self, outer_binder),
4041
}
@@ -49,7 +50,7 @@ impl<'a, I: Interner> Visitor<'a, I> for UnsizeParameterCollector<'a, I> {
4950
self.parameters.insert(bound_var.index);
5051
}
5152
}
52-
ControlFlow::CONTINUE
53+
ControlFlow::Continue(())
5354
}
5455

5556
fn interner(&self) -> &'a I {
@@ -90,9 +91,9 @@ impl<'a, 'p, I: Interner> Visitor<'a, I> for ParameterOccurenceCheck<'a, 'p, I>
9091
if bound_var.debruijn.shifted_in() == outer_binder
9192
&& self.parameters.contains(&bound_var.index)
9293
{
93-
ControlFlow::BREAK
94+
ControlFlow::Break(())
9495
} else {
95-
ControlFlow::CONTINUE
96+
ControlFlow::Continue(())
9697
}
9798
}
9899
_ => ty.super_visit_with(self, outer_binder),
@@ -107,12 +108,12 @@ impl<'a, 'p, I: Interner> Visitor<'a, I> for ParameterOccurenceCheck<'a, 'p, I>
107108
if bound_var.debruijn.shifted_in() == outer_binder
108109
&& self.parameters.contains(&bound_var.index)
109110
{
110-
ControlFlow::BREAK
111+
ControlFlow::Break(())
111112
} else {
112-
ControlFlow::CONTINUE
113+
ControlFlow::Continue(())
113114
}
114115
}
115-
_ => ControlFlow::CONTINUE,
116+
_ => ControlFlow::Continue(()),
116117
}
117118
}
118119

@@ -130,8 +131,9 @@ fn uses_outer_binder_params<I: Interner>(
130131
interner,
131132
parameters,
132133
};
133-
v.visit_with(&mut visitor, DebruijnIndex::INNERMOST)
134-
.is_break()
134+
135+
let flow = v.visit_with(&mut visitor, DebruijnIndex::INNERMOST);
136+
matches!(flow, ControlFlow::Break(_))
135137
}
136138

137139
fn principal_id<'a, I: Interner>(

chalk-solve/src/clauses/env_elaborator.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use crate::RustIrDatabase;
88
use crate::Ty;
99
use crate::{debug_span, TyKind};
1010
use chalk_ir::interner::Interner;
11-
use chalk_ir::visit::{ControlFlow, Visit, Visitor};
11+
use chalk_ir::visit::{Visit, Visitor};
1212
use chalk_ir::{DebruijnIndex, Environment};
1313
use rustc_hash::FxHashSet;
14+
use std::ops::ControlFlow;
1415
use tracing::instrument;
1516

1617
/// When proving a `FromEnv` goal, we elaborate all `FromEnv` goals
@@ -71,7 +72,7 @@ impl<'me, 'builder, I: Interner> Visitor<'me, I> for EnvElaborator<'me, 'builder
7172
.unwrap()
7273
}
7374
}
74-
ControlFlow::CONTINUE
75+
ControlFlow::Continue(())
7576
}
7677

7778
fn visit_domain_goal(
@@ -95,12 +96,12 @@ impl<'me, 'builder, I: Interner> Visitor<'me, I> for EnvElaborator<'me, 'builder
9596
.associated_ty_data(associated_ty_id)
9697
.to_program_clauses(self.builder, self.environment);
9798
}
98-
ControlFlow::CONTINUE
99+
ControlFlow::Continue(())
99100
}
100101
FromEnv::Ty(ty) => ty.visit_with(self, outer_binder),
101102
}
102103
} else {
103-
ControlFlow::CONTINUE
104+
ControlFlow::Continue(())
104105
}
105106
}
106107
}

0 commit comments

Comments
 (0)