Skip to content

Commit

Permalink
Rollup merge of rust-lang#116522 - bvanjoi:fix-115599, r=oli-obk
Browse files Browse the repository at this point in the history
use `PatKind::wild` when an ADT const value has violation

Fixes rust-lang#115599

Since the [to_pat](https://github.com/rust-lang/rust/pull/111913/files#diff-6d8d99538aca600d633270051580c7a9e40b35824ea2863d9dda2c85a733b5d9R126-R155) behavior has been changed in the rust-lang#111913 update, the kind of `inlined_const_ast_pat` has transformed from `PatKind::Leaf { pattern: Pat { kind: Wild, ..} } ` to `PatKind::Constant`. This caused a scenario where there are no matched candidates, leading to a testing of the candidates. This process ultimately attempts to test the string const, triggering the `bug!` invocation finally.

r? `@oli-obk`
  • Loading branch information
matthiaskrgr authored Oct 11, 2023
2 parents 28644de + 3cc0b01 commit ae15846
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ impl<'tcx> ConstToPat<'tcx> {
// All branches above emitted an error. Don't print any more lints.
// The pattern we return is irrelevant since we errored.
return Box::new(Pat { span: self.span, ty: cv.ty(), kind: PatKind::Wild });
} else if let ty::Adt(..) = cv.ty().kind() && matches!(cv, mir::Const::Val(..)) {
// This branch is only entered when the current `cv` is `mir::Const::Val`.
// This is because `mir::Const::ty` has already been handled by `Self::recur`
// and the invalid types may be ignored.
let err = TypeNotStructural { span: self.span, non_sm_ty };
self.tcx().sess.emit_err(err);
return Box::new(Pat { span: self.span, ty: cv.ty(), kind: PatKind::Wild });
} else if !self.saw_const_match_lint.get() {
if let Some(mir_structural_match_violation) = mir_structural_match_violation {
match non_sm_ty.kind() {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/pattern/issue-115599.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const CONST_STRING: String = String::new();

fn main() {
let empty_str = String::from("");
if let CONST_STRING = empty_str {}
//~^ ERROR to use a constant of type `Vec<u8>` in a pattern, `Vec<u8>` must be annotated with `#[derive(PartialEq, Eq)]`
//~| WARN irrefutable `if let` pattern
}
21 changes: 21 additions & 0 deletions tests/ui/pattern/issue-115599.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: to use a constant of type `Vec<u8>` in a pattern, `Vec<u8>` must be annotated with `#[derive(PartialEq, Eq)]`
--> $DIR/issue-115599.rs:5:12
|
LL | if let CONST_STRING = empty_str {}
| ^^^^^^^^^^^^
|
= note: the traits must be derived, manual `impl`s are not sufficient
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details

warning: irrefutable `if let` pattern
--> $DIR/issue-115599.rs:5:8
|
LL | if let CONST_STRING = empty_str {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this pattern will always match, so the `if let` is useless
= help: consider replacing the `if let` with a `let`
= note: `#[warn(irrefutable_let_patterns)]` on by default

error: aborting due to previous error; 1 warning emitted

0 comments on commit ae15846

Please sign in to comment.