Skip to content

Commit 9cdca18

Browse files
authored
Rollup merge of #114178 - estebank:let-binding-macro, r=petrochenkov
Account for macros when suggesting a new let binding 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(); | ```
2 parents aa8462b + 66d2379 commit 9cdca18

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2133,13 +2133,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21332133
self.current -= 1;
21342134
}
21352135
fn visit_expr(&mut self, expr: &hir::Expr<'tcx>) {
2136-
if self.span == expr.span {
2136+
if self.span == expr.span.source_callsite() {
21372137
self.found = self.current;
21382138
}
21392139
walk_expr(self, expr);
21402140
}
21412141
}
21422142
let source_info = self.body.source_info(location);
2143+
let proper_span = proper_span.source_callsite();
21432144
if let Some(scope) = self.body.source_scopes.get(source_info.scope)
21442145
&& let ClearCrossCrate::Set(scope_data) = &scope.local_data
21452146
&& let Some(node) = self.infcx.tcx.hir().find(scope_data.lint_root)

tests/ui/borrowck/borrowck-borrowed-uniq-rvalue-2.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ LL | let x = defer(&vec!["Goodbye", "world!"]);
88
LL | x.x[0];
99
| ------ borrow later used here
1010
|
11-
= note: consider using a `let` binding to create a longer lived value
1211
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
help: consider using a `let` binding to create a longer lived value
13+
|
14+
LL ~ let binding = vec!["Goodbye", "world!"];
15+
LL ~ let x = defer(&binding);
16+
|
1317

1418
error: aborting due to previous error
1519

tests/ui/lifetimes/borrowck-let-suggestion.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ LL |
99
LL | x.use_mut();
1010
| - borrow later used here
1111
|
12-
= note: consider using a `let` binding to create a longer lived value
1312
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider using a `let` binding to create a longer lived value
14+
|
15+
LL ~ let binding = vec![1];
16+
LL ~ let mut x = binding.iter();
17+
|
1418

1519
error: aborting due to previous error
1620

tests/ui/pin-macro/lifetime_errors_on_promotion_misusage.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ LL |
99
LL | stuff(phantom_pinned)
1010
| -------------- borrow later used here
1111
|
12-
= note: consider using a `let` binding to create a longer lived value
1312
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: consider using a `let` binding to create a longer lived value
14+
|
15+
LL ~ let binding = pin!(PhantomPinned);
16+
LL ~ let phantom_pinned = identity(binding);
17+
|
1418

1519
error[E0716]: temporary value dropped while borrowed
1620
--> $DIR/lifetime_errors_on_promotion_misusage.rs:18:30

0 commit comments

Comments
 (0)