Skip to content

Commit 3cc0b01

Browse files
committed
use PatKind::wild when an ADT const value has violation
1 parent 71704c4 commit 3cc0b01

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

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

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