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

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

Closed
zzucch opened this issue Oct 15, 2024 · 0 comments · Fixed by #131795
Closed

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

zzucch opened this issue Oct 15, 2024 · 0 comments · Fixed by #131795
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zzucch
Copy link

zzucch commented Oct 15, 2024

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

@zzucch zzucch added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 15, 2024
@bors bors closed this as completed in 559f8ce Oct 20, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 20, 2024
Rollup merge of rust-lang#131795 - compiler-errors:expectation, r=Nadrieril

Stop inverting expectation in normalization errors

We have some funky special case logic to invert the expectation and actual type for normalization errors depending on their cause code. IMO most of the error messages get better, except for `try {}` blocks' type expectations. I think that these need to be special cased in some other way, rather than via this hack.

Fixes rust-lang#131763
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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.

1 participant