Skip to content

The compiler doesn't suggest borrowing dbg!(x) #121795

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

Closed
amab8901 opened this issue Feb 29, 2024 · 3 comments
Closed

The compiler doesn't suggest borrowing dbg!(x) #121795

amab8901 opened this issue Feb 29, 2024 · 3 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@amab8901
Copy link
Contributor

amab8901 commented Feb 29, 2024

    let x = "Hello".to_string();
    dbg!(x);
    println!("{x}");

The above code returns this following error:

borrow of moved value: `x`
value borrowed here after move

To solve this, you'd normally have to add a reference character like this:

dbg!(&x);

But I think that Rust compiler should be able to understand what i'm trying to do without me having to manually add a reference on the variable x. I think this could be achieved by making an implementation that follows a similar logic as the following pseudocode:

fn dbg_reference_checker<T>(
    x: T
) -> Result<(), DbgError> {
    if !error_w_moved_value {
        use_moved_value(x)?;
    } else if !error_with_reference {
        use_reference(x)?;
    } else {
        return Err(DbgError::CannotDebugTheVariable)
    }
}

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 29, 2024
@clubby789
Copy link
Contributor

dbg is just a regular macro, it can't do the high level analysis that would be needed to know if it needed to insert a borrow.

It's specifically designed to move the argument, so you can do, e.g.

some_function(dbg!(argument));

@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 29, 2024
@Noratrieb
Copy link
Member

The error message I'm getting is really silly though

error[E0382]: use of moved value: `s`
 --> src/main.rs:2:27
  |
2 | let s = vec![0]; dbg!(s); s;
  |     -            -------  ^ value used here after move
  |     |            |
  |     |            value moved here
  |     move occurs because `s` has type `Vec<i32>`, which does not implement the `Copy` trait
  |
help: borrow this binding in the pattern to avoid moving the value
 --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/macros.rs:364:13
  |
36|             ref tmp => {
  |             +++

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

So let's repurpose this issue, whose feature request will not be implemented as clubby showed, into a diagnostics issue where the compiler should at least suggest borrowing. I feel like I've seen that before, but not in my test.

@Noratrieb Noratrieb 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. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Feb 29, 2024
@Noratrieb Noratrieb changed the title Let dbg!(x) automatically reference the variable x to prevent error The compiler doesn't suggest borrowing dbg!(x) Feb 29, 2024
@fmease
Copy link
Member

fmease commented Mar 1, 2024

changed the title Let dbg!(x) automatically reference the variable x to prevent error The compiler doesn't suggest borrowing dbg!(x)

In this case closing as duplicate of #120327 which may be fixed by #120990.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 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. 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

6 participants