about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBastian Kauschke <bastian_kauschke@hotmail.de>2020-04-11 21:02:49 +0200
committerBastian Kauschke <bastian_kauschke@hotmail.de>2020-04-29 19:29:10 +0200
commit9f34b82de203a01b7bb1afd57714886a65dbea8f (patch)
tree6baa941ec5460ce864d6ec471d7e14dffa139935
parent36d13cb01ba6a0a9b7c13ca2b9461a111cb3e395 (diff)
downloadrust-9f34b82de203a01b7bb1afd57714886a65dbea8f.tar.gz
rust-9f34b82de203a01b7bb1afd57714886a65dbea8f.zip
forbid `dyn Trait` in const generics
-rw-r--r--src/librustc_mir_build/hair/pattern/const_to_pat.rs3
-rw-r--r--src/librustc_trait_selection/traits/structural_match.rs5
-rw-r--r--src/test/ui/const-generics/issues/issue-63322-forbid-dyn.rs15
-rw-r--r--src/test/ui/const-generics/issues/issue-63322-forbid-dyn.stderr17
4 files changed, 40 insertions, 0 deletions
diff --git a/src/librustc_mir_build/hair/pattern/const_to_pat.rs b/src/librustc_mir_build/hair/pattern/const_to_pat.rs
index ad87afc3e03..5a3eea38775 100644
--- a/src/librustc_mir_build/hair/pattern/const_to_pat.rs
+++ b/src/librustc_mir_build/hair/pattern/const_to_pat.rs
@@ -116,6 +116,9 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
                     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);
 
diff --git a/src/librustc_trait_selection/traits/structural_match.rs b/src/librustc_trait_selection/traits/structural_match.rs
index fbe1fcb08f2..8007290f35d 100644
--- a/src/librustc_trait_selection/traits/structural_match.rs
+++ b/src/librustc_trait_selection/traits/structural_match.rs
@@ -11,6 +11,7 @@ use rustc_span::Span;
 pub enum NonStructuralMatchTy<'tcx> {
     Adt(&'tcx AdtDef),
     Param,
+    Dynamic,
 }
 
 /// This method traverses the structure of `ty`, trying to find an
@@ -137,6 +138,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
                 self.found = Some(NonStructuralMatchTy::Param);
                 return true; // Stop visiting.
             }
+            ty::Dynamic(..) => {
+                self.found = Some(NonStructuralMatchTy::Dynamic);
+                return true; // Stop visiting.
+            }
             ty::RawPtr(..) => {
                 // structural-match ignores substructure of
                 // `*const _`/`*mut _`, so skip `super_visit_with`.
diff --git a/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.rs b/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.rs
new file mode 100644
index 00000000000..2bacd6c9a9c
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.rs
@@ -0,0 +1,15 @@
+#![feature(const_generics)]
+//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
+
+trait A {}
+struct B;
+impl A for B {}
+
+fn test<const T: &'static dyn A>() {
+    //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]` to be used
+    unimplemented!()
+}
+
+fn main() {
+    test::<{ &B }>();
+}
diff --git a/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.stderr b/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.stderr
new file mode 100644
index 00000000000..73315af4483
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-63322-forbid-dyn.stderr
@@ -0,0 +1,17 @@
+warning: the feature `const_generics` is incomplete and may cause the compiler to crash
+  --> $DIR/issue-63322-forbid-dyn.rs:1:12
+   |
+LL | #![feature(const_generics)]
+   |            ^^^^^^^^^^^^^^
+   |
+   = note: `#[warn(incomplete_features)]` on by default
+
+error[E0741]: `&'static (dyn A + 'static)` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the type of a const parameter
+  --> $DIR/issue-63322-forbid-dyn.rs:8:18
+   |
+LL | fn test<const T: &'static dyn A>() {
+   |                  ^^^^^^^^^^^^^^ `&'static (dyn A + 'static)` doesn't derive both `PartialEq` and `Eq`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0741`.