Skip to content

Commit 87313c9

Browse files
committed
use PatKind::wild when an ADT const value has violation
1 parent 5b88d65 commit 87313c9

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ 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+
let err = TypeNotStructural { span: self.span, non_sm_ty };
202+
self.tcx().sess.emit_err(err);
203+
return Box::new(Pat { span: self.span, ty: cv.ty(), kind: PatKind::Wild });
200204
} else if !self.saw_const_match_lint.get() {
201205
if let Some(mir_structural_match_violation) = mir_structural_match_violation {
202206
match non_sm_ty.kind() {

tests/ui/pattern/issue-115599.rs

+8
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

+21
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)