Skip to content

Commit 9f34b82

Browse files
committed
forbid dyn Trait in const generics
1 parent 36d13cb commit 9f34b82

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
@@ -116,6 +116,9 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
116116
traits::NonStructuralMatchTy::Param => {
117117
bug!("use of constant whose type is a parameter inside a pattern")
118118
}
119+
traits::NonStructuralMatchTy::Dynamic => {
120+
bug!("use of a trait object inside a pattern")
121+
}
119122
};
120123
let path = self.tcx().def_path_str(adt_def.did);
121124

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 must be annotated with `#[derive(PartialEq, Eq)]` to be used
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]: `&'static (dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
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)