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

Remove the unhelpful let binding diag comes from FormatArguments #114511

Merged
merged 1 commit into from
Sep 6, 2023

Conversation

chenyukang
Copy link
Member

Fixes #114374

@rustbot
Copy link
Collaborator

rustbot commented Aug 5, 2023

r? @fee1-dead

(rustbot has picked a reviewer for you, use r? to override)

@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 Aug 5, 2023
@fee1-dead
Copy link
Member

Not entirely sure if this is what should happen.

r? compiler

@rustbot rustbot assigned b-naber and unassigned fee1-dead Aug 6, 2023
if self.span == expr.span.source_callsite() {
self.found = self.current;
if self.prop_expr.is_none() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we have nested format_args expressions?

Do we only care about the case where we assign the result of format_args!? If so, why can't we just check for stmt being an assignment where the rhs expr is_format_args_item?

Copy link
Member Author

Choose a reason for hiding this comment

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

the if self.span == expr.span.source_callsite() guarantee we find out the expr we're intrested in, the code goes here means a borrow(mostly binding) happens, I think use span to find expr is mostly enough for suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

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

What does your branch output on something like this: let x = format_args!("a {} {} {}.", 1, format_args!("b{}!", 2), 3)? Can you maybe add this to your test?

Copy link
Member Author

@chenyukang chenyukang Aug 23, 2023

Choose a reason for hiding this comment

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

The output is:

error[E0716]: temporary value dropped while borrowed
 --> tests/ui/borrowck/issue-114374-invalid-help-fmt-args.rs:5:13
  |
5 |     let x = format_args!("a {} {} {}.", 1, format_args!("b{}!", 2), 3);
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- temporary value is freed at the end of this statement
  |             |
  |             creates a temporary value which is freed while still in use
6 |     bar(x);
  |         - borrow later used here
  |
  = note: the result of `format_args!` can only be assigned directly if no placeholders in it's arguments are used
  = note: to learn more, visit <https://doc.rust-lang.org/std/macro.format_args.html>
  = note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

I added it to the test case.

if visitor.found == 0
&& !is_format_arguments_item
Copy link
Contributor

Choose a reason for hiding this comment

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

If we already special-case for FormatArguments, can't we also provide a more helpful diagnostic, which explains why this fails, instead of just getting rid of the binding suggestion? Note that we also still have this note: = note: consider using a let binding to create a longer lived value, so it might make sense to special case this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point!
I add a special note for format_args!, I'm not sure whether the words are accurate enough, seems it's hard to explain 😂 , so I added a link to the document.

@chenyukang chenyukang force-pushed the yukang-fix-114374-fmt-args branch 2 times, most recently from 397b91a to 116ef45 Compare August 18, 2023 09:40
@b-naber
Copy link
Contributor

b-naber commented Sep 6, 2023

I'm sorry that I didn't respond. I missed the notifications for your replies, so thought you hadn't replied.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 6, 2023

📌 Commit 1f107b1 has been approved by b-naber

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-review Status: Awaiting review from the assignee but also interested parties. labels Sep 6, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 6, 2023
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#114511 (Remove the unhelpful let binding diag comes from FormatArguments)
 - rust-lang#115473 (Add explanatory note to 'expected item' error)
 - rust-lang#115574 (Replace `rustc_data_structures` dependency with `rustc_index` in `rustc_parse_format`)
 - rust-lang#115578 (Clarify cryptic comments)
 - rust-lang#115587 (fix rust-lang#115348)
 - rust-lang#115596 (A small change)
 - rust-lang#115598 (Fix log formatting in bootstrap)
 - rust-lang#115605 (Better Debug for `Ty` in smir)
 - rust-lang#115614 (Fix minor grammar typo)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b046541 into rust-lang:master Sep 6, 2023
11 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 6, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 6, 2023
Rollup merge of rust-lang#114511 - chenyukang:yukang-fix-114374-fmt-args, r=b-naber

Remove the unhelpful let binding diag comes from FormatArguments

Fixes rust-lang#114374
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.

borrow error gives unhelpful let binding suggestion
5 participants