Skip to content

Derive macros don't bound associated types consistently #139231

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
harudagondi opened this issue Apr 2, 2025 · 3 comments
Closed

Derive macros don't bound associated types consistently #139231

harudagondi opened this issue Apr 2, 2025 · 3 comments
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. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@harudagondi
Copy link
Contributor

I tried this code:

trait Bar { type Qux; }

#[derive(Clone)]
struct Foo<T>(T::Qux) where T: Bar;

#[derive(Clone)]
struct Zoo<T>(<T as Bar>::Qux) where T: Bar;

I expected to see this happen: No error.

Instead, this happened:

Error on Long only:

error[E0277]: the trait bound `<T as Bar>::Qux: Clone` is not satisfied
 --> src/lib.rs:8:16
  |
7 | #[derive(Clone)]
  |          ----- in this derive macro expansion
8 | struct Long<T>(<T as Bar>::Qux) where T: Bar;
  |                ^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `<T as Bar>::Qux`
  |
  = 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 1 previous error

Macro output:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
extern crate std;
trait Bar {
    type Qux;
}

struct Short<T>(T::Qux) where T: Bar;
#[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for Short<T> where T: Bar,
    T::Qux: ::core::clone::Clone {
    #[inline]
    fn clone(&self) -> Short<T> {
        Short(::core::clone::Clone::clone(&self.0))
    }
}

struct Long<T>(<T as Bar>::Qux) where T: Bar;
#[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for Long<T> where T: Bar {
    #[inline]
    fn clone(&self) -> Long<T> { Long(::core::clone::Clone::clone(&self.0)) }
}

Meta

Playground link.

Tested on both stable 1.85.1 and nightly 1.88.0-nightly (2025-03-31 0b45675).

@harudagondi harudagondi added the C-bug Category: This is a bug. label Apr 2, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 2, 2025
@moxian
Copy link
Contributor

moxian commented Apr 2, 2025

See also: #122531 and #26925
@rustbot label: +A-macros +S-has-mcve +T-compiler -needs-triage +A-associated-items

@rustbot rustbot added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-associated-items Area: Associated items (types, constants & functions) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 2, 2025
@theemathas
Copy link
Contributor

Duplicate of #50730

@lcnr
Copy link
Contributor

lcnr commented Apr 2, 2025

Thank you for opening this issue. Closing as duplicate

@lcnr lcnr closed this as completed Apr 2, 2025
@fmease fmease closed this as completed Apr 2, 2025
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. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants