Skip to content

Commit 7b8251e

Browse files
committed
Account for method call and indexing when looking for inner-most path in expression
1 parent c6111e8 commit 7b8251e

14 files changed

+40
-8
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
7777
if let Some(mut expr) = expr_finder.result {
7878
while let hir::ExprKind::AddrOf(_, _, inner)
7979
| hir::ExprKind::Unary(hir::UnOp::Deref, inner)
80-
| hir::ExprKind::Field(inner, _) = &expr.kind
80+
| hir::ExprKind::Field(inner, _)
81+
| hir::ExprKind::MethodCall(_, inner, _, _)
82+
| hir::ExprKind::Index(inner, _) = &expr.kind
8183
{
8284
expr = inner;
8385
}

tests/ui/box/leak-alloc.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0505]: cannot move out of `alloc` because it is borrowed
22
--> $DIR/leak-alloc.rs:26:10
33
|
4+
LL | let alloc = Alloc {};
5+
| ----- binding `alloc` declared here
46
LL | let boxed = Box::new_in(10, alloc.by_ref());
57
| -------------- borrow of `alloc` occurs here
68
LL | let theref = Box::leak(boxed);

tests/ui/dropck/drop-with-active-borrows-1.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0505]: cannot move out of `a` because it is borrowed
22
--> $DIR/drop-with-active-borrows-1.rs:4:10
33
|
4+
LL | let a = "".to_string();
5+
| - binding `a` declared here
46
LL | let b: Vec<&str> = a.lines().collect();
57
| --------- borrow of `a` occurs here
68
LL | drop(a);

tests/ui/generator/dropck.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0597]: `*cell` does not live long enough
22
--> $DIR/dropck.rs:10:40
33
|
4+
LL | let (mut gen, cell);
5+
| ---- binding `cell` declared here
6+
LL | cell = Box::new(RefCell::new(0));
47
LL | let ref_ = Box::leak(Box::new(Some(cell.borrow_mut())));
58
| ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
69
...

tests/ui/issues/issue-52126-assign-op-invariance.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0597]: `line` does not live long enough
22
--> $DIR/issue-52126-assign-op-invariance.rs:34:28
33
|
4+
LL | for line in vec!["123456789".to_string(), "12345678".to_string()] {
5+
| ---- binding `line` declared here
46
LL | let v: Vec<&str> = line.split_whitespace().collect();
57
| ^^^^^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
68
...

tests/ui/macros/format-args-temporaries-in-write.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0597]: `mutex` does not live long enough
22
--> $DIR/format-args-temporaries-in-write.rs:41:27
33
|
4+
LL | let mutex = Mutex;
5+
| ----- binding `mutex` declared here
46
LL | write!(Out, "{}", mutex.lock()) /* no semicolon */
57
| ^^^^^^^^^^^^
68
| |
@@ -16,6 +18,8 @@ LL | };
1618
error[E0597]: `mutex` does not live long enough
1719
--> $DIR/format-args-temporaries-in-write.rs:47:29
1820
|
21+
LL | let mutex = Mutex;
22+
| ----- binding `mutex` declared here
1923
LL | writeln!(Out, "{}", mutex.lock()) /* no semicolon */
2024
| ^^^^^^^^^^^^
2125
| |

tests/ui/match/issue-74050-end-span.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0597]: `arg` does not live long enough
44
LL | let _arg = match args.next() {
55
| ---- borrow later stored here
66
LL | Some(arg) => {
7+
| --- binding `arg` declared here
78
LL | match arg.to_str() {
89
| ^^^^^^^^^^^^ borrowed value does not live long enough
910
...

tests/ui/moves/move-fn-self-receiver.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ LL | fn use_pin_box_self(self: Pin<Box<Self>>) {}
7575
error[E0505]: cannot move out of `mut_foo` because it is borrowed
7676
--> $DIR/move-fn-self-receiver.rs:50:5
7777
|
78+
LL | let mut mut_foo = Foo;
79+
| ----------- binding `mut_foo` declared here
7880
LL | let ret = mut_foo.use_mut_self();
7981
| ---------------------- borrow of `mut_foo` occurs here
8082
LL | mut_foo;

tests/ui/nll/issue-54556-niconii.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0597]: `counter` does not live long enough
22
--> $DIR/issue-54556-niconii.rs:22:20
33
|
4+
LL | let counter = Mutex;
5+
| ------- binding `counter` declared here
6+
LL |
47
LL | if let Ok(_) = counter.lock() { }
58
| ^^^^^^^^^^^^^^
69
| |

tests/ui/span/borrowck-let-suggestion-suffixes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn f() {
88

99
{
1010
let young = ['y']; // statement 3
11+
//~^ NOTE binding `young` declared here
1112

1213
v2.push(&young[0]); // statement 4
1314
//~^ ERROR `young[_]` does not live long enough

tests/ui/span/borrowck-let-suggestion-suffixes.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0597]: `young[_]` does not live long enough
2-
--> $DIR/borrowck-let-suggestion-suffixes.rs:12:17
2+
--> $DIR/borrowck-let-suggestion-suffixes.rs:13:17
33
|
4+
LL | let young = ['y']; // statement 3
5+
| ----- binding `young` declared here
6+
...
47
LL | v2.push(&young[0]); // statement 4
58
| ^^^^^^^^^ borrowed value does not live long enough
69
...
@@ -11,7 +14,7 @@ LL | (v1, v2, v3, /* v4 is above. */ v5).use_ref();
1114
| -- borrow later used here
1215

1316
error[E0716]: temporary value dropped while borrowed
14-
--> $DIR/borrowck-let-suggestion-suffixes.rs:19:14
17+
--> $DIR/borrowck-let-suggestion-suffixes.rs:20:14
1518
|
1619
LL | v3.push(&id('x')); // statement 6
1720
| ^^^^^^^ - temporary value is freed at the end of this statement
@@ -28,7 +31,7 @@ LL ~ v3.push(&binding); // statement 6
2831
|
2932

3033
error[E0716]: temporary value dropped while borrowed
31-
--> $DIR/borrowck-let-suggestion-suffixes.rs:29:18
34+
--> $DIR/borrowck-let-suggestion-suffixes.rs:30:18
3235
|
3336
LL | v4.push(&id('y'));
3437
| ^^^^^^^ - temporary value is freed at the end of this statement
@@ -41,7 +44,7 @@ LL | v4.use_ref();
4144
= note: consider using a `let` binding to create a longer lived value
4245

4346
error[E0716]: temporary value dropped while borrowed
44-
--> $DIR/borrowck-let-suggestion-suffixes.rs:40:14
47+
--> $DIR/borrowck-let-suggestion-suffixes.rs:41:14
4548
|
4649
LL | v5.push(&id('z'));
4750
| ^^^^^^^ - temporary value is freed at the end of this statement

tests/ui/span/destructor-restrictions.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0597]: `*a` does not live long enough
22
--> $DIR/destructor-restrictions.rs:8:10
33
|
4+
LL | let a = Box::new(RefCell::new(4));
5+
| - binding `a` declared here
46
LL | *a.borrow() + 1
57
| ^^^^^^^^^^
68
| |

tests/ui/span/issue-23338-locals-die-before-temps-of-body.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0597]: `y` does not live long enough
22
--> $DIR/issue-23338-locals-die-before-temps-of-body.rs:10:5
33
|
4+
LL | let y = x;
5+
| - binding `y` declared here
46
LL | y.borrow().clone()
57
| ^^^^^^^^^^
68
| |
@@ -22,6 +24,8 @@ LL | let x = y.borrow().clone(); x
2224
error[E0597]: `y` does not live long enough
2325
--> $DIR/issue-23338-locals-die-before-temps-of-body.rs:17:9
2426
|
27+
LL | let y = x;
28+
| - binding `y` declared here
2529
LL | y.borrow().clone()
2630
| ^^^^^^^^^^
2731
| |

tests/ui/span/issue-40157.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ error[E0597]: `foo` does not live long enough
22
--> $DIR/issue-40157.rs:2:53
33
|
44
LL | {println!("{:?}", match { let foo = vec![1, 2]; foo.get(1) } { x => x });}
5-
| ^^^^^^^^^^ - `foo` dropped here while still borrowed
6-
| |
7-
| borrowed value does not live long enough
5+
| --- ^^^^^^^^^^ - `foo` dropped here while still borrowed
6+
| | |
7+
| | borrowed value does not live long enough
8+
| binding `foo` declared here
89

910
error: aborting due to previous error
1011

0 commit comments

Comments
 (0)