Skip to content

Commit e4928d7

Browse files
committed
Use correct type in diagnostics again
1 parent 2bc54d4 commit e4928d7

File tree

4 files changed

+50
-53
lines changed

4 files changed

+50
-53
lines changed

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

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,42 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
8989
self.infcx.tcx
9090
}
9191

92-
fn search_for_structural_match_violation(
93-
&self,
94-
ty: Ty<'tcx>,
95-
) -> Option<traits::NonStructuralMatchTy<'tcx>> {
96-
traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty)
92+
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
93+
traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty).map(
94+
|non_sm_ty| {
95+
with_no_trimmed_paths(|| match non_sm_ty {
96+
traits::NonStructuralMatchTy::Adt(adt_def) => {
97+
let path = self.tcx().def_path_str(adt_def.did);
98+
format!(
99+
"to use a constant of type `{}` in a pattern, \
100+
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
101+
path, path,
102+
)
103+
}
104+
traits::NonStructuralMatchTy::Dynamic => {
105+
"trait objects cannot be used in patterns".to_string()
106+
}
107+
traits::NonStructuralMatchTy::Opaque => {
108+
"opaque types cannot be used in patterns".to_string()
109+
}
110+
traits::NonStructuralMatchTy::Generator => {
111+
"generators cannot be used in patterns".to_string()
112+
}
113+
traits::NonStructuralMatchTy::Closure => {
114+
"closures cannot be used in patterns".to_string()
115+
}
116+
traits::NonStructuralMatchTy::Param => {
117+
bug!("use of a constant whose type is a parameter inside a pattern")
118+
}
119+
traits::NonStructuralMatchTy::Projection => {
120+
bug!("use of a constant whose type is a projection inside a pattern")
121+
}
122+
traits::NonStructuralMatchTy::Foreign => {
123+
bug!("use of a value of a foreign type inside a pattern")
124+
}
125+
})
126+
},
127+
)
97128
}
98129

99130
fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {
@@ -135,39 +166,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
135166
return inlined_const_as_pat;
136167
}
137168

138-
if let Some(non_sm_ty) = structural {
139-
let msg = with_no_trimmed_paths(|| match non_sm_ty {
140-
traits::NonStructuralMatchTy::Adt(adt_def) => {
141-
let path = self.tcx().def_path_str(adt_def.did);
142-
format!(
143-
"to use a constant of type `{}` in a pattern, \
144-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
145-
path, path,
146-
)
147-
}
148-
traits::NonStructuralMatchTy::Dynamic => {
149-
"trait objects cannot be used in patterns".to_string()
150-
}
151-
traits::NonStructuralMatchTy::Opaque => {
152-
"opaque types cannot be used in patterns".to_string()
153-
}
154-
traits::NonStructuralMatchTy::Generator => {
155-
"generators cannot be used in patterns".to_string()
156-
}
157-
traits::NonStructuralMatchTy::Closure => {
158-
"closures cannot be used in patterns".to_string()
159-
}
160-
traits::NonStructuralMatchTy::Param => {
161-
bug!("use of a constant whose type is a parameter inside a pattern")
162-
}
163-
traits::NonStructuralMatchTy::Projection => {
164-
bug!("use of a constant whose type is a projection inside a pattern")
165-
}
166-
traits::NonStructuralMatchTy::Foreign => {
167-
bug!("use of a value of a foreign type inside a pattern")
168-
}
169-
});
170-
169+
if let Some(msg) = structural {
171170
if !self.type_may_have_partial_eq_impl(cv.ty) {
172171
// span_fatal avoids ICE from resolution of non-existent method (rare case).
173172
self.tcx().sess.span_fatal(self.span, &msg);
@@ -272,11 +271,9 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
272271
// `search_for_structural_match_violation` and then remove this condition.
273272
&& self.search_for_structural_match_violation(cv.ty).is_some() =>
274273
{
275-
let msg = format!(
276-
"to use a constant of type `{}` in a pattern, \
277-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
278-
cv.ty, cv.ty,
279-
);
274+
// Obtain the actual type that isn't annotated. If we just looked at `cv.ty` we
275+
// could get `Option<NonStructEq>`, even though `Option` is annotated with derive.
276+
let msg = self.search_for_structural_match_violation(cv.ty).unwrap();
280277
self.saw_const_match_error.set(true);
281278
if self.include_lint_checks {
282279
tcx.sess.span_err(self.span, &msg);
@@ -512,11 +509,11 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
512509
&& self.search_for_structural_match_violation(cv.ty).is_some()
513510
{
514511
self.saw_const_match_lint.set(true);
515-
let msg = format!(
516-
"to use a constant of type `{}` in a pattern, \
517-
the constant's initializer must be trivial or all types \
518-
in the constant must be annotated with `#[derive(PartialEq, Eq)]`",
519-
cv.ty,
512+
// Obtain the actual type that isn't annotated. If we just looked at `cv.ty` we
513+
// could get `Option<NonStructEq>`, even though `Option` is annotated with derive.
514+
let msg = self.search_for_structural_match_violation(cv.ty).unwrap().replace(
515+
"in a pattern,",
516+
"in a pattern, the constant's initializer must be trivial or",
520517
);
521518
tcx.struct_span_lint_hir(
522519
lint::builtin::NONTRIVIAL_STRUCTURAL_MATCH,

src/test/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: to use a constant of type `Foo` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
1+
warning: to use a constant of type `CustomEq` in a pattern, the constant's initializer must be trivial or `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
22
--> $DIR/custom-eq-branch-warn.rs:29:9
33
|
44
LL | BAR_BAZ => panic!(),

src/test/ui/consts/const_in_pattern/reject_non_partial_eq.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: to use a constant of type `Option<NoPartialEq>` in a pattern, `Option<NoPartialEq>` must be annotated with `#[derive(PartialEq, Eq)]`
1+
error: to use a constant of type `NoPartialEq` in a pattern, `NoPartialEq` must be annotated with `#[derive(PartialEq, Eq)]`
22
--> $DIR/reject_non_partial_eq.rs:28:9
33
|
44
LL | NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),

src/test/ui/consts/const_in_pattern/warn_corner_cases.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: to use a constant of type `Option<NoDerive>` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
1+
warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
22
--> $DIR/warn_corner_cases.rs:26:47
33
|
44
LL | match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
@@ -8,7 +8,7 @@ LL | match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
88
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
= note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
1010

11-
warning: to use a constant of type `Option<NoDerive>` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
11+
warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
1212
--> $DIR/warn_corner_cases.rs:32:47
1313
|
1414
LL | match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
@@ -17,7 +17,7 @@ LL | match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
1717
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1818
= note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
1919

20-
warning: to use a constant of type `Option<NoDerive>` in a pattern, the constant's initializer must be trivial or all types in the constant must be annotated with `#[derive(PartialEq, Eq)]`
20+
warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
2121
--> $DIR/warn_corner_cases.rs:38:47
2222
|
2323
LL | match None { Some(_) => panic!("whoops"), METHOD_CALL => dbg!(METHOD_CALL), };

0 commit comments

Comments
 (0)