Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Closed
mbartelsm opened this issue Dec 31, 2021 · 1 comment · Fixed by #92806
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mbartelsm
Copy link

mbartelsm commented Dec 31, 2021

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

@mbartelsm mbartelsm added the C-bug Category: This is a bug. label Dec 31, 2021
@compiler-errors
Copy link
Member

@rustbot label -C-bug +A-diagnostics +T-compiler +D-confusing
@rustbot claim

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Jan 1, 2022
@bors bors closed this as completed in 5c08c39 Feb 19, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants