Skip to content

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

@jjl9807

Description

@jjl9807

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcT-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