Skip to content

Commit 9e3f330

Browse files
authored
Rollup merge of #106897 - estebank:issue-99430, r=davidtwco
Tweak E0597 CC #99430
2 parents f21728f + 7b8251e commit 9e3f330

File tree

230 files changed

+853
-354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

230 files changed

+853
-354
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
3737
desc,
3838
);
3939

40-
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
40+
err.span_label(borrow_span, format!("{} is borrowed here", borrow_desc));
4141
err.span_label(span, format!("use of borrowed {}", borrow_desc));
4242
err
4343
}
@@ -250,8 +250,8 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
250250
desc,
251251
);
252252

253-
err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
254-
err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
253+
err.span_label(borrow_span, format!("{} is borrowed here", desc));
254+
err.span_label(span, format!("{} is assigned to here but it was already borrowed", desc));
255255
err
256256
}
257257

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17361736
&self.local_names,
17371737
&mut err,
17381738
"",
1739-
None,
1739+
Some(borrow_span),
17401740
None,
17411741
);
17421742
}

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Print diagnostics to explain why values are borrowed.
22
33
use rustc_errors::{Applicability, Diagnostic};
4+
use rustc_hir as hir;
5+
use rustc_hir::intravisit::Visitor;
46
use rustc_index::vec::IndexVec;
57
use rustc_infer::infer::NllRegionVariableOrigin;
68
use rustc_middle::mir::{
@@ -11,6 +13,7 @@ use rustc_middle::ty::adjustment::PointerCast;
1113
use rustc_middle::ty::{self, RegionVid, TyCtxt};
1214
use rustc_span::symbol::{kw, Symbol};
1315
use rustc_span::{sym, DesugaringKind, Span};
16+
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;
1417

1518
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
1619
use crate::{
@@ -63,6 +66,36 @@ impl<'tcx> BorrowExplanation<'tcx> {
6366
borrow_span: Option<Span>,
6467
multiple_borrow_span: Option<(Span, Span)>,
6568
) {
69+
if let Some(span) = borrow_span {
70+
let def_id = body.source.def_id();
71+
if let Some(node) = tcx.hir().get_if_local(def_id)
72+
&& let Some(body_id) = node.body_id()
73+
{
74+
let body = tcx.hir().body(body_id);
75+
let mut expr_finder = FindExprBySpan::new(span);
76+
expr_finder.visit_expr(body.value);
77+
if let Some(mut expr) = expr_finder.result {
78+
while let hir::ExprKind::AddrOf(_, _, inner)
79+
| hir::ExprKind::Unary(hir::UnOp::Deref, inner)
80+
| hir::ExprKind::Field(inner, _)
81+
| hir::ExprKind::MethodCall(_, inner, _, _)
82+
| hir::ExprKind::Index(inner, _) = &expr.kind
83+
{
84+
expr = inner;
85+
}
86+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
87+
&& let [hir::PathSegment { ident, args: None, .. }] = p.segments
88+
&& let hir::def::Res::Local(hir_id) = p.res
89+
&& let Some(hir::Node::Pat(pat)) = tcx.hir().find(hir_id)
90+
{
91+
err.span_label(
92+
pat.span,
93+
&format!("binding `{ident}` declared here"),
94+
);
95+
}
96+
}
97+
}
98+
}
6699
match *self {
67100
BorrowExplanation::UsedLater(later_use_kind, var_or_use_span, path_span) => {
68101
let message = match later_use_kind {

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
448448
};
449449
self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span), "");
450450

451-
use_spans.args_span_label(err, format!("move out of {place_desc} occurs here"));
451+
use_spans.args_span_label(err, format!("{place_desc} is moved here"));
452452
}
453453
}
454454
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2825,7 +2825,7 @@ pub struct FindExprBySpan<'hir> {
28252825
}
28262826

28272827
impl<'hir> FindExprBySpan<'hir> {
2828-
fn new(span: Span) -> Self {
2828+
pub fn new(span: Span) -> Self {
28292829
Self { span, result: None, ty_result: None }
28302830
}
28312831
}

tests/ui-fulldeps/dropck-tarena-cycle-checked.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0597]: `arena` does not live long enough
22
--> $DIR/dropck-tarena-cycle-checked.rs:116:7
33
|
4+
LL | let arena = TypedArena::default();
5+
| ----- binding `arena` declared here
46
LL | f(&arena);
57
| ^^^^^^ borrowed value does not live long enough
68
LL | }

