Skip to content

Tweak illegal Copy impl message #108884

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

Merged
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
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ hir_analysis_missing_type_params =
.note = because of the default `Self` reference, type parameters must be specified on object types

hir_analysis_copy_impl_on_type_with_dtor =
the trait `Copy` may not be implemented for this type; the type has a destructor
the trait `Copy` cannot be implemented for this type; the type has a destructor
.label = `Copy` not allowed on types with destructors

hir_analysis_multiple_relaxed_default_bounds =
type parameter has more than one relaxed default bound, only one is supported

hir_analysis_copy_impl_on_non_adt =
the trait `Copy` may not be implemented for this type
the trait `Copy` cannot be implemented for this type
.label = type is not a structure or enumeration

hir_analysis_const_impl_for_non_const_trait =
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! up data structures required by type-checking/codegen.

use crate::errors::{CopyImplOnNonAdt, CopyImplOnTypeWithDtor, DropImplOnWrongItem};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{struct_span_err, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -86,15 +87,22 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
tcx.sess,
span,
E0204,
"the trait `Copy` may not be implemented for this type"
"the trait `Copy` cannot be implemented for this type"
);

// We'll try to suggest constraining type parameters to fulfill the requirements of
// their `Copy` implementation.
let mut errors: BTreeMap<_, Vec<_>> = Default::default();
let mut bounds = vec![];

let mut seen_tys = FxHashSet::default();

for (field, ty, reason) in fields {
// Only report an error once per type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if more than one field isn't copy, it won't report all fields? am i understanding that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not highlight subsequent fields with identical types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh once per field type, got it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually unrelated to the example code you gave me, and was something that @fee1-dead had pointed out somewhere else a few days ago. It just came to mind when I was making the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was from @WaffleLapkin? Anyways thanks for working on this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, just remember seeing a screenshot of a really bad error message somewhere 🤣

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The screenshot in question:
2023-03-03_15-05

And ye, thanks for fixing this, errs <3

if !seen_tys.insert(ty) {
continue;
}

let field_span = tcx.def_span(field.did);
err.span_label(field_span, "this field does not implement `Copy`");

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ hir_typeck_field_multiply_specified_in_initializer =
.previous_use_label = first use of `{$ident}`

hir_typeck_copy_impl_on_type_with_dtor =
the trait `Copy` may not be implemented for this type; the type has a destructor
the trait `Copy` cannot be implemented for this type; the type has a destructor
.label = `Copy` not allowed on types with destructors

hir_typeck_multiple_relaxed_default_bounds =
type parameter has more than one relaxed default bound, only one is supported

hir_typeck_copy_impl_on_non_adt =
the trait `Copy` may not be implemented for this type
the trait `Copy` cannot be implemented for this type
.label = type is not a structure or enumeration

hir_typeck_trait_object_declared_with_no_traits =
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub trait StructuralEq {
/// attempt to derive a `Copy` implementation, we'll get an error:
///
/// ```text
/// the trait `Copy` may not be implemented for this type; field `points` does not implement `Copy`
/// the trait `Copy` cannot be implemented for this type; field `points` does not implement `Copy`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, this is just a documentation change.

/// ```
///
/// Shared references (`&T`) are also `Copy`, so a type can be `Copy`, even when it holds
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/coherence/coherence-impls-copy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ LL | impl Copy for [MyType] {}
|
= note: define and implement a trait or new type instead

error[E0206]: the trait `Copy` may not be implemented for this type
error[E0206]: the trait `Copy` cannot be implemented for this type
--> $DIR/coherence-impls-copy.rs:21:15
|
LL | impl Copy for &'static mut MyType {}
| ^^^^^^^^^^^^^^^^^^^ type is not a structure or enumeration

error[E0206]: the trait `Copy` may not be implemented for this type
error[E0206]: the trait `Copy` cannot be implemented for this type
--> $DIR/coherence-impls-copy.rs:25:15
|
LL | impl Copy for (MyType, MyType) {}
| ^^^^^^^^^^^^^^^^ type is not a structure or enumeration

error[E0206]: the trait `Copy` may not be implemented for this type
error[E0206]: the trait `Copy` cannot be implemented for this type
--> $DIR/coherence-impls-copy.rs:30:15
|
LL | impl Copy for [MyType] {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coherence/deep-bad-copy-reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'tcx, T> Clone for List<'tcx, T> {
}

impl<'tcx, T> Copy for List<'tcx, T> {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn assert_is_copy<T: Copy>() {}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coherence/deep-bad-copy-reason.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/deep-bad-copy-reason.rs:33:24
|
LL | pub struct List<'tcx, T>(Interned<'tcx, ListS<T>>);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0184.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0184]: the trait `Copy` may not be implemented for this type; the type has a destructor
error[E0184]: the trait `Copy` cannot be implemented for this type; the type has a destructor
--> $DIR/E0184.rs:1:10
|
LL | #[derive(Copy)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0206.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
struct Bar;

impl Copy for &'static mut Bar { }
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn main() {
}
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0206.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0206]: the trait `Copy` may not be implemented for this type
error[E0206]: the trait `Copy` cannot be implemented for this type
--> $DIR/E0206.rs:4:15
|
LL | impl Copy for &'static mut Bar { }
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/exclusive-drop-and-copy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// issue #20126

#[derive(Copy, Clone)] //~ ERROR the trait `Copy` may not be implemented
#[derive(Copy, Clone)] //~ ERROR the trait `Copy` cannot be implemented
struct Foo;

impl Drop for Foo {
fn drop(&mut self) {}
}

#[derive(Copy, Clone)] //~ ERROR the trait `Copy` may not be implemented
#[derive(Copy, Clone)] //~ ERROR the trait `Copy` cannot be implemented
struct Bar<T>(::std::marker::PhantomData<T>);

impl<T> Drop for Bar<T> {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/exclusive-drop-and-copy.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error[E0184]: the trait `Copy` may not be implemented for this type; the type has a destructor
error[E0184]: the trait `Copy` cannot be implemented for this type; the type has a destructor
--> $DIR/exclusive-drop-and-copy.rs:3:10
|
LL | #[derive(Copy, Clone)]
| ^^^^ `Copy` not allowed on types with destructors
|
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0184]: the trait `Copy` may not be implemented for this type; the type has a destructor
error[E0184]: the trait `Copy` cannot be implemented for this type; the type has a destructor
--> $DIR/exclusive-drop-and-copy.rs:10:10
|
LL | #[derive(Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-27340.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct Foo;
#[derive(Copy, Clone)]
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type
struct Bar(Foo);

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-27340.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/issue-27340.rs:2:10
|
LL | #[derive(Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/opt-in-copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct IWantToCopyThis {
}

