Skip to content

using impl Trait on a return type as part of a trait declaration results in a misleading error #92458

Closed
@mbartelsm

Description

@mbartelsm

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

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler 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