Skip to content

Commit 9e3f330

Browse files
authored
Rollup merge of rust-lang#106897 - estebank:issue-99430, r=davidtwco
Tweak E0597 CC rust-lang#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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 33 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 8 additions & 8 deletions
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 | }

0 commit comments

Comments
 (0)