rust-lang / rust-lang/rust

`derive` generates incorrect bounds for associated types

Open
#122,531 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-bug S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the associated-type case in src/main.rs using the linked playground example, then inspect the generated derive expansion shown in the issue. The fix is done when the derived implementation no longer requires AWC: Default for the generic parameter and the example compiles with only the associated-type constraint.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.