Skip to content

Inappropriate suggestion when mismatched types occur in the dbg! macro #139253

@jjl9807

Description

@jjl9807

Code

#[derive(Clone, Debug)]
struct Point<T> {
    v: T,
}

fn main() {
    let a: Point<u8> = dbg!(Point { v: 42 });
    let b: Point<u8> = dbg!(&a);
}

Current output

error[E0308]: mismatched types
  --> test.rs:10:24
   |
10 |     let b: Point<u8> = dbg!(&a);
   |                        ^^^^^^^^ expected `Point<u8>`, found `&Point<u8>`
   |
   = note: expected struct `Point<_>`
           found reference `&Point<_>`
   = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using clone here
  --> /home/jl-jiang/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/macros.rs:367:20
   |
367|                 tmp.clone()
   |                    ++++++++

error: aborting due to 1 previous error

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

Desired output

error[E0308]: mismatched types
  --> test.rs:10:24
   |
10 |     let b: Point<u8> = dbg!(&a);
   |                        ^^^^^^^^ expected `Point<u8>`, found `&Point<u8>`
   |
   = note: expected struct `Point<_>`
           found reference `&Point<_>`
   = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using clone here
  --> test.rs:10:29
   |
10 |     let b: Point<u8> = dbg!(a.clone());
   |                               +++++++

error: aborting due to 1 previous error

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

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0-nightly (e2014e876 2025-04-01)
binary: rustc
commit-hash: e2014e876e3efaa69bf51c19579adb16c3df5f81
commit-date: 2025-04-01
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.1

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-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