Skip to content

Commit 6bf3cbe

Browse files
In rustc_pattern_analysis, put true witnesses before false witnesses
In rustc it doesn't really matter what the order of the witnesses is, but I'm planning to use the witnesses for implementing the "add missing match arms" assist in rust-analyzer, and there `true` before `false` is the natural order (like `Some` before `None`), and also what the current assist does. The current order doesn't seem to be intentional; the code was created when bool ctors became their own thing, not just int ctors, but for integer, 0 before 1 is indeed the natural order.
1 parent 4b596bb commit 6bf3cbe

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
lines changed

compiler/rustc_pattern_analysis/src/constructor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,16 +1130,16 @@ impl<Cx: PatCx> ConstructorSet<Cx> {
11301130
seen_false = true;
11311131
}
11321132
}
1133-
if seen_false {
1134-
present.push(Bool(false));
1135-
} else {
1136-
missing.push(Bool(false));
1137-
}
11381133
if seen_true {
11391134
present.push(Bool(true));
11401135
} else {
11411136
missing.push(Bool(true));
11421137
}
1138+
if seen_false {
1139+
present.push(Bool(false));
1140+
} else {
1141+
missing.push(Bool(false));
1142+
}
11431143
}
11441144
ConstructorSet::Integers { range_1, range_2 } => {
11451145
let seen_ranges: Vec<_> =

compiler/rustc_pattern_analysis/tests/exhaustiveness.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ fn test_witnesses() {
176176
),
177177
vec!["Enum::Variant1(_)", "Enum::Variant2(_)", "_"],
178178
);
179+
180+
// Assert we put `true` before `false`.
181+
assert_witnesses(AllOfThem, Ty::Bool, Vec::new(), vec!["true", "false"]);
179182
}
180183

181184
#[test]

tests/ui/pattern/deref-patterns/usefulness/non-exhaustive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
}
1616

1717
match Box::new((true, Box::new(false))) {
18-
//~^ ERROR non-exhaustive patterns: `deref!((false, deref!(false)))` and `deref!((true, deref!(true)))` not covered
18+
//~^ ERROR non-exhaustive patterns: `deref!((true, deref!(true)))` and `deref!((false, deref!(false)))` not covered
1919
(true, false) => {}
2020
(false, true) => {}
2121
}

tests/ui/pattern/deref-patterns/usefulness/non-exhaustive.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ LL ~ true => {},
2828
LL + deref!(deref!(false)) => todo!()
2929
|
3030

31-
error[E0004]: non-exhaustive patterns: `deref!((false, deref!(false)))` and `deref!((true, deref!(true)))` not covered
31+
error[E0004]: non-exhaustive patterns: `deref!((true, deref!(true)))` and `deref!((false, deref!(false)))` not covered
3232
--> $DIR/non-exhaustive.rs:17:11
3333
|
3434
LL | match Box::new((true, Box::new(false))) {
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ patterns `deref!((false, deref!(false)))` and `deref!((true, deref!(true)))` not covered
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ patterns `deref!((true, deref!(true)))` and `deref!((false, deref!(false)))` not covered
3636
|
3737
note: `Box<(bool, Box<bool>)>` defined here
3838
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
3939
= note: the matched value is of type `Box<(bool, Box<bool>)>`
4040
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
4141
|
4242
LL ~ (false, true) => {},
43-
LL + deref!((false, deref!(false))) | deref!((true, deref!(true))) => todo!()
43+
LL + deref!((true, deref!(true))) | deref!((false, deref!(false))) => todo!()
4444
|
4545

4646
error[E0004]: non-exhaustive patterns: `deref!((deref!(T::C), _))` not covered

tests/ui/pattern/usefulness/unions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
}
2727
// Our approach can report duplicate witnesses sometimes.
2828
match (x, true) {
29-
//~^ ERROR non-exhaustive patterns: `(U8AsBool { n: 0_u8 }, false)`, `(U8AsBool { b: false }, false)`, `(U8AsBool { n: 0_u8 }, false)` and 1 more not covered
29+
//~^ ERROR non-exhaustive patterns: `(U8AsBool { n: 0_u8 }, false)`, `(U8AsBool { b: true }, false)`, `(U8AsBool { n: 0_u8 }, false)` and 1 more not covered
3030
(U8AsBool { b: true }, true) => {}
3131
(U8AsBool { b: false }, true) => {}
3232
(U8AsBool { n: 1.. }, true) => {}

tests/ui/pattern/usefulness/unions.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ LL ~ U8AsBool { n: 1.. } => {},
1616
LL + U8AsBool { n: 0_u8 } | U8AsBool { b: false } => todo!()
1717
|
1818

19-
error[E0004]: non-exhaustive patterns: `(U8AsBool { n: 0_u8 }, false)`, `(U8AsBool { b: false }, false)`, `(U8AsBool { n: 0_u8 }, false)` and 1 more not covered
19+
error[E0004]: non-exhaustive patterns: `(U8AsBool { n: 0_u8 }, false)`, `(U8AsBool { b: true }, false)`, `(U8AsBool { n: 0_u8 }, false)` and 1 more not covered
2020
--> $DIR/unions.rs:28:15
2121
|
2222
LL | match (x, true) {
23-
| ^^^^^^^^^ patterns `(U8AsBool { n: 0_u8 }, false)`, `(U8AsBool { b: false }, false)`, `(U8AsBool { n: 0_u8 }, false)` and 1 more not covered
23+
| ^^^^^^^^^ patterns `(U8AsBool { n: 0_u8 }, false)`, `(U8AsBool { b: true }, false)`, `(U8AsBool { n: 0_u8 }, false)` and 1 more not covered
2424
|
2525
= note: the matched value is of type `(U8AsBool, bool)`
2626
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown, or multiple match arms

0 commit comments

Comments
 (0)