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 1 commit
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
8 changes: 8 additions & 0 deletions 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 @@ -94,7 +95,14 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ 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
4 changes: 0 additions & 4 deletions tests/ui/suggestions/missing-bound-in-derive-copy-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ 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