Skip to content

"expected opaque type, found ..." when trait impl with type_alias_impl_trait #136142

Open
@MabaKalox

Description

@MabaKalox

I tried this code:

type U32Iter = impl Iterator<Item = u32>;

struct Foo {
    iter: U32Iter,
}

impl From<u32> for Foo {
    fn from(value: u32) -> Self {
        Self {
            iter: std::iter::once(value),
        }
    }
}

I expected to see this happen: Compiles

Instead, this happened:

12 |             iter: std::iter::once(value),
   |                   ^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found `Once<u32>`

Meta

rustc --version --verbose:

rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5
Backtrace

   Compiling smth v0.1.0 (/tmp/smth)
error[E0308]: mismatched types
  --> src/main.rs:12:19
   |
3  | type U32Iter = impl Iterator<Item = u32>;
   |                ------------------------- the expected opaque type
...
12 |             iter: std::iter::once(value),
   |                   ^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found `Once<u32>`
   |
   = note: expected opaque type `U32Iter`
                   found struct `std::iter::Once<u32>`
note: this item must have the opaque type in its signature in order to be able to register hidden types
  --> src/main.rs:10:8
   |
10 |     fn from(value: u32) -> Self {
   |        ^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `smth` (bin "smth") due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions