Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail gracefully when const pattern is not structural match. #100240

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
// once indirect_structural_match is a full fledged error, this
// level of indirection can be eliminated

let inlined_const_as_pat = self.recur(cv, mir_structural_match_violation).unwrap();
let inlined_const_as_pat =
self.recur(cv, mir_structural_match_violation).unwrap_or_else(|_| Pat {
span: self.span,
ty: cv.ty(),
kind: Box::new(PatKind::Constant { value: cv }),
});

if self.include_lint_checks && !self.saw_const_match_error.get() {
// If we were able to successfully convert the const to some pat,
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/consts/const_in_pattern/incomplete-slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[derive(PartialEq)]
enum E {
A,
}

const E_SL: &[E] = &[E::A];

fn main() {
match &[][..] {
//~^ ERROR non-exhaustive patterns: `&_` not covered [E0004]
E_SL => {}
//~^ WARN to use a constant of type `E` in a pattern, `E` must be annotated with `#[derive(PartialEq, Eq)]`
//~| WARN this was previously accepted by the compiler but is being phased out
}
}
26 changes: 26 additions & 0 deletions src/test/ui/consts/const_in_pattern/incomplete-slice.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
warning: to use a constant of type `E` in a pattern, `E` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/incomplete-slice.rs:11:9
|
LL | E_SL => {}
| ^^^^
|
= note: `#[warn(indirect_structural_match)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>

error[E0004]: non-exhaustive patterns: `&_` not covered
--> $DIR/incomplete-slice.rs:9:11
|
LL | match &[][..] {
| ^^^^^^^ pattern `&_` not covered
|
= note: the matched value is of type `&[E]`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ E_SL => {}
LL + &_ => todo!()
|

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0004`.