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

Account for macros when suggesting a new let binding #114178

Merged
merged 1 commit into from
Aug 2, 2023

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jul 28, 2023

Provide a structured suggestion when the expression comes from a macro expansion:

error[E0716]: temporary value dropped while borrowed
  --> $DIR/borrowck-let-suggestion.rs:2:17
   |
LL |     let mut x = vec![1].iter();
   |                 ^^^^^^^       - temporary value is freed at the end of this statement
   |                 |
   |                 creates a temporary value which is freed while still in use
LL |
LL |     x.use_mut();
   |     - borrow later used here
   |
   = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a `let` binding to create a longer lived value
   |
LL ~     let binding = vec![1];
LL ~     let mut x = binding.iter();
   |

@rustbot
Copy link
Collaborator

rustbot commented Jul 28, 2023

r? @petrochenkov

(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 Jul 28, 2023
@@ -2133,13 +2133,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
self.current -= 1;
}
fn visit_expr(&mut self, expr: &hir::Expr<'tcx>) {
if self.span == expr.span {
if self.span == expr.span.source_callsite() {
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain what this is actually doing? It's not immediately clear what the failure mode was previously before this PR.

Side-note, I wonder if this should be using Span::find_ancestor_inside instead of source_callsite, perhaps to deal with multiply nested macro exprs.

Copy link
Contributor Author

@estebank estebank Jul 28, 2023

Choose a reason for hiding this comment

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

source_callsite is recursive as well, from what I could tell.

When you have let x = vec![1, 2, 3].iter();, the span in the obligation corresponds to the expression inside the macro, which points into stdlib. By calling source_callsite we get the span to vec![1, 2, 3]. We then also account for it in the visitor. The reason it wasn't found before was mainly because we also checked that the span we saw was within the statement, if it wasn't we just bailed in line 2157.

Copy link
Member

@compiler-errors compiler-errors Jul 28, 2023

Choose a reason for hiding this comment

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

This may be purely academic, but the usage of Span::*_callsite functions (which are absolute, in a sense) instead of Span::find_ancestor_* (which are relative) means that we somewhat arbitrarily provide poorer suggestions for code like:

macro_rules! foo {
    () => {
        fn main() {
            let mut x = vec![1].iter();
            x.next();
        }
    };
}

foo!();

But whatever I guess. Just wanted to note that using source_callsite/etc is a tiny bit sketchy here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see. The fact that it recursively climbs is the problem you wanted to highlight. That's fair. I was thinking of the opposite case, where lets say vec is actually nested a few macros deep before actually expanding to an expression, where we wouldn't suggest the binding. I guess either one or the other works, until we start properly tracking the expression instead of trying to reconstruct things from a stray span.

@petrochenkov
Copy link
Contributor

Not sure what is the status here, given the discussion above.
@rustbot author

@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 31, 2023
@estebank
Copy link
Contributor Author

estebank commented Aug 1, 2023

@petrochenkov I would lean towards landing with the current approach, at the cost of not providing a structured suggestion within macros.

@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 1, 2023
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 2, 2023

📌 Commit 66d2379 has been approved by petrochenkov

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 Aug 2, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 2, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#114178 (Account for macros when suggesting a new let binding)
 - rust-lang#114199 (Don't unsize coerce infer vars in select in new solver)
 - rust-lang#114301 (Don't check unnecessarily that impl trait is RPIT)
 - rust-lang#114314 (Tweaks to `adt_sized_constraint`)
 - rust-lang#114322 (Fix invalid slice coercion suggestion reported in turbofish)
 - rust-lang#114340 ([rustc_attr][nit] Replace `filter` + `is_some` with `map_or`.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9cdca18 into rust-lang:master Aug 2, 2023
11 checks passed
@rustbot rustbot added this to the 1.73.0 milestone Aug 2, 2023
@bors
Copy link
Contributor

bors commented Aug 2, 2023

⌛ Testing commit 66d2379 with merge 7a5d2d0...

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.

5 participants