using impl Trait on a return type as part of a trait declaration results in a misleading error · Issue #92458 · rust-lang/rust (original) (raw)

I tried this code:

pub trait Test { fn into_iter(&self) -> impl Iterator<Item = Self> { todo!() // A proper implementation will also fail } }

I expected to see this happen: an error message stating that impl Trait is not allowed as part of trait declarations.

Instead, this happened: the following misleading error

error[E0562]: `impl Trait` not allowed outside of function and method return types
 --> src/main.rs:3:28
  |
3 |     fn into_iter(&self) -> impl Iterator<Item = Self> {
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0562`.

This error is misleading because it states that the problem is that the keyword impl is not being used as part of a return type, even though it is.

Meta

rustc --version --verbose:

rustc 1.59.0-nightly (efec54529 2021-12-04)
binary: rustc
commit-hash: efec545293b9263be9edfb283a7aa66350b3acbf
commit-date: 2021-12-04
host: x86_64-pc-windows-msvc
release: 1.59.0-nightly
LLVM version: 13.0.0

Backtrace

Produces the exact same error