Skip to content

Commit 61f8182

Browse files
TypeVisitor: use ControlFlow in rustc_{mir,privacy,traits,typeck}
1 parent 4fe735b commit 61f8182

File tree

13 files changed

+171
-122
lines changed

13 files changed

+171
-122
lines changed

compiler/rustc_mir/src/interpret/util.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_middle::mir::interpret::InterpResult;
22
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor};
33
use std::convert::TryInto;
4+
use std::ops::ControlFlow;
45

56
/// Returns `true` if a used generic parameter requires substitution.
67
crate fn ensure_monomorphic_enough<'tcx, T>(tcx: TyCtxt<'tcx>, ty: T) -> InterpResult<'tcx>
@@ -17,24 +18,24 @@ where
1718
};
1819

1920
impl<'tcx> TypeVisitor<'tcx> for UsedParamsNeedSubstVisitor<'tcx> {
20-
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
21+
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<(), ()> {
2122
if !c.needs_subst() {
22-
return false;
23+
return ControlFlow::CONTINUE;
2324
}
2425

2526
match c.val {
26-
ty::ConstKind::Param(..) => true,
27+
ty::ConstKind::Param(..) => ControlFlow::BREAK,
2728
_ => c.super_visit_with(self),
2829
}
2930
}
3031

31-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
32+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
3233
if !ty.needs_subst() {
33-
return false;
34+
return ControlFlow::CONTINUE;
3435
}
3536

3637
match *ty.kind() {
37-
ty::Param(_) => true,
38+
ty::Param(_) => ControlFlow::BREAK,
3839
ty::Closure(def_id, substs)
3940
| ty::Generator(def_id, substs, ..)
4041
| ty::FnDef(def_id, substs) => {
@@ -50,11 +51,7 @@ where
5051
match (is_used, subst.needs_subst()) {
5152
// Just in case there are closures or generators within this subst,
5253
// recurse.
53-
(true, true) if subst.super_visit_with(self) => {
54-
// Only return when we find a parameter so the remaining substs
55-
// are not skipped.
56-
return true;
57-
}
54+
(true, true) => return subst.super_visit_with(self),
5855
// Confirm that polymorphization replaced the parameter with
5956
// `ty::Param`/`ty::ConstKind::Param`.
6057
(false, true) if cfg!(debug_assertions) => match subst.unpack() {
@@ -69,15 +66,15 @@ where
6966
_ => {}
7067
}
7168
}
72-
false
69+
ControlFlow::CONTINUE
7370
}
7471
_ => ty.super_visit_with(self),
7572
}
7673
}
7774
}
7875

