Skip to content

Commit 332e511

Browse files
committed
support boolean and character patterns
1 parent 7d88da4 commit 332e511

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
255255

256256
fn is_supported_loop_match_type(ty: Ty<'_>) -> bool {
257257
match ty.kind() {
258-
ty::Uint(_) | ty::Int(_) => true,
258+
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => true,
259259
ty::Adt(adt_def, _) => match adt_def.adt_kind() {
260260
ty::AdtKind::Struct | ty::AdtKind::Union => false,
261261
ty::AdtKind::Enum => {

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
29712971
};
29722972
IntRange::from_singleton(actual_int).is_subrange(int_range)
29732973
}
2974+
Constructor::Bool(pattern_value) => match valtree.unwrap_leaf().try_to_bool() {
2975+
Ok(actual_value) => *pattern_value == actual_value,
2976+
Err(()) => bug!("bool value with invalid bits"),
2977+
},
29742978
Constructor::Wildcard => true,
29752979

29762980
// these we may eventually support
@@ -2979,7 +2983,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
29792983
| Constructor::Slice(_)
29802984
| Constructor::UnionField
29812985
| Constructor::Or
2982-
| Constructor::Bool(_)
29832986
| Constructor::F16Range(..)
29842987
| Constructor::F32Range(..)
29852988
| Constructor::F64Range(..)

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -862,15 +862,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
862862

863863
let state_decl = &self.local_decls[scope.state_place.as_local().unwrap()];
864864
let state_ty = state_decl.ty;
865-
let discriminant_ty = match state_ty {
866-
ty if ty.is_enum() => ty.discriminant_ty(self.tcx),
867-
ty if ty.is_integral() => ty,
868-
_ => span_bug!(state_decl.source_info.span, "unsupported #[loop_match] state"),
869-
};
870-
871-
let rvalue = match state_ty {
872-
ty if ty.is_enum() => Rvalue::Discriminant(scope.state_place),
873-
ty if ty.is_integral() => Rvalue::Use(Operand::Copy(scope.state_place)),
865+
let (discriminant_ty, rvalue) = match state_ty.kind() {
866+
ty::Adt(adt_def, _) if adt_def.is_enum() => {
867+
(state_ty.discriminant_ty(self.tcx), Rvalue::Discriminant(scope.state_place))
868+
}
869+
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
870+
(state_ty, Rvalue::Use(Operand::Copy(scope.state_place)))
871+
}
874872
_ => span_bug!(state_decl.source_info.span, "unsupported #[loop_match] state"),
875873
};
876874

tests/ui/loop-match/integer-patterns.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66
#![feature(loop_match)]
77

88
fn main() {
9-
let mut state = 0i32;
9+
assert_eq!(integer(0), 2);
10+
assert_eq!(integer(-1), 2);
11+
assert_eq!(integer(2), 2);
12+
13+
assert_eq!(boolean(true), false);
14+
assert_eq!(boolean(false), false);
15+
16+
assert_eq!(character('a'), 'b');
17+
assert_eq!(character('b'), 'b');
18+
assert_eq!(character('c'), 'd');
19+
assert_eq!(character('d'), 'd');
20+
}
21+
22+
fn integer(mut state: i32) -> i32 {
1023
#[loop_match]
1124
'a: loop {
1225
state = 'blk: {
@@ -24,5 +37,41 @@ fn main() {
2437
}
2538
}
2639
}
27-
assert_eq!(state, 2);
40+
41+
state
42+
}
43+
44+
fn boolean(mut state: bool) -> bool {
45+
#[loop_match]
46+
loop {
47+
state = 'blk: {
48+
match state {
49+
true => {
50+
#[const_continue]
51+
break 'blk false;
52+
}
53+
false => return state,
54+
}
55+
}
56+
}
57+
}
58+
59+
fn character(mut state: char) -> char {
60+
#[loop_match]
61+
loop {
62+
state = 'blk: {
63+
match state {
64+
'a' => {
65+
#[const_continue]
66+
break 'blk 'b';
67+
}
68+
'b' => return state,
69+
'c' => {
70+
#[const_continue]
71+
break 'blk 'd';
72+
}
73+
_ => return state,
74+
}
75+
}
76+
}
2877
}

0 commit comments

Comments
 (0)