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

Misleading compiler suggestion: Swapp the equality in rustlib due to mismatched types in user code #139050

Open
jjl9807 opened this issue Mar 28, 2025 · 2 comments · May be fixed by #139403
Open
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jjl9807
Copy link

jjl9807 commented Mar 28, 2025

Code

use std::fmt::Debug;

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

fn main() {}

Current output

error[E0412]: cannot find type `Item` in this scope
 --> .\test.rs:5:5
  |
5 |     Item: Eq + Debug,
  |     ^^^^ not found in this scope

error[E0308]: mismatched types
 --> .\test.rs:7:35
  |
7 |     debug_assert_eq!(iter.next(), Some(value));
  |                                   ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
  |
  = note: expected enum `Option<_>`
             found enum `Option<&_>`
  = note: `Option<&<I as Iterator>::Item>` implements `PartialEq<Option<<I as Iterator>::Item>>`
help: consider swapping the equality
 --> C:\Users\jjl98\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\macros\mod.rs:46:22
  |
46|                 if !(*right_val == *left_val) {
  |                      ~~~~~~~~~~    ~~~~~~~~~

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0308, E0412.
For more information about an error, try `rustc --explain E0308`.

Desired output

Rationale and extra context

I'm not entirely sure what the compiler's output should be, but it is inappropriate for the compiler to suggest modifying rustlib when the issue lies in the user code. This suggestion is clearly flawed.

Other cases

Rust Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-pc-windows-msvc
release: 1.85.1
LLVM version: 19.1.7

Anything else?

No response

@jjl9807 jjl9807 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 Mar 28, 2025
@cyrgani
Copy link
Contributor

cyrgani commented Mar 28, 2025

simpler version:

pub fn foo()
where
    Foo: PartialEq,
{
    assert_eq!((), &());
}

fn main() {}

the malformed trait bound is required to cause the bad suggestion too for some reason.

@fmease fmease added A-trait-system Area: Trait system D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 4, 2025
@xizheyin
Copy link
Contributor

xizheyin commented Apr 4, 2025

@rustbot claim

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-trait-system Area: Trait system D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

4 participants