Skip to content

Commit ae15846

Browse files
authored
Rollup merge of rust-lang#116522 - bvanjoi:fix-115599, r=oli-obk
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`
2 parents 28644de + 3cc0b01 commit ae15846

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ impl<'tcx> ConstToPat<'tcx> {
197197
// All branches above emitted an error. Don't print any more lints.
198198
// The pattern we return is irrelevant since we errored.
199199
return Box::new(Pat { span: self.span, ty: cv.ty(), kind: PatKind::Wild });
200+
} else if let ty::Adt(..) = cv.ty().kind() && matches!(cv, mir::Const::Val(..)) {
201+
// This branch is only entered when the current `cv` is `mir::Const::Val`.
202+
// This is because `mir::Const::ty` has already been handled by `Self::recur`
203+
// and the invalid types may be ignored.
204+
let err = TypeNotStructural { span: self.span, non_sm_ty };
205+
self.tcx().sess.emit_err(err);
206+
return Box::new(Pat { span: self.span, ty: cv.ty(), kind: PatKind::Wild });
200207
} else if !self.saw_const_match_lint.get() {
201208
if let Some(mir_structural_match_violation) = mir_structural_match_violation {
202209
match non_sm_ty.kind() {

tests/ui/pattern/issue-115599.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const CONST_STRING: String = String::new();
2+
3+
fn main() {
4+
let empty_str = String::from("");
5+
if let CONST_STRING = empty_str {}
6+
//~^ ERROR to use a constant of type `Vec<u8>` in a pattern, `Vec<u8>` must be annotated with `#[derive(PartialEq, Eq)]`
7+
//~| WARN irrefutable `if let` pattern
8+
}

tests/ui/pattern/issue-115599.stderr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: to use a constant of type `Vec<u8>` in a pattern, `Vec<u8>` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/issue-115599.rs:5:12
3+
|
4+
LL | if let CONST_STRING = empty_str {}
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
9+
10+
warning: irrefutable `if let` pattern
11+
--> $DIR/issue-115599.rs:5:8
12+
|
13+
LL | if let CONST_STRING = empty_str {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: this pattern will always match, so the `if let` is useless
17+
= help: consider replacing the `if let` with a `let`
18+
= note: `#[warn(irrefutable_let_patterns)]` on by default
19+
20+
error: aborting due to previous error; 1 warning emitted
21+

0 commit comments

Comments
 (0)