7976
let mut vis = UsedParamsNeedSubstVisitor { tcx };
80-
if ty.visit_with(&mut vis) {
77+
if ty.visit_with(&mut vis) == ControlFlow::BREAK {
8178
throw_inval!(TooGeneric);
8279
} else {
8380
Ok(())

compiler/rustc_mir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Rust MIR: a lowered representation of Rust.
2727
#![feature(option_expect_none)]
2828
#![feature(or_patterns)]
2929
#![feature(once_cell)]
30+
#![feature(control_flow_enum)]
3031
#![recursion_limit = "256"]
3132

3233
#[macro_use]

compiler/rustc_mir/src/monomorphize/polymorphize.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_middle::ty::{
2020
};
2121
use rustc_span::symbol::sym;
2222
use std::convert::TryInto;
23+
use std::ops::ControlFlow;
2324

2425
/// Provide implementations of queries relating to polymorphization analysis.
2526
pub fn provide(providers: &mut Providers) {
@@ -138,7 +139,7 @@ fn mark_used_by_predicates<'tcx>(
138139
// predicate is used.
139140
let any_param_used = {
140141
let mut vis = HasUsedGenericParams { unused_parameters };
141-
predicate.visit_with(&mut vis)
142+
predicate.visit_with(&mut vis) == ControlFlow::BREAK
142143
};
143144

144145
if any_param_used {
@@ -249,17 +250,17 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
249250
}
250251

251252
impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
252-
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> bool {
253+
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<(), ()> {
253254
debug!("visit_const: c={:?}", c);
254255
if !c.has_param_types_or_consts() {
255-
return false;
256+
return ControlFlow::CONTINUE;
256257
}
257258

258259
match c.val {
259260
ty::ConstKind::Param(param) => {
260261
debug!("visit_const: param={:?}", param);
261262
self.unused_parameters.clear(param.index);
262-
false
263+
ControlFlow::CONTINUE
263264
}
264265
ty::ConstKind::Unevaluated(def, _, Some(p))
265266
// Avoid considering `T` unused when constants are of the form:
@@ -270,41 +271,41 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
270271
// the generic parameters, instead, traverse the promoted MIR.
271272
let promoted = self.tcx.promoted_mir(def.did);
272273
self.visit_body(&promoted[p]);
273-
false
274+
ControlFlow::CONTINUE
274275
}
275276
ty::ConstKind::Unevaluated(def, unevaluated_substs, None)
276277
if self.tcx.def_kind(def.did) == DefKind::AnonConst =>
277278
{
278279
self.visit_child_body(def.did, unevaluated_substs);
279-
false
280+
ControlFlow::CONTINUE
280281
}
281282
_ => c.super_visit_with(self),
282283
}
283284
}
284285

285-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
286+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
286287
debug!("visit_ty: ty={:?}", ty);
287288
if !ty.has_param_types_or_consts() {
288-
return false;
289+
return ControlFlow::CONTINUE;
289290
}
290291

291292
match *ty.kind() {
292293
ty::Closure(def_id, substs) | ty::Generator(def_id, substs, ..) => {
293294
debug!("visit_ty: def_id={:?}", def_id);
294295
// Avoid cycle errors with generators.
295296
if def_id == self.def_id {
296-
return false;
297+
return ControlFlow::CONTINUE;
297298
}
298299

299300
// Consider any generic parameters used by any closures/generators as used in the
300301
// parent.
301302
self.visit_child_body(def_id, substs);
302-
false
303+
ControlFlow::CONTINUE
303304
}
304305
ty::Param(param) => {
305306
debug!("visit_ty: param={:?}", param);
306307
self.unused_parameters.clear(param.index);
307-
false
308+
ControlFlow::CONTINUE
308309
}
309310
_ => ty.super_visit_with(self),
310311
}
@@ -317,28 +318,38 @@ struct HasUsedGenericParams<'a> {
317318
}
318319

319320
impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a> {
320-
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> bool {
321+
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<(), ()> {
321322
debug!("visit_const: c={:?}", c);
322323
if !c.has_param_types_or_consts() {
323-
return false;
324+
return ControlFlow::CONTINUE;
324325
}
325326

326327
match c.val {
327328
ty::ConstKind::Param(param) => {
328-
!self.unused_parameters.contains(param.index).unwrap_or(false)
329+
if self.unused_parameters.contains(param.index).unwrap_or(false) {
330+
ControlFlow::CONTINUE
331+
} else {
332+
ControlFlow::BREAK
333+
}
329334
}
330335
_ => c.super_visit_with(self),
331336
}
332337
}
333338

334-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
339+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> {
335340
debug!("visit_ty: ty={:?}", ty);
336341
if !ty.has_param_types_or_consts() {
337-
return false;
342+
return ControlFlow::CONTINUE;
338343
}
339344

340345
match ty.kind() {
341-
ty::Param(param) => !self.unused_parameters.contains(param.index).unwrap_or(false),
346+
ty::Param(param) => {
347+
if self.unused_parameters.contains(param.index).unwrap_or(false) {
348+
ControlFlow::CONTINUE
349+
} else {
350+
ControlFlow::BREAK
351+
}
352+
}
342353
_ => ty.super_visit_with(self),
343354
}
344355
}

compiler/rustc_mir/src/util/pretty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::mir::visit::Visitor;
1919
use rustc_middle::mir::*;
2020
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
2121
use rustc_target::abi::Size;
22+
use std::ops::ControlFlow;
2223

2324
const INDENT: &str = " ";
2425
/// Alignment for lining up comments following MIR statements
@@ -639,7 +640,7 @@ pub fn write_allocations<'tcx>(
639640
}
640641
struct CollectAllocIds(BTreeSet<AllocId>);
641642
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
642-
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
643+
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<(), ()> {
643644
if let ty::ConstKind::Value(val) = c.val {
644645
self.0.extend(alloc_ids_from_const(val));
645646
}

0 commit comments

Comments
 (0)