Skip to content

Commit 223674a

Browse files
committed
use PatKind::error when an ADT const value has violation
1 parent b0fedc0 commit 223674a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ impl<'tcx> ConstToPat<'tcx> {
198198
// We errored. Signal that in the pattern, so that follow up errors can be silenced.
199199
let kind = PatKind::Error(e);
200200
return Box::new(Pat { span: self.span, ty: cv.ty(), kind });
201+
} else if let ty::Adt(..) = cv.ty().kind() && matches!(cv, mir::Const::Val(..)) {
202+
// This branch is only entered when the current `cv` is `mir::Const::Val`.
203+
// This is because `mir::Const::ty` has already been handled by `Self::recur`
204+
// and the invalid types may be ignored.
205+
let err = TypeNotStructural { span: self.span, non_sm_ty };
206+
let e = self.tcx().sess.emit_err(err);
207+
let kind = PatKind::Error(e);
208+
return Box::new(Pat { span: self.span, ty: cv.ty(), kind });
201209
} else if !self.saw_const_match_lint.get() {
202210
if let Some(mir_structural_match_violation) = mir_structural_match_violation {
203211
match non_sm_ty.kind() {

tests/ui/pattern/issue-115599.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

tests/ui/pattern/issue-115599.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)