rust-lang / rust-lang/rust

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

Open
#136,142 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-type_alias_impl_trait T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

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

Start by reproducing the minimal type_alias_impl_trait example with rustc 1.84.0 and investigate how the compiler handles the opaque type in the From trait implementation. Done means the example compiles as expected and the behavior is covered by a regression test.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.