Skip to content

Commit fee1598

Browse files
committed
forbid dyn Trait in const generics
1 parent 32fb4dc commit fee1598

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

src/librustc_mir_build/hair/pattern/const_to_pat.rs

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
106106
traits::NonStructuralMatchTy::Param => {
107107
bug!("use of constant whose type is a parameter inside a pattern")
108108
}
109+
traits::NonStructuralMatchTy::Dynamic => {
110+
bug!("use of a trait object inside a pattern")
111+
}
109112
};
110113
let path = self.tcx().def_path_str(adt_def.did);
111114

src/librustc_trait_selection/traits/structural_match.rs

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_span::Span;
1111
pub enum NonStructuralMatchTy<'tcx> {
1212
Adt(&'tcx AdtDef),
1313
Param,
14+
Dynamic,
1415
}
1516

1617
/// This method traverses the structure of `ty`, trying to find an
@@ -137,6 +138,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
137138
self.found = Some(NonStructuralMatchTy::Param);
138139
return true; // Stop visiting.
139140
}
141+
ty::Dynamic(..) => {
142+
self.found = Some(NonStructuralMatchTy::Dynamic);
143+
return true; // Stop visiting.
144+
}
140145
ty::RawPtr(..) => {
141146
// structural-match ignores substructure of
142147
// `*const _`/`*mut _`, so skip `super_visit_with`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
3+
4+
trait A {}
5+
struct B;
6+
impl A for B {}
7+
8+
fn test<const T: &'static dyn A>() {
9+
//~^ ERROR the types of const generic parameters must derive `PartialEq` and `Eq`
10+
unimplemented!()
11+
}
12+
13+
fn main() {
14+
test::<{ &B }>();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/issue-63322-forbid-dyn.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error[E0741]: the types of const generic parameters must derive `PartialEq` and `Eq`
10+
--> $DIR/issue-63322-forbid-dyn.rs:8:18
11+
|
12+
LL | fn test<const T: &'static dyn A>() {
13+
| ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0741`.

0 commit comments

Comments
 (0)