Skip to content

Commit 3e8b82e

Browse files
committed
use PatKind::wild when an ADT const value has violation
1 parent 9d1e4b7 commit 3e8b82e

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
@@ -200,6 +200,13 @@ impl<'tcx> ConstToPat<'tcx> {
200200
value: mir::Const::Ty(ty::Const::new_error(self.tcx(), e, cv.ty())),
201201
};
202202
return Box::new(Pat { span: self.span, ty: cv.ty(), kind });
203+
} else if let ty::Adt(..) = cv.ty().kind() && matches!(cv, mir::Const::Val(..)) {
204+
// This branch is only entered when the current `cv` is `mir::Const::Val`.
205+
// This is because `mir::Const::ty` has already been handled by `Self::recur`
206+
// and the invalid types may be ignored.
207+
let err = TypeNotStructural { span: self.span, non_sm_ty };
208+
self.tcx().sess.emit_err(err);
209+
return Box::new(Pat { span: self.span, ty: cv.ty(), kind: PatKind::Wild });
203210
} else if !self.saw_const_match_lint.get() {
204211
if let Some(mir_structural_match_violation) = mir_structural_match_violation {
205212
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)