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

Suggest a borrow when using dbg #120990

Merged
merged 1 commit into from
Jul 16, 2024
Merged

Conversation

chenyukang
Copy link
Member

Fixes #120327
r? @estebank

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 12, 2024
@bors
Copy link
Contributor

bors commented Mar 27, 2024

☔ The latest upstream changes (presumably #123108) made this pull request unmergeable. Please resolve the merge conflicts.

@chenyukang
Copy link
Member Author

@rustbot ready

@chenyukang
Copy link
Member Author

@estebank, any comments on this PR?

@wesleywiser
Copy link
Member

r? rust-lang/compiler

@rustbot rustbot assigned oli-obk and unassigned estebank Jun 13, 2024
Comment on lines 521 to 523
// it's useless to suggest inserting `ref` when the span comes from std library
// anyway, user can not modify std library in most cases, so let's keep it quite?
if !from_std {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll want to forbid the suggestion whenever it points anywhere but to local code, the standard library is just the most common external crate in our test suite, but the issue probably exists for any dependency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it to:

    && !move_span.is_dummy()
    && !self.infcx.tcx.sess.source_map().is_imported(move_span)

Comment on lines 549 to 552
// only suggest for macro
if move_span.source_callsite() == move_span {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? How do the diagnostics look without macros and could they improved by this, too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't limit only for macro, it will also add suggestion for this code:

fn s() -> String {
    let a = String::new();
    let _b = match a {
        tmp => {
            eprintln!("dbg: {}", tmp);
            tmp
        }
    };
    return a;
}

with diagnostic:

 --> ./t/ha.rs:15:12
   |
7  |     let a = String::new();
   |         - move occurs because `a` has type `String`, which does not implement the `Copy` trait
...
10 |         tmp => {
   |         --- value moved here
...
15 |     return a; //~ ERROR use of moved value:
   |            ^ value used here after move
   |
help: consider borrowing instead of transferring ownership
   |
9  |     let _b = match &a {
   |                    +
help: borrow this binding in the pattern to avoid moving the value
   |
10 |         ref tmp => {
   |         +++

Seems it's also a good one.

I will add it into testcase.

Comment on lines 554 to 531
let arg_code = if let Some(span) = span
&& let Ok(code) = sm.span_to_snippet(span)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer not to be using snippets for making decisions. Do we not have the expression of the value somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems no, I just have a try to use ExpressionFinder to get the hir, and it only can not handle this scenario:

https://github.com/rust-lang/rust/pull/120990/files#diff-bd7f956e58a376780df31f75794e7af9b5d4043582aed6f129ae386a2b83ebdeR13-R17

the argument comes from the function parameters, since an ExpressionFinder will search in the body.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check self.body.var_debug_info for any entry for place, that gives you a name. Not perfect, ideally we'd have an Ident with expansion information, but this is better than span_to_snippet imo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, fixed the code.

@chenyukang chenyukang force-pushed the yukang-fix-120327-dbg branch 3 times, most recently from b31f9b9 to 6ff8a84 Compare June 23, 2024 18:29
@chenyukang
Copy link
Member Author

@rustbot ready
I could not find a better way without using snippets.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, forgot to send the review I wrote recently

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs Outdated Show resolved Hide resolved
Comment on lines 554 to 531
let arg_code = if let Some(span) = span
&& let Ok(code) = sm.span_to_snippet(span)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check self.body.var_debug_info for any entry for place, that gives you a name. Not perfect, ideally we'd have an Ident with expansion information, but this is better than span_to_snippet imo

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 4, 2024
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-120327-dbg branch 2 times, most recently from b01c541 to 1767432 Compare July 4, 2024 13:36
@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2024

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 15, 2024

📌 Commit 6ff8a84 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 15, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 15, 2024
…r=oli-obk

Suggest a borrow when using dbg

Fixes rust-lang#120327
r? `@estebank`
@matthiaskrgr
Copy link
Member

@bors r- needs rebase + build fix
#127775 (comment)

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 15, 2024
@chenyukang
Copy link
Member Author

rebased.

@matthiaskrgr
Copy link
Member

thx!
@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Jul 15, 2024

📌 Commit 3179526 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 15, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 15, 2024
…r=oli-obk

Suggest a borrow when using dbg

Fixes rust-lang#120327
r? `@estebank`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 15, 2024
…r=oli-obk

Suggest a borrow when using dbg

Fixes rust-lang#120327
r? ``@estebank``
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 15, 2024
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#120990 (Suggest a borrow when using dbg)
 - rust-lang#127047 (fix least significant digits of f128 associated constants)
 - rust-lang#127629 (Suggest using `map_or` when `Option<&T>::unwrap_or where T: Deref` fails)
 - rust-lang#127770 (Update books)
 - rust-lang#127780 (Make sure trait def ids match before zipping args in `note_function_argument_obligation`)

r? `@ghost`
`@rustbot` modify labels: rollup
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 16, 2024
…r=oli-obk

Suggest a borrow when using dbg

Fixes rust-lang#120327
r? ```@estebank```
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 16, 2024
Rollup of 5 pull requests

Successful merges:

 - rust-lang#120990 (Suggest a borrow when using dbg)
 - rust-lang#127047 (fix least significant digits of f128 associated constants)
 - rust-lang#127680 (Bootstrap command refactoring: port remaining commands with access to `Build` (step 6))
 - rust-lang#127770 (Update books)
 - rust-lang#127780 (Make sure trait def ids match before zipping args in `note_function_argument_obligation`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 16, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#120990 (Suggest a borrow when using dbg)
 - rust-lang#127047 (fix least significant digits of f128 associated constants)
 - rust-lang#127709 (match lowering: Move `MatchPair` tree creation to its own module)
 - rust-lang#127770 (Update books)
 - rust-lang#127780 (Make sure trait def ids match before zipping args in `note_function_argument_obligation`)
 - rust-lang#127795 (Fix typos in RELEASES.md)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 12fd2f9 into rust-lang:master Jul 16, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 16, 2024
Rollup merge of rust-lang#120990 - chenyukang:yukang-fix-120327-dbg, r=oli-obk

Suggest a borrow when using dbg

Fixes rust-lang#120327
r? ````@estebank````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest a borrow when using dbg!()
8 participants