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

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

Open
jjl9807 opened this issue Apr 2, 2025 · 2 comments · May be fixed by #139399
Open

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

jjl9807 opened this issue Apr 2, 2025 · 2 comments · May be fixed by #139399
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) 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 Apr 2, 2025

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

@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 Apr 2, 2025
@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 4, 2025
@fmease
Copy link
Member

fmease commented Apr 4, 2025

Potential fix for this category of issues: Suppress the suggestion if the span is .in_external_macro(map).

@xizheyin
Copy link
Contributor

xizheyin commented Apr 4, 2025

If so, I can repair it.

@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-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) 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.

3 participants