Skip to content

Built-in derives don't work when specifying associated types with a fully qualified path. #133232

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

Closed
theemathas opened this issue Nov 20, 2024 · 1 comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.

Comments

@theemathas
Copy link
Contributor

Note: This issue was initially discovered by @lcnr, and mentioned at rust-lang/reference#540 (comment). However, I think that this is a bug in the derive macro, and not in the documentation. Therefore, I am filing this issue.

I tried this code:

trait Trait {
    type Assoc;
}

#[derive(Clone)]
struct Foo<T: Trait> {
    value: T::Assoc, // ok, gets a `T::Assoc: Clone` bound
}

#[derive(Clone)]
struct Bar<T: Trait> {
    value: <T as Trait>::Assoc, // error
}

#[derive(Clone)]
struct Baz<T: Trait> {
    value: <T>::Assoc, // error
}

I expected the code to compile. Instead, the Foo struct compiles fine, but the Bar and Baz structs don't compile, producing the following error:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `<T as Trait>::Assoc: Clone` is not satisfied
  --> src/lib.rs:12:5
   |
10 | #[derive(Clone)]
   |          ----- in this derive macro expansion
11 | struct Bar<T: Trait> {
12 |     value: <T as Trait>::Assoc, // error
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<T as Trait>::Assoc`
   |
   = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `<T as Trait>::Assoc: Clone` is not satisfied
  --> src/lib.rs:17:5
   |
15 | #[derive(Clone)]
   |          ----- in this derive macro expansion
16 | struct Baz<T: Trait> {
17 |     value: <T>::Assoc, // error
   |     ^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<T as Trait>::Assoc`
   |
   = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 2 previous errors

The same issue also happens when deriving Debug, Default, PartialEq, or Hash.

Meta

Reproduces on the playground on stable rust version 1.82.0, and on nightly rust version 1.84.0-nightly (2024-11-19 ee612c45f00391aff71e)

@theemathas theemathas added the C-bug Category: This is a bug. label Nov 20, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 20, 2024
@fmease
Copy link
Member

fmease commented Nov 20, 2024

Duplicate of #50730

@fmease fmease marked this as a duplicate of #50730 Nov 20, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 20, 2024
@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2024
@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-associated-items Area: Associated items (types, constants & functions) labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants