Skip to content

Error message flips expected and actual types in E0271 note #131763

Closed
@zzucch

Description

@zzucch

Code

use std::rc::Rc;

struct SomeOtherStruct<T> {
    data: Rc<T>,
}

struct SomeStruct<T> {
    data: Option<Rc<SomeOtherStruct<T>>>,
}

impl<T> SomeStruct<T> {
    pub fn iter(&self) -> impl Iterator<Item = Rc<T>> {
        SomeIterator {
            current: self.data.clone(),
        }
    }
}

struct SomeIterator<T> {
    current: Option<Rc<SomeOtherStruct<T>>>,
}

impl<T> Iterator for SomeIterator<T> {
    type Item = Rc<SomeOtherStruct<T>>;

    fn next(&mut self) -> Option<Self::Item> {
        unimplemented!()
    }
}

Current output

error[E0271]: expected `SomeIterator<T>` to be an iterator that yields `Rc<T>`, but it yields `Rc<SomeOtherStruct<T>>`
  --> src/lib.rs:12:27
   |
11 |   impl<T> SomeStruct<T> {
   |        - found this type parameter
12 |       pub fn iter(&self) -> impl Iterator<Item = Rc<T>> {
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<SomeIterator<T> as Iterator>::Item == Rc<T>`
13 | /         SomeIterator {
14 | |             current: self.data.clone(),
15 | |         }
   | |_________- return type was inferred to be `SomeIterator<T>` here
   |
note: expected this to be `Rc<SomeOtherStruct<T>>`
  --> src/lib.rs:24:17
   |
24 |     type Item = Rc<SomeOtherStruct<T>>;
   |                 ^^^^^^^^^^^^^^^^^^^^^^
   = note: expected struct `Rc<SomeOtherStruct<T>>`
              found struct `Rc<T>`

Desired output

error[E0271]: expected `SomeIterator<T>` to be an iterator that yields `Rc<T>`, but it yields `Rc<SomeOtherStruct<T>>`
  --> src/lib.rs:12:27
   |
11 |   impl<T> SomeStruct<T> {
   |        - found this type parameter
12 |       pub fn iter(&self) -> impl Iterator<Item = Rc<T>> {
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<SomeIterator<T> as Iterator>::Item == Rc<T>`
13 | /         SomeIterator {
14 | |             current: self.data.clone(),
15 | |         }
   | |_________- return type was inferred to be `SomeIterator<T>` here
   |
note: expected this to be `Rc<T>`
  --> src/lib.rs:24:17
   |
24 |     type Item = Rc<SomeOtherStruct<T>>;
   |                 ^^^^^^^^^^^^^^^^^^^^^^
   = note: expected struct `Rc<T>`
              found struct `Rc<SomeOtherStruct<T>>`

Rationale and extra context

The note section in error message for E0271 mixes up the expected and actual types. In the example, the error message incorrectly states that the iterator is expected to yield Rc<SomeOtherStruct> but actually yields Rc. It should state that the iterator is expected to yield Rc but actually yields Rc<SomeOtherStruct>.

The LSP highlighting for the error makes understanding the actual problem quite confusing
Image

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.81.0 (eeb90cda1 2024-09-04) (built from a source tarball)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.8

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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