tests/ui-fulldeps/dropck-tarena-unsound-drop.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0597]: `arena` does not live long enough
22
--> $DIR/dropck-tarena-unsound-drop.rs:41:7
33
|
4+
LL | let arena: TypedArena<C> = TypedArena::default();
5+
| ----- binding `arena` declared here
46
LL | f(&arena);
57
| ^^^^^^ borrowed value does not live long enough
68
LL | }

tests/ui/asm/type-check-4.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0506]: cannot assign to `a` because it is borrowed
22
--> $DIR/type-check-4.rs:14:9
33
|
44
LL | let p = &a;
5-
| -- borrow of `a` occurs here
5+
| -- `a` is borrowed here
66
LL | asm!("{}", out(reg) a);
7-
| ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `a` occurs here
7+
| ^^^^^^^^^^^^^^^^^^^^^^ `a` is assigned to here but it was already borrowed
88
LL |
99
LL | println!("{}", p);
1010
| - borrow later used here
@@ -13,7 +13,7 @@ error[E0503]: cannot use `a` because it was mutably borrowed
1313
--> $DIR/type-check-4.rs:22:28
1414
|
1515
LL | let p = &mut a;
16-
| ------ borrow of `a` occurs here
16+
| ------ `a` is borrowed here
1717
LL | asm!("{}", in(reg) a);
1818
| ^ use of borrowed `a`
1919
LL |

tests/ui/associated-types/associated-types-outlives.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0505]: cannot move out of `x` because it is borrowed
22
--> $DIR/associated-types-outlives.rs:22:14
33
|
4+
LL | F: for<'a> FnOnce(<T as Foo<'a>>::Bar)>(x: T, f: F) {
5+
| - binding `x` declared here
6+
...
47
LL | 's: loop { y = denormalise(&x); break }
58
| -- borrow of `x` occurs here
69
LL | drop(x);

tests/ui/async-await/issue-74072-lifetime-name-annotations.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ error[E0506]: cannot assign to `*x` because it is borrowed
44
LL | pub async fn async_fn(x: &mut i32) -> &i32 {
55
| - let's call the lifetime of this reference `'1`
66
LL | let y = &*x;
7-
| --- borrow of `*x` occurs here
7+
| --- `*x` is borrowed here
88
LL | *x += 1;
9-
| ^^^^^^^ assignment to borrowed `*x` occurs here
9+
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
1010
LL | y
1111
| - returning this value requires that `*x` is borrowed for `'1`
1212

1313
error[E0506]: cannot assign to `*x` because it is borrowed
1414
--> $DIR/issue-74072-lifetime-name-annotations.rs:16:9
1515
|
1616
LL | let y = &*x;
17-
| --- borrow of `*x` occurs here
17+
| --- `*x` is borrowed here
1818
LL | *x += 1;
19-
| ^^^^^^^ assignment to borrowed `*x` occurs here
19+
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
2020
LL | y
2121
| - returning this value requires that `*x` is borrowed for `'1`
2222
LL | })()
@@ -28,19 +28,19 @@ error[E0506]: cannot assign to `*x` because it is borrowed
2828
LL | (async move || -> &i32 {
2929
| - let's call the lifetime of this reference `'1`
3030
LL | let y = &*x;
31-
| --- borrow of `*x` occurs here
31+
| --- `*x` is borrowed here
3232
LL | *x += 1;
33-
| ^^^^^^^ assignment to borrowed `*x` occurs here
33+
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
3434
LL | y
3535
| - returning this value requires that `*x` is borrowed for `'1`
3636

3737
error[E0506]: cannot assign to `*x` because it is borrowed
3838
--> $DIR/issue-74072-lifetime-name-annotations.rs:32:9
3939
|
4040
LL | let y = &*x;
41-
| --- borrow of `*x` occurs here
41+
| --- `*x` is borrowed here
4242
LL | *x += 1;
43-
| ^^^^^^^ assignment to borrowed `*x` occurs here
43+
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
4444
LL | y
4545
| - returning this value requires that `*x` is borrowed for `'1`
4646
LL | }

tests/ui/async-await/issue-75785-confusing-named-region.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ error[E0506]: cannot assign to `*x` because it is borrowed
44
LL | pub async fn async_fn(x: &mut i32) -> (&i32, &i32) {
55
| - let's call the lifetime of this reference `'1`
66
LL | let y = &*x;
7-
| --- borrow of `*x` occurs here
7+
| --- `*x` is borrowed here
88
LL | *x += 1;
9-
| ^^^^^^^ assignment to borrowed `*x` occurs here
9+
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
1010
LL | (&32, y)
1111
| -------- returning this value requires that `*x` is borrowed for `'1`
1212

tests/ui/async-await/multiple-lifetimes/ret-ref.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0506]: cannot assign to `a` because it is borrowed
22
--> $DIR/ret-ref.rs:16:5
33
|
44
LL | let future = multiple_named_lifetimes(&a, &b);
5-
| -- borrow of `a` occurs here
5+
| -- `a` is borrowed here
66
LL | a += 1;
7-
| ^^^^^^ assignment to borrowed `a` occurs here
7+
| ^^^^^^ `a` is assigned to here but it was already borrowed
88
LL | b += 1;
99
LL | let p = future.await;
1010
| ------ borrow later used here
@@ -13,21 +13,21 @@ error[E0506]: cannot assign to `b` because it is borrowed
1313
--> $DIR/ret-ref.rs:17:5
1414
|
1515
LL | let future = multiple_named_lifetimes(&a, &b);
16-
| -- borrow of `b` occurs here
16+
| -- `b` is borrowed here
1717
LL | a += 1;
1818
LL | b += 1;
19-
| ^^^^^^ assignment to borrowed `b` occurs here
19+
| ^^^^^^ `b` is assigned to here but it was already borrowed
2020
LL | let p = future.await;
2121
| ------ borrow later used here
2222

2323
error[E0506]: cannot assign to `a` because it is borrowed
2424
--> $DIR/ret-ref.rs:28:5
2525
|
2626
LL | let future = multiple_named_lifetimes(&a, &b);
27-
| -- borrow of `a` occurs here
27+
| -- `a` is borrowed here
2828
LL | let p = future.await;
2929
LL | a += 1;
30-
| ^^^^^^ assignment to borrowed `a` occurs here
30+
| ^^^^^^ `a` is assigned to here but it was already borrowed
3131
LL | b += 1;
3232
LL | drop(p);
3333
| - borrow later used here

tests/ui/augmented-assignments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl AddAssign for Int {
99
}
1010

1111
fn main() {
12-
let mut x = Int(1);
12+
let mut x = Int(1); //~ NOTE binding `x` declared here
1313
x
1414
//~^ NOTE borrow of `x` occurs here
1515
+=

tests/ui/augmented-assignments.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0505]: cannot move out of `x` because it is borrowed
22
--> $DIR/augmented-assignments.rs:16:5
33
|
4+
LL | let mut x = Int(1);
5+
| ----- binding `x` declared here
46
LL | x
57
| - borrow of `x` occurs here
68
...

tests/ui/binop/binop-move-semantics.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ LL | fn move_then_borrow<T: Add<Output=()> + Clone + Copy>(x: T) {
4141
error[E0505]: cannot move out of `x` because it is borrowed
4242
--> $DIR/binop-move-semantics.rs:21:5
4343
|
44+
LL | fn move_borrowed<T: Add<Output=()>>(x: T, mut y: T) {
45+
| - binding `x` declared here
4446
LL | let m = &x;
4547
| -- borrow of `x` occurs here
4648
...
@@ -53,6 +55,9 @@ LL | use_mut(n); use_imm(m);
5355
error[E0505]: cannot move out of `y` because it is borrowed
5456
--> $DIR/binop-move-semantics.rs:23:5
5557
|
58+
LL | fn move_borrowed<T: Add<Output=()>>(x: T, mut y: T) {
59+
| ----- binding `y` declared here
60+
LL | let m = &x;
5661
LL | let n = &mut y;
5762
| ------ borrow of `y` occurs here
5863
...

tests/ui/borrowck/borrow-tuple-fields.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0505]: cannot move out of `x` because it is borrowed
22
--> $DIR/borrow-tuple-fields.rs:12:13
33
|
4+
LL | let x: (Box<_>, _) = (Box::new(1), 2);
5+
| - binding `x` declared here
46
LL | let r = &x.0;
57
| ---- borrow of `x.0` occurs here
68
LL | let y = x;
@@ -32,6 +34,8 @@ LL | a.use_ref();
3234
error[E0505]: cannot move out of `x` because it is borrowed
3335
--> $DIR/borrow-tuple-fields.rs:28:13
3436
|
37+
LL | let x = Foo(Box::new(1), 2);
38+
| - binding `x` declared here
3539
LL | let r = &x.0;
3640
| ---- borrow of `x.0` occurs here
3741
LL | let y = x;

tests/ui/borrowck/borrowck-anon-fields-variant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0503]: cannot use `y` because it was mutably borrowed
22
--> $DIR/borrowck-anon-fields-variant.rs:16:19
33
|
44
LL | Foo::Y(ref mut a, _) => a,
5-
| --------- borrow of `y.0` occurs here
5+
| --------- `y.0` is borrowed here
66
...
77
LL | let b = match y {
88
| ^ use of borrowed `y.0`
@@ -14,7 +14,7 @@ error[E0503]: cannot use `y` because it was mutably borrowed
1414
--> $DIR/borrowck-anon-fields-variant.rs:34:19
1515
|
1616
LL | Foo::Y(ref mut a, _) => a,
17-
| --------- borrow of `y.0` occurs here
17+
| --------- `y.0` is borrowed here
1818
...
1919
LL | let b = match y {
2020
| ^ use of borrowed `y.0`

tests/ui/borrowck/borrowck-assign-comp.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ error[E0506]: cannot assign to `p.x` because it is borrowed
22
--> $DIR/borrowck-assign-comp.rs:10:5
33
|
44
LL | let q = &p;
5-
| -- borrow of `p.x` occurs here
5+
| -- `p.x` is borrowed here
66
...
77
LL | p.x = 5;
8-
| ^^^^^^^ assignment to borrowed `p.x` occurs here
8+
| ^^^^^^^ `p.x` is assigned to here but it was already borrowed
99
LL | q.x;
1010
| --- borrow later used here
1111

1212
error[E0506]: cannot assign to `p` because it is borrowed
1313
--> $DIR/borrowck-assign-comp.rs:20:5
1414
|
1515
LL | let q = &p.y;
16-
| ---- borrow of `p` occurs here
16+
| ---- `p` is borrowed here
1717
LL | p = Point {x: 5, y: 7};
18-
| ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `p` occurs here
18+
| ^^^^^^^^^^^^^^^^^^^^^^ `p` is assigned to here but it was already borrowed
1919
LL | p.x; // silence warning
2020
LL | *q; // stretch loan
2121
| -- borrow later used here
@@ -24,9 +24,9 @@ error[E0506]: cannot assign to `p.y` because it is borrowed
2424
--> $DIR/borrowck-assign-comp.rs:31:5
2525
|
2626
LL | let q = &p.y;
27-
| ---- borrow of `p.y` occurs here
27+
| ---- `p.y` is borrowed here
2828
LL | p.y = 5;
29-
| ^^^^^^^ assignment to borrowed `p.y` occurs here
29+
| ^^^^^^^ `p.y` is assigned to here but it was already borrowed
3030
LL | *q;
3131
| -- borrow later used here
3232

tests/ui/borrowck/borrowck-assign-to-andmut-in-borrowed-loc.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0503]: cannot use `*y.pointer` because it was mutably borrowed
22
--> $DIR/borrowck-assign-to-andmut-in-borrowed-loc.rs:18:9
33
|
44
LL | let z = copy_borrowed_ptr(&mut y);
5-
| ------ borrow of `y` occurs here
5+
| ------ `y` is borrowed here
66
LL | *y.pointer += 1;
77
| ^^^^^^^^^^^^^^^ use of borrowed `y`
88
...
@@ -13,9 +13,9 @@ error[E0506]: cannot assign to `*y.pointer` because it is borrowed
1313
--> $DIR/borrowck-assign-to-andmut-in-borrowed-loc.rs:18:9
1414
|
1515
LL | let z = copy_borrowed_ptr(&mut y);
16-
| ------ borrow of `*y.pointer` occurs here
16+
| ------ `*y.pointer` is borrowed here
1717
LL | *y.pointer += 1;
18-
| ^^^^^^^^^^^^^^^ assignment to borrowed `*y.pointer` occurs here
18+
| ^^^^^^^^^^^^^^^ `*y.pointer` is assigned to here but it was already borrowed
1919
...
2020
LL | *z.pointer += 1;
2121
| --------------- borrow later used here

tests/ui/borrowck/borrowck-bad-nested-calls-move.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0505]: cannot move out of `a` because it is borrowed
22
--> $DIR/borrowck-bad-nested-calls-move.rs:25:9
33
|
4+
LL | let mut a: Box<_> = Box::new(1);
5+
| ----- binding `a` declared here
6+
...
47
LL | add(
58
| --- borrow later used by call
69
LL | &*a,
@@ -11,6 +14,8 @@ LL | a);
1114
error[E0505]: cannot move out of `a` because it is borrowed
1215
--> $DIR/borrowck-bad-nested-calls-move.rs:32:9
1316
|
17+
LL | let mut a: Box<_> = Box::new(1);
18+
| ----- binding `a` declared here
1419
LL | add(
1520
| --- borrow later used by call
1621
LL | &*a,

0 commit comments

Comments
 (0)