Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MabaKalox opened this issue Jan 27, 2025 · 1 comment
Open
Labels
C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@MabaKalox
Copy link

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

@MabaKalox MabaKalox added the C-bug Category: This is a bug. label Jan 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 27, 2025
@MabaKalox
Copy link
Author

MabaKalox commented Jan 27, 2025

This happens only if trying to implement trait for struct with type_alias_impl_trait field, and this trait implementation would concertize type. For example, following code works:

#![feature(type_alias_impl_trait)]
type U32Iter = impl Iterator<Item = u32>;
struct Foo {
    iter: U32Iter,
}
impl Foo {
    fn bar(value: u32) -> Self {
        Self {
            iter: std::iter::once(value),
        }
    }
}
impl From<u32> for Foo {
    fn from(value: u32) -> Self {
        Self::bar(value)
    }
}

@jieyouxu jieyouxu added F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` T-types Relevant to the types team, which will review and decide on the PR/issue. labels Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants