Skip to content

Nightly regression in resolving trait impl with generic associated types #117646

Closed
@cpubot

Description

@cpubot

I tried this code:

trait MyIterator {
    type Item;
}

impl<T> MyIterator for Box<T>
where
    T: ?Sized + MyIterator,
{
    type Item = T::Item;
}

trait TraitWithIterator {
    type Iter<T>: MyIterator<Item = T>;
}

struct MyStruct;

impl TraitWithIterator for MyStruct {
    type Iter<T> = Box<dyn MyIterator<Item = T>>;
}

I expected to see this happen: Box<dyn MyIterator<Item = T>> is recognized as a valid instance of MyIterator<Item = T>, given the implementation of MyIterator for Box<T>.

The code compiles without issue on stable and nightly up to nightly-2023-11-03.

On nightly nightly-2023-11-04, it results in the following compiler error:

error[E0277]: the trait bound `(dyn MyIterator<Item = T> + 'static): MyIterator` is not satisfied
   --> src/lib.rs:301:20
    |
301 |     type Iter<T> = Box<dyn MyIterator<Item = T>>;
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MyIterator` is not implemented for `(dyn MyIterator<Item = T> + 'static)`
    |
    = help: the trait `MyIterator` is implemented for `std::boxed::Box<T>`
note: required for `std::boxed::Box<(dyn MyIterator<Item = T> + 'static)>` to implement `MyIterator`
   --> src/lib.rs:287:9
    |
287 | impl<T> MyIterator for Box<T>
    |         ^^^^^^^^^^     ^^^^^^
288 | where
289 |     T: ?Sized + MyIterator,
    |                 ---------- unsatisfied trait bound introduced here
note: required by a bound in `TraitWithIterator::Iter`
   --> src/lib.rs:295:30
    |
295 |     type Iter<T>: MyIterator<Item = T>;
    |                              ^^^^^^^^ required by this bound in `TraitWithIterator::Iter`

Meta

rustc --version --verbose:

rustc 1.75.0-nightly (1bb6553b9 2023-11-03)
binary: rustc
commit-hash: 1bb6553b967b69eed9ed8147e78b4f65cfc48e11
commit-date: 2023-11-03
host: aarch64-apple-darwin
release: 1.75.0-nightly
LLVM version: 17.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions