Skip to content

Commit 2f5c0f5

Browse files
committed
emit err when using trait objects in pat
1 parent 9f34b82 commit 2f5c0f5

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/librustc_mir_build/hair/pattern/const_to_pat.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,21 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
111111
}
112112

113113
if let Some(non_sm_ty) = structural {
114-
let adt_def = match non_sm_ty {
115-
traits::NonStructuralMatchTy::Adt(adt_def) => adt_def,
116-
traits::NonStructuralMatchTy::Param => {
117-
bug!("use of constant whose type is a parameter inside a pattern")
114+
let msg = match non_sm_ty {
115+
traits::NonStructuralMatchTy::Adt(adt_def) => {
116+
let path = self.tcx().def_path_str(adt_def.did);
117+
format!(
118+
"to use a constant of type `{}` in a pattern, \
119+
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
120+
path, path,
121+
)
118122
}
119123
traits::NonStructuralMatchTy::Dynamic => {
120-
bug!("use of a trait object inside a pattern")
124+
format!("trait objects cannot be used in patterns")
125+
}
126+
traits::NonStructuralMatchTy::Param => {
127+
bug!("use of constant whose type is a parameter inside a pattern")
121128
}
122-
};
123-
let path = self.tcx().def_path_str(adt_def.did);
124-
125-
let make_msg = || -> String {
126-
format!(
127-
"to use a constant of type `{}` in a pattern, \
128-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
129-
path, path,
130-
)
131129
};
132130

133131
// double-check there even *is* a semantic `PartialEq` to dispatch to.
@@ -158,13 +156,13 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
158156

159157
if !ty_is_partial_eq {
160158
// span_fatal avoids ICE from resolution of non-existent method (rare case).
161-
self.tcx().sess.span_fatal(self.span, &make_msg());
159+
self.tcx().sess.span_fatal(self.span, &msg);
162160
} else if mir_structural_match_violation {
163161
self.tcx().struct_span_lint_hir(
164162
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
165163
self.id,
166164
self.span,
167-
|lint| lint.build(&make_msg()).emit(),
165+
|lint| lint.build(&msg).emit(),
168166
);
169167
} else {
170168
debug!(

src/test/ui/const-generics/issues/issue-63322-forbid-dyn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ error[E0741]: `&'static (dyn A + 'static)` must be annotated with `#[derive(Part
1212
LL | fn test<const T: &'static dyn A>() {
1313
| ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq`
1414

15-
error: aborting due to previous error
15+
error: aborting due to previous error; 1 warning emitted
1616

1717
For more information about this error, try `rustc --explain E0741`.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const F: &'static dyn Send = &7u32;
2+
3+
fn main() {
4+
let a: &dyn Send = &7u32;
5+
match a {
6+
F => panic!(),
7+
//~^ ERROR trait objects cannot be used in patterns
8+
_ => {}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: trait objects cannot be used in patterns
2+
--> $DIR/issue-70972-dyn-trait.rs:6:9
3+
|
4+
LL | F => panic!(),
5+
| ^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)