Skip to content

Commit b3a87ed

Browse files
committed
simplify logic for when a #[loop_match] state type is valid
1 parent ff1db77 commit b3a87ed

File tree

6 files changed

+18
-22
lines changed

6 files changed

+18
-22
lines changed

compiler/rustc_mir_build/src/builder/expr/into.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -254,25 +254,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
254254
// block available to the lowering of `#[const_continue]`.
255255

256256
fn is_supported_loop_match_type(ty: Ty<'_>) -> bool {
257-
if ty.is_integral() {
258-
return true;
259-
}
260-
261-
if let Some(adt_def) = ty.ty_adt_def() {
262-
if adt_def.adt_kind() != ty::AdtKind::Enum {
263-
return false;
264-
}
265-
266-
for variant in adt_def.variants() {
267-
if !variant.fields.is_empty() {
268-
return false;
257+
match ty.kind() {
258+
ty::Uint(_) | ty::Int(_) => true,
259+
ty::Adt(adt_def, _) => match adt_def.adt_kind() {
260+
ty::AdtKind::Struct | ty::AdtKind::Union => false,
261+
ty::AdtKind::Enum => {
262+
adt_def.variants().iter().all(|v| v.fields.is_empty())
269263
}
270-
}
271-
272-
return true;
264+
},
265+
_ => false,
273266
}
274-
275-
false
276267
}
277268

278269
let state_ty = this.thir.exprs[state].ty;

tests/ui/feature-gates/feature-gate-loop-match.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// A #[const_continue] to a labeled loop should error.
2+
13
enum State {
24
A,
35
B,

tests/ui/feature-gates/feature-gate-loop-match.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: the `#[loop_match]` attribute is an experimental feature
2-
--> $DIR/feature-gate-loop-match.rs:9:5
2+
--> $DIR/feature-gate-loop-match.rs:11:5
33
|
44
LL | #[loop_match]
55
| ^^^^^^^^^^^^^
@@ -9,7 +9,7 @@ LL | #[loop_match]
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

1111
error[E0658]: the `#[const_continue]` attribute is an experimental feature
12-
--> $DIR/feature-gate-loop-match.rs:14:21
12+
--> $DIR/feature-gate-loop-match.rs:16:21
1313
|
1414
LL | #[const_continue]
1515
| ^^^^^^^^^^^^^^^^^
@@ -19,7 +19,7 @@ LL | #[const_continue]
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

2121
error[E0658]: the `#[const_continue]` attribute is an experimental feature
22-
--> $DIR/feature-gate-loop-match.rs:19:21
22+
--> $DIR/feature-gate-loop-match.rs:21:21
2323
|
2424
LL | #[const_continue]
2525
| ^^^^^^^^^^^^^^^^^

tests/ui/loop-match/const-continue-to-block.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// A #[const_continue] to a normal labeled block (that does is not part of a #[loop_match]) should
2+
// error.
3+
14
#![allow(incomplete_features)]
25
#![feature(loop_match)]
36
#![crate_type = "lib"]

tests/ui/loop-match/const-continue-to-block.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `#[const_continue]` must break to a labeled block that participates in a `#[loop_match]`
2-
--> $DIR/const-continue-to-block.rs:17:27
2+
--> $DIR/const-continue-to-block.rs:20:27
33
|
44
LL | break 'b 2;
55
| ^^

tests/ui/loop-match/invalid-attribute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Checks that #[loop_match] and #[const_continue] attributes can be
22
// placed on expressions only.
3-
//
3+
44
#![allow(incomplete_features)]
55
#![feature(loop_match)]
66
#![loop_match] //~ ERROR should be applied to a loop

0 commit comments

Comments
 (0)