Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress copy impl error when post-normalized type references errors #108883

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_trait_selection/src/traits/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/coherence/illegal-copy-bad-projection.rs
Original file line number Diff line number Diff line change
@@ -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() {}
9 changes: 9 additions & 0 deletions tests/ui/coherence/illegal-copy-bad-projection.stderr
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 9 additions & 0 deletions tests/ui/const-generics/bad-generic-in-copy-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[derive(Copy, Clone)]
pub struct Foo {
x: [u8; SIZE],
//~^ ERROR mismatched types
}

const SIZE: u32 = 1;

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/const-generics/bad-generic-in-copy-impl.stderr
Original file line number Diff line number Diff line change
@@ -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`.