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

Bogus suggestion on I::Item vs &I::Item mismatch #112104

Open
bugaevc opened this issue May 30, 2023 · 0 comments
Open

Bogus suggestion on I::Item vs &I::Item mismatch #112104

bugaevc opened this issue May 30, 2023 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bugaevc
Copy link

bugaevc commented May 30, 2023

Code

fn foo<I: Iterator>(mut iter: I, value: &I::Item) where I::Item: Eq + Debug {
    assert_eq!(iter.next(), Some(value));
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:4:5
  |
4 |     assert_eq!(iter.next(), Some(value));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
  |
  = note: expected enum `Option<<I as Iterator>::Item>`
             found enum `Option<&<I as Iterator>::Item>`
  = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider constraining the associated type `<I as Iterator>::Item` to `&<I as Iterator>::Item`
  |
3 | fn foo<I: Iterator<Item = &<I as Iterator>::Item>>(mut iter: I, value: &I::Item) where I::Item: Eq + Debug {
  |                   +++++++++++++++++++++++++++++++

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

Desired output

error: cannot compare Option<I::Item> to Option<&I::Item>
 --> src/lib.rs:4:5
  |
4 |     assert_eq!(iter.next(), Some(value));

help: consider using `.as_ref()` to convert `Option<I::Item>` to `Option<&I::Item>`:
  |
4 |     assert_eq!(iter.next().as_ref(), Some(value));
  |                           +++++++++

Rationale and extra context

Item = &Item obviously wouldn't work, the compiler should not suggest that. The proper fix here is to convert the first Option<I::Item> to an Option<&I::Item> by calling .as_ref() on it.

Other cases

No response

Anything else?

This is reduced from a real example where I was trying to check that the iterator emits the expected series of items:

fn assert_collect<I: IntoIterator>(iter: I, expected_items: &[I::Item]) where I::Item: Eq + Debug {
    let mut iter = iter.into_iter();
    for item in expected_items {
        assert_eq!(iter.next(), Some(item));
    }
    assert_eq!(iter.next(), None);
}
@bugaevc bugaevc 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 May 30, 2023
@compiler-errors compiler-errors self-assigned this May 30, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 9, 2023
…atch-tweaks, r=WaffleLapkin

Misc HIR typeck type mismatch tweaks

These are all intended to improve rust-lang#112104, but I couldn't get it to actually suggest adding `as_ref` to the LHS of the equality expr without some hacks that I may play around with some more.

Each commit's title should explain what it's doing except for perhaps the last one, which addresses the bogus suggestion on rust-lang#112104 itself.
@compiler-errors compiler-errors removed their assignment Jun 22, 2023
@jieyouxu jieyouxu added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels Mar 25, 2024
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants