Open
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.
> 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