Skip to content

Commit b11b8d6

Browse files
authored
Rollup merge of #100240 - cjgillot:noice-structural-match, r=davidtwco
Fail gracefully when const pattern is not structural match. Fixes #82909
2 parents e6c9594 + aa031f9 commit b11b8d6

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
168168
// once indirect_structural_match is a full fledged error, this
169169
// level of indirection can be eliminated
170170

171-
let inlined_const_as_pat = self.recur(cv, mir_structural_match_violation).unwrap();
171+
let inlined_const_as_pat =
172+
self.recur(cv, mir_structural_match_violation).unwrap_or_else(|_| Pat {
173+
span: self.span,
174+
ty: cv.ty(),
175+
kind: Box::new(PatKind::Constant { value: cv }),
176+
});
172177

173178
if self.include_lint_checks && !self.saw_const_match_error.get() {
174179
// If we were able to successfully convert the const to some pat,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[derive(PartialEq)]
2+
enum E {
3+
A,
4+
}
5+
6+
const E_SL: &[E] = &[E::A];
7+
8+
fn main() {
9+
match &[][..] {
10+
//~^ ERROR non-exhaustive patterns: `&_` not covered [E0004]
11+
E_SL => {}
12+
//~^ WARN to use a constant of type `E` in a pattern, `E` must be annotated with `#[derive(PartialEq, Eq)]`
13+
//~| WARN this was previously accepted by the compiler but is being phased out
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: to use a constant of type `E` in a pattern, `E` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/incomplete-slice.rs:11:9
3+
|
4+
LL | E_SL => {}
5+
| ^^^^
6+
|
7+
= note: `#[warn(indirect_structural_match)]` on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
10+
11+
error[E0004]: non-exhaustive patterns: `&_` not covered
12+
--> $DIR/incomplete-slice.rs:9:11
13+
|
14+
LL | match &[][..] {
15+
| ^^^^^^^ pattern `&_` not covered
16+
|
17+
= note: the matched value is of type `&[E]`
18+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
19+
|
20+
LL ~ E_SL => {}
21+
LL + &_ => todo!()
22+
|
23+
24+
error: aborting due to previous error; 1 warning emitted
25+
26+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)