emit err when using trait objects in pat
This commit is contained in:
parent
9f34b82de2
commit
2f5c0f59a9
4 changed files with 33 additions and 17 deletions
|
@ -111,23 +111,21 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(non_sm_ty) = structural {
|
if let Some(non_sm_ty) = structural {
|
||||||
let adt_def = match non_sm_ty {
|
let msg = match non_sm_ty {
|
||||||
traits::NonStructuralMatchTy::Adt(adt_def) => adt_def,
|
traits::NonStructuralMatchTy::Adt(adt_def) => {
|
||||||
traits::NonStructuralMatchTy::Param => {
|
|
||||||
bug!("use of constant whose type is a parameter inside a pattern")
|
|
||||||
}
|
|
||||||
traits::NonStructuralMatchTy::Dynamic => {
|
|
||||||
bug!("use of a trait object inside a pattern")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let path = self.tcx().def_path_str(adt_def.did);
|
let path = self.tcx().def_path_str(adt_def.did);
|
||||||
|
|
||||||
let make_msg = || -> String {
|
|
||||||
format!(
|
format!(
|
||||||
"to use a constant of type `{}` in a pattern, \
|
"to use a constant of type `{}` in a pattern, \
|
||||||
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
|
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
|
||||||
path, path,
|
path, path,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
traits::NonStructuralMatchTy::Dynamic => {
|
||||||
|
format!("trait objects cannot be used in patterns")
|
||||||
|
}
|
||||||
|
traits::NonStructuralMatchTy::Param => {
|
||||||
|
bug!("use of constant whose type is a parameter inside a pattern")
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// double-check there even *is* a semantic `PartialEq` to dispatch to.
|
// double-check there even *is* a semantic `PartialEq` to dispatch to.
|
||||||
|
@ -158,13 +156,13 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
||||||
|
|
||||||
if !ty_is_partial_eq {
|
if !ty_is_partial_eq {
|
||||||
// span_fatal avoids ICE from resolution of non-existent method (rare case).
|
// span_fatal avoids ICE from resolution of non-existent method (rare case).
|
||||||
self.tcx().sess.span_fatal(self.span, &make_msg());
|
self.tcx().sess.span_fatal(self.span, &msg);
|
||||||
} else if mir_structural_match_violation {
|
} else if mir_structural_match_violation {
|
||||||
self.tcx().struct_span_lint_hir(
|
self.tcx().struct_span_lint_hir(
|
||||||
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
|
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
|
||||||
self.id,
|
self.id,
|
||||||
self.span,
|
self.span,
|
||||||
|lint| lint.build(&make_msg()).emit(),
|
|lint| lint.build(&msg).emit(),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
debug!(
|
debug!(
|
||||||
|
|
|
@ -12,6 +12,6 @@ error[E0741]: `&'static (dyn A + 'static)` must be annotated with `#[derive(Part
|
||||||
LL | fn test<const T: &'static dyn A>() {
|
LL | fn test<const T: &'static dyn A>() {
|
||||||
| ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq`
|
| ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error; 1 warning emitted
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0741`.
|
For more information about this error, try `rustc --explain E0741`.
|
||||||
|
|
10
src/test/ui/match/issue-70972-dyn-trait.rs
Normal file
10
src/test/ui/match/issue-70972-dyn-trait.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
const F: &'static dyn Send = &7u32;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a: &dyn Send = &7u32;
|
||||||
|
match a {
|
||||||
|
F => panic!(),
|
||||||
|
//~^ ERROR trait objects cannot be used in patterns
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
8
src/test/ui/match/issue-70972-dyn-trait.stderr
Normal file
8
src/test/ui/match/issue-70972-dyn-trait.stderr
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
error: trait objects cannot be used in patterns
|
||||||
|
--> $DIR/issue-70972-dyn-trait.rs:6:9
|
||||||
|
|
|
||||||
|
LL | F => panic!(),
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue