about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2023-03-07 23:37:52 +0000
committerMichael Goulet <michael@errs.io>2023-03-08 00:12:33 +0000
commit8a99ffc3442329d2fb2f07ce87852c3e12cc4d6a (patch)
tree852617af92a29f7c413e708136b98c87714f20db
parent1a521db67e2935e5a46c7b95b511ab9a43be5770 (diff)
downloadrust-8a99ffc3442329d2fb2f07ce87852c3e12cc4d6a.tar.gz
rust-8a99ffc3442329d2fb2f07ce87852c3e12cc4d6a.zip
Suppress copy impl error when post-normalized type references errors
-rw-r--r--compiler/rustc_trait_selection/src/traits/misc.rs7
-rw-r--r--tests/ui/coherence/illegal-copy-bad-projection.rs16
-rw-r--r--tests/ui/coherence/illegal-copy-bad-projection.stderr9
-rw-r--r--tests/ui/const-generics/bad-generic-in-copy-impl.rs9
-rw-r--r--tests/ui/const-generics/bad-generic-in-copy-impl.stderr9
5 files changed, 49 insertions, 1 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/misc.rs b/compiler/rustc_trait_selection/src/traits/misc.rs
index b94346b0956..336db4fee6c 100644
--- a/compiler/rustc_trait_selection/src/traits/misc.rs
+++ b/compiler/rustc_trait_selection/src/traits/misc.rs
@@ -87,7 +87,12 @@ pub fn type_allowed_to_implement_copy<'tcx>(
             };
             let ty = ocx.normalize(&normalization_cause, param_env, unnormalized_ty);
             let normalization_errors = ocx.select_where_possible();
-            if !normalization_errors.is_empty() {
+
+            // NOTE: The post-normalization type may also reference errors,
+            // such as when we project to a missing type or we have a mismatch
+            // between expected and found const-generic types. Don't report an
+            // additional copy error here, since it's not typically useful.
+            if !normalization_errors.is_empty() || ty.references_error() {
                 tcx.sess.delay_span_bug(field_span, format!("couldn't normalize struct field `{unnormalized_ty}` when checking Copy implementation"));
                 continue;
             }
diff --git a/tests/ui/coherence/illegal-copy-bad-projection.rs b/tests/ui/coherence/illegal-copy-bad-projection.rs
new file mode 100644
index 00000000000..797443a0abe
--- /dev/null
+++ b/tests/ui/coherence/illegal-copy-bad-projection.rs
@@ -0,0 +1,16 @@
+trait AsPtr {
+    type Ptr;
+}
+
+impl AsPtr for () {
+    type Ptr = *const void;
+    //~^ ERROR cannot find type `void` in this scope
+}
+
+#[derive(Copy, Clone)]
+struct Foo {
+    p: <() as AsPtr>::Ptr,
+    // Do not report a "`Copy` cannot be implemented" here.
+}
+
+fn main() {}
diff --git a/tests/ui/coherence/illegal-copy-bad-projection.stderr b/tests/ui/coherence/illegal-copy-bad-projection.stderr
new file mode 100644
index 00000000000..8fed9ba23b2
--- /dev/null
+++ b/tests/ui/coherence/illegal-copy-bad-projection.stderr
@@ -0,0 +1,9 @@
+error[E0412]: cannot find type `void` in this scope
+  --> $DIR/illegal-copy-bad-projection.rs:6:23
+   |
+LL |     type Ptr = *const void;
+   |                       ^^^^ not found in this scope
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0412`.
diff --git a/tests/ui/const-generics/bad-generic-in-copy-impl.rs b/tests/ui/const-generics/bad-generic-in-copy-impl.rs
new file mode 100644
index 00000000000..b5663464cf4
--- /dev/null
+++ b/tests/ui/const-generics/bad-generic-in-copy-impl.rs
@@ -0,0 +1,9 @@
+#[derive(Copy, Clone)]
+pub struct Foo {
+    x: [u8; SIZE],
+    //~^ ERROR mismatched types
+}
+
+const SIZE: u32 = 1;
+
+fn main() {}
diff --git a/tests/ui/const-generics/bad-generic-in-copy-impl.stderr b/tests/ui/const-generics/bad-generic-in-copy-impl.stderr
new file mode 100644
index 00000000000..25701ce68cc
--- /dev/null
+++ b/tests/ui/const-generics/bad-generic-in-copy-impl.stderr
@@ -0,0 +1,9 @@
+error[E0308]: mismatched types
+  --> $DIR/bad-generic-in-copy-impl.rs:3:13
+   |
+LL |     x: [u8; SIZE],
+   |             ^^^^ expected `usize`, found `u32`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.