impl Copy for IWantToCopyThis {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

enum CantCopyThisEither {
A,
Expand All @@ -17,6 +17,6 @@ enum IWantToCopyThisToo {
}

impl Copy for IWantToCopyThisToo {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/opt-in-copy.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/opt-in-copy.rs:7:15
|
LL | but_i_cant: CantCopyThis,
Expand All @@ -7,7 +7,7 @@ LL | but_i_cant: CantCopyThis,
LL | impl Copy for IWantToCopyThis {}
| ^^^^^^^^^^^^^^^

error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/opt-in-copy.rs:19:15
|
LL | ButICant(CantCopyThisEither),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/range/range_traits-2.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/range_traits-2.rs:3:10
|
LL | #[derive(Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/range/range_traits-3.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/range_traits-3.rs:3:10
|
LL | #[derive(Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/range/range_traits-6.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/range_traits-6.rs:3:10
|
LL | #[derive(Copy, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/span/E0204.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ struct Foo {
foo: Vec<u32>,
}

impl Copy for Foo { } //~ ERROR may not be implemented for this type
impl Copy for Foo { } //~ ERROR cannot be implemented for this type

#[derive(Copy)] //~ ERROR may not be implemented for this type
#[derive(Copy)] //~ ERROR cannot be implemented for this type
struct Foo2<'a> {
ty: &'a mut bool,
}
Expand All @@ -14,9 +14,9 @@ enum EFoo {
Baz,
}

impl Copy for EFoo { } //~ ERROR may not be implemented for this type
impl Copy for EFoo { } //~ ERROR cannot be implemented for this type

#[derive(Copy)] //~ ERROR may not be implemented for this type
#[derive(Copy)] //~ ERROR cannot be implemented for this type
enum EFoo2<'a> {
Bar(&'a mut bool),
Baz,
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/span/E0204.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/E0204.rs:5:15
|
LL | foo: Vec<u32>,
Expand All @@ -7,7 +7,7 @@ LL | foo: Vec<u32>,
LL | impl Copy for Foo { }
| ^^^

error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/E0204.rs:7:10
|
LL | #[derive(Copy)]
Expand All @@ -18,7 +18,7 @@ LL | ty: &'a mut bool,
|
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/E0204.rs:17:15
|
LL | Bar { x: Vec<u32> },
Expand All @@ -27,7 +27,7 @@ LL | Bar { x: Vec<u32> },
LL | impl Copy for EFoo { }
| ^^^^

error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/E0204.rs:19:10
|
LL | #[derive(Copy)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Vector2<T: Debug + Copy + Clone>{
pub y: T
}

#[derive(Debug, Copy, Clone)] //~ ERROR the trait `Copy` may not be implemented for this type
#[derive(Debug, Copy, Clone)] //~ ERROR the trait `Copy` cannot be implemented for this type
pub struct AABB<K: Copy + Debug>{
pub loc: Vector2<K>,
pub size: Vector2<K>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Vector2<T: Debug + Copy + Clone>{
pub y: T
}

#[derive(Debug, Copy, Clone)] //~ ERROR the trait `Copy` may not be implemented for this type
#[derive(Debug, Copy, Clone)] //~ ERROR the trait `Copy` cannot be implemented for this type
pub struct AABB<K: Copy>{
pub loc: Vector2<K>,
pub size: Vector2<K>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/missing-bound-in-derive-copy-impl-3.rs:10:17
|
LL | #[derive(Debug, Copy, Clone)]
| ^^^^
LL | pub struct AABB<K: Copy>{
LL | pub loc: Vector2<K>,
| ------------------- this field does not implement `Copy`
LL | pub size: Vector2<K>
| -------------------- this field does not implement `Copy`
|
note: the `Copy` impl for `Vector2<K>` requires that `K: Debug`
--> $DIR/missing-bound-in-derive-copy-impl-3.rs:12:14
|
LL | pub loc: Vector2<K>,
| ^^^^^^^^^^
LL | pub size: Vector2<K>
| ^^^^^^^^^^
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/missing-bound-in-derive-copy-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Vector2<T: Debug + Copy + Clone>{
pub y: T
}

#[derive(Debug, Copy, Clone)] //~ ERROR the trait `Copy` may not be implemented for this type
#[derive(Debug, Copy, Clone)] //~ ERROR the trait `Copy` cannot be implemented for this type
pub struct AABB<K>{
pub loc: Vector2<K>,
pub size: Vector2<K>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/missing-bound-in-derive-copy-impl.rs:9:17
|
LL | #[derive(Debug, Copy, Clone)]
| ^^^^
LL | pub struct AABB<K>{
LL | pub loc: Vector2<K>,
| ------------------- this field does not implement `Copy`
LL | pub size: Vector2<K>
| -------------------- this field does not implement `Copy`
|
note: the `Copy` impl for `Vector2<K>` requires that `K: Debug`
--> $DIR/missing-bound-in-derive-copy-impl.rs:11:14
|
LL | pub loc: Vector2<K>,
| ^^^^^^^^^^
LL | pub size: Vector2<K>
| ^^^^^^^^^^
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `K`
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ impl<T: std::fmt::Display> Clone for OnlyCopyIfDisplay<T> {
impl<T: std::fmt::Display> Copy for OnlyCopyIfDisplay<T> {}

impl<S: std::fmt::Display> Copy for Wrapper<OnlyCopyIfDisplay<S>> {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ impl<T: std::fmt::Display> Clone for OnlyCopyIfDisplay<T> {
impl<T: std::fmt::Display> Copy for OnlyCopyIfDisplay<T> {}

impl<S> Copy for Wrapper<OnlyCopyIfDisplay<S>> {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0204]: the trait `Copy` may not be implemented for this type
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/missing-bound-in-manual-copy-impl-2.rs:16:18
|
LL | struct Wrapper<T>(T);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
struct Wrapper<T>(T);

impl<S: Copy> Copy for Wrapper<S> {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/suggestions/missing-bound-in-manual-copy-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
struct Wrapper<T>(T);

impl<S> Copy for Wrapper<S> {}
//~^ ERROR the trait `Copy` may not be implemented for this type
//~^ ERROR the trait `Copy` cannot be implemented for this type

fn main() {}
Loading