Skip to content

Commit 656db98

Browse files
committed
Tweak E0597
CC rust-lang#99430
1 parent ae4d89d commit 656db98

File tree

208 files changed

+772
-349
lines changed

Some content is hidden

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

208 files changed

+772
-349
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
@@ -1742,7 +1742,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17421742
&self.local_names,
17431743
&mut err,
17441744
"",
1745-
None,
1745+
Some(borrow_span),
17461746
None,
17471747
);
17481748
}

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+28
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,31 @@ 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) = &expr.kind {
79+
expr = inner;
80+
}
81+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
82+
&& let [hir::PathSegment { ident, args: None, .. }] = p.segments
83+
&& let hir::def::Res::Local(hir_id) = p.res
84+
&& let Some(hir::Node::Pat(pat)) = tcx.hir().find(hir_id)
85+
{
86+
err.span_label(
87+
pat.span,
88+
&format!("binding `{ident}` declared here"),
89+
);
90+
}
91+
}
92+
}
93+
}
6694
match *self {
6795
BorrowExplanation::UsedLater(later_use_kind, var_or_use_span, path_span) => {
6896
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
@@ -2827,7 +2827,7 @@ pub struct FindExprBySpan<'hir> {
28272827
}
28282828

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

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/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-closures-mut-and-imm.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ error[E0506]: cannot assign to `x` because it is borrowed
4949
LL | let c2 = || x * 5;
5050
| -- - borrow occurs due to use in closure
5151
| |
52-
| borrow of `x` occurs here
52+
| `x` is borrowed here
5353
LL | x = 5;
54-
| ^^^^^ assignment to borrowed `x` occurs here
54+
| ^^^^^ `x` is assigned to here but it was already borrowed
5555
LL |
5656
LL | drop(c2);
5757
| -- borrow later used here
@@ -62,9 +62,9 @@ error[E0506]: cannot assign to `x` because it is borrowed
6262
LL | let c1 = || get(&x);
6363
| -- - borrow occurs due to use in closure
6464
| |
65-
| borrow of `x` occurs here
65+
| `x` is borrowed here
6666
LL | x = 5;
67-
| ^^^^^ assignment to borrowed `x` occurs here
67+
| ^^^^^ `x` is assigned to here but it was already borrowed
6868
LL |
6969
LL | drop(c1);
7070
| -- borrow later used here
@@ -75,9 +75,9 @@ error[E0506]: cannot assign to `*x` because it is borrowed
7575
LL | let c1 = || get(&*x);
7676
| -- -- borrow occurs due to use in closure
7777
| |
78-
| borrow of `*x` occurs here
78+
| `*x` is borrowed here
7979
LL | *x = 5;
80-
| ^^^^^^ assignment to borrowed `*x` occurs here
80+
| ^^^^^^ `*x` is assigned to here but it was already borrowed
8181
LL |
8282
LL | drop(c1);
8383
| -- borrow later used here
@@ -88,9 +88,9 @@ error[E0506]: cannot assign to `*x.f` because it is borrowed
8888
LL | let c1 = || get(&*x.f);
8989
| -- ---- borrow occurs due to use in closure
9090
| |
91-
| borrow of `*x.f` occurs here
91+
| `*x.f` is borrowed here
9292
LL | *x.f = 5;
93-
| ^^^^^^^^ assignment to borrowed `*x.f` occurs here
93+
| ^^^^^^^^ `*x.f` is assigned to here but it was already borrowed
9494
LL |
9595
LL | drop(c1);
9696
| -- borrow later used here

0 commit comments

Comments
 (0)