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

derive generates incorrect bounds for associated types #122531

Open
IsaacBreen opened this issue Mar 15, 2024 · 1 comment
Open

derive generates incorrect bounds for associated types #122531

IsaacBreen opened this issue Mar 15, 2024 · 1 comment
Labels
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

@IsaacBreen
Copy link

IsaacBreen commented Mar 15, 2024

I think there's a bug causing incorrect derive behaviour for a field whose type is an associated type extracted from a generic parameter.

trait AssociatedWithDefaultable {
    type Defaultable: Default;  // (1) `Defaultable` implements `Default`.
}

// (2) Expectation: `StoreAssociatedDefaultable` implements `Default` due to (3).
#[derive(Default)]
struct StoreAssociatedDefaultable<AWC: AssociatedWithDefaultable> {
    defaultable: AWC::Defaultable,  // (3) `Defaultable` implements `Default` due to (1).
}

fn get_default<A: AssociatedWithDefaultable>() -> StoreAssociatedDefaultable<A> {
    Default::default()
}
error[E0277]: the trait bound `A: Default` is not satisfied
  --> src/main.rs:12:5
   |
12 |     Default::default()
   |     ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `A`, which is required by `StoreAssociatedDefaultable<A>: Default`
   |
note: required for `StoreAssociatedDefaultable<A>` to implement `Default`
  --> src/main.rs:6:10
   |
6  | #[derive(Default)]
   |          ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
   = note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
   |
11 | fn get_default<A: AssociatedWithDefaultable + std::default::Default>() -> StoreAssociatedDefaultable<A> {
   |                                             +++++++++++++++++++++++


The macro expansion makes the problem clearer.

struct StoreAssociatedDefaultable<AWC: AssociatedWithDefaultable> {
    defaultable: AWC::Defaultable,
}
#[automatically_derived]
impl<AWC: ::core::default::Default + AssociatedWithDefaultable> ::core::default::Default
//        ^^^^^^^^^^^^^^^^^^^^^^^^ incorrect
for StoreAssociatedDefaultable<AWC>
where
    AWC::Defaultable: ::core::default::Default,
//                    ^^^^^^^^^^^^^^^^^^^^^^^^ correct but unnecessary
{
    #[inline]
    fn default() -> StoreAssociatedDefaultable<AWC> {
        StoreAssociatedDefaultable {
            defaultable: ::core::default::Default::default(),
        }
    }
}

It seems to confuse the associated type with the generic that carries it, causing it to add the bound AWC: ::core::default::Default unexpectedly.

It also adds AWC::Defaultable: ::core::default::Default, which is consistent but superfluous.

Link to playground

> rustc --version --verbose
rustc 1.78.0-nightly (3246e7951 2024-02-19)
binary: rustc
commit-hash: 3246e79513cb89ddbfc0f21cb5a877e5b321dcc5
commit-date: 2024-02-19
host: aarch64-apple-darwin
release: 1.78.0-nightly
LLVM version: 18.1.0
@IsaacBreen IsaacBreen added the C-bug Category: This is a bug. label Mar 15, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 15, 2024
@krtab
Copy link
Contributor

krtab commented Mar 15, 2024

I think this is a more intricate version of

use core::marker::PhantomData;

#[derive(Default)]
struct F<A> {
    ph: PhantomData<A>
}

struct X {}

fn f() -> F<X> {
    F::default()
}

which is expected #26925

@jieyouxu jieyouxu added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants