Skip to content

Commit

Permalink
Rollup merge of rust-lang#106897 - estebank:issue-99430, r=davidtwco
Browse files Browse the repository at this point in the history
Tweak E0597

CC rust-lang#99430
  • Loading branch information
matthiaskrgr authored Jan 25, 2023
2 parents 0c9d9e5 + 7b8251e commit 4d0e6aa
Show file tree
Hide file tree
Showing 230 changed files with 853 additions and 354 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
desc,
);

err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
err.span_label(borrow_span, format!("{} is borrowed here", borrow_desc));
err.span_label(span, format!("use of borrowed {}", borrow_desc));
err
}
Expand Down Expand Up @@ -250,8 +250,8 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
desc,
);

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&self.local_names,
&mut err,
"",
None,
Some(borrow_span),
None,
);
}
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Print diagnostics to explain why values are borrowed.

use rustc_errors::{Applicability, Diagnostic};
use rustc_hir as hir;
use rustc_hir::intravisit::Visitor;
use rustc_index::vec::IndexVec;
use rustc_infer::infer::NllRegionVariableOrigin;
use rustc_middle::mir::{
Expand All @@ -11,6 +13,7 @@ use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::{self, RegionVid, TyCtxt};
use rustc_span::symbol::{kw, Symbol};
use rustc_span::{sym, DesugaringKind, Span};
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;

use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
use crate::{
Expand Down Expand Up @@ -63,6 +66,36 @@ impl<'tcx> BorrowExplanation<'tcx> {
borrow_span: Option<Span>,
multiple_borrow_span: Option<(Span, Span)>,
) {
if let Some(span) = borrow_span {
let def_id = body.source.def_id();
if let Some(node) = tcx.hir().get_if_local(def_id)
&& let Some(body_id) = node.body_id()
{
let body = tcx.hir().body(body_id);
let mut expr_finder = FindExprBySpan::new(span);
expr_finder.visit_expr(body.value);
if let Some(mut expr) = expr_finder.result {
while let hir::ExprKind::AddrOf(_, _, inner)
| hir::ExprKind::Unary(hir::UnOp::Deref, inner)
| hir::ExprKind::Field(inner, _)
| hir::ExprKind::MethodCall(_, inner, _, _)
| hir::ExprKind::Index(inner, _) = &expr.kind
{
expr = inner;
}
if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
&& let [hir::PathSegment { ident, args: None, .. }] = p.segments
&& let hir::def::Res::Local(hir_id) = p.res
&& let Some(hir::Node::Pat(pat)) = tcx.hir().find(hir_id)
{
err.span_label(
pat.span,
&format!("binding `{ident}` declared here"),
);
}
}
}
}
match *self {
BorrowExplanation::UsedLater(later_use_kind, var_or_use_span, path_span) => {
let message = match later_use_kind {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
};
self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span), "");

use_spans.args_span_label(err, format!("move out of {place_desc} occurs here"));
use_spans.args_span_label(err, format!("{place_desc} is moved here"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2825,7 +2825,7 @@ pub struct FindExprBySpan<'hir> {
}

impl<'hir> FindExprBySpan<'hir> {
fn new(span: Span) -> Self {
pub fn new(span: Span) -> Self {
Self { span, result: None, ty_result: None }
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui-fulldeps/dropck-tarena-cycle-checked.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0597]: `arena` does not live long enough
--> $DIR/dropck-tarena-cycle-checked.rs:116:7
|
LL | let arena = TypedArena::default();
| ----- binding `arena` declared here
LL | f(&arena);
| ^^^^^^ borrowed value does not live long enough
LL | }
Expand Down
2 changes: 2 additions & 0 deletions tests/ui-fulldeps/dropck-tarena-unsound-drop.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0597]: `arena` does not live long enough
--> $DIR/dropck-tarena-unsound-drop.rs:41:7
|
LL | let arena: TypedArena<C> = TypedArena::default();
| ----- binding `arena` declared here
LL | f(&arena);
| ^^^^^^ borrowed value does not live long enough
LL | }
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/asm/type-check-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0506]: cannot assign to `a` because it is borrowed
--> $DIR/type-check-4.rs:14:9
|
LL | let p = &a;
| -- borrow of `a` occurs here
| -- `a` is borrowed here
LL | asm!("{}", out(reg) a);
| ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `a` occurs here
| ^^^^^^^^^^^^^^^^^^^^^^ `a` is assigned to here but it was already borrowed
LL |
LL | println!("{}", p);
| - borrow later used here
Expand All @@ -13,7 +13,7 @@ error[E0503]: cannot use `a` because it was mutably borrowed
--> $DIR/type-check-4.rs:22:28
|
LL | let p = &mut a;
| ------ borrow of `a` occurs here
| ------ `a` is borrowed here
LL | asm!("{}", in(reg) a);
| ^ use of borrowed `a`
LL |
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/associated-types/associated-types-outlives.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/associated-types-outlives.rs:22:14
|
LL | F: for<'a> FnOnce(<T as Foo<'a>>::Bar)>(x: T, f: F) {
| - binding `x` declared here
...
LL | 's: loop { y = denormalise(&x); break }
| -- borrow of `x` occurs here
LL | drop(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ error[E0506]: cannot assign to `*x` because it is borrowed
LL | pub async fn async_fn(x: &mut i32) -> &i32 {
| - let's call the lifetime of this reference `'1`
LL | let y = &*x;
| --- borrow of `*x` occurs here
| --- `*x` is borrowed here
LL | *x += 1;
| ^^^^^^^ assignment to borrowed `*x` occurs here
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
LL | y
| - returning this value requires that `*x` is borrowed for `'1`

error[E0506]: cannot assign to `*x` because it is borrowed
--> $DIR/issue-74072-lifetime-name-annotations.rs:16:9
|
LL | let y = &*x;
| --- borrow of `*x` occurs here
| --- `*x` is borrowed here
LL | *x += 1;
| ^^^^^^^ assignment to borrowed `*x` occurs here
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
LL | y
| - returning this value requires that `*x` is borrowed for `'1`
LL | })()
Expand All @@ -28,19 +28,19 @@ error[E0506]: cannot assign to `*x` because it is borrowed
LL | (async move || -> &i32 {
| - let's call the lifetime of this reference `'1`
LL | let y = &*x;
| --- borrow of `*x` occurs here
| --- `*x` is borrowed here
LL | *x += 1;
| ^^^^^^^ assignment to borrowed `*x` occurs here
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
LL | y
| - returning this value requires that `*x` is borrowed for `'1`

error[E0506]: cannot assign to `*x` because it is borrowed
--> $DIR/issue-74072-lifetime-name-annotations.rs:32:9
|
LL | let y = &*x;
| --- borrow of `*x` occurs here
| --- `*x` is borrowed here
LL | *x += 1;
| ^^^^^^^ assignment to borrowed `*x` occurs here
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
LL | y
| - returning this value requires that `*x` is borrowed for `'1`
LL | }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ error[E0506]: cannot assign to `*x` because it is borrowed
LL | pub async fn async_fn(x: &mut i32) -> (&i32, &i32) {
| - let's call the lifetime of this reference `'1`
LL | let y = &*x;
| --- borrow of `*x` occurs here
| --- `*x` is borrowed here
LL | *x += 1;
| ^^^^^^^ assignment to borrowed `*x` occurs here
| ^^^^^^^ `*x` is assigned to here but it was already borrowed
LL | (&32, y)
| -------- returning this value requires that `*x` is borrowed for `'1`

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/async-await/multiple-lifetimes/ret-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0506]: cannot assign to `a` because it is borrowed
--> $DIR/ret-ref.rs:16:5
|
LL | let future = multiple_named_lifetimes(&a, &b);
| -- borrow of `a` occurs here
| -- `a` is borrowed here
LL | a += 1;
| ^^^^^^ assignment to borrowed `a` occurs here
| ^^^^^^ `a` is assigned to here but it was already borrowed
LL | b += 1;
LL | let p = future.await;
| ------ borrow later used here
Expand All @@ -13,21 +13,21 @@ error[E0506]: cannot assign to `b` because it is borrowed
--> $DIR/ret-ref.rs:17:5
|
LL | let future = multiple_named_lifetimes(&a, &b);
| -- borrow of `b` occurs here
| -- `b` is borrowed here
LL | a += 1;
LL | b += 1;
| ^^^^^^ assignment to borrowed `b` occurs here
| ^^^^^^ `b` is assigned to here but it was already borrowed
LL | let p = future.await;
| ------ borrow later used here

error[E0506]: cannot assign to `a` because it is borrowed
--> $DIR/ret-ref.rs:28:5
|
LL | let future = multiple_named_lifetimes(&a, &b);
| -- borrow of `a` occurs here
| -- `a` is borrowed here
LL | let p = future.await;
LL | a += 1;
| ^^^^^^ assignment to borrowed `a` occurs here
| ^^^^^^ `a` is assigned to here but it was already borrowed
LL | b += 1;
LL | drop(p);
| - borrow later used here
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/augmented-assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl AddAssign for Int {
}

fn main() {
let mut x = Int(1);
let mut x = Int(1); //~ NOTE binding `x` declared here
x
//~^ NOTE borrow of `x` occurs here
+=
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/augmented-assignments.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/augmented-assignments.rs:16:5
|
LL | let mut x = Int(1);
| ----- binding `x` declared here
LL | x
| - borrow of `x` occurs here
...
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/binop/binop-move-semantics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ LL | fn move_then_borrow<T: Add<Output=()> + Clone + Copy>(x: T) {
error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/binop-move-semantics.rs:21:5
|
LL | fn move_borrowed<T: Add<Output=()>>(x: T, mut y: T) {
| - binding `x` declared here
LL | let m = &x;
| -- borrow of `x` occurs here
...
Expand All @@ -53,6 +55,9 @@ LL | use_mut(n); use_imm(m);
error[E0505]: cannot move out of `y` because it is borrowed
--> $DIR/binop-move-semantics.rs:23:5
|
LL | fn move_borrowed<T: Add<Output=()>>(x: T, mut y: T) {
| ----- binding `y` declared here
LL | let m = &x;
LL | let n = &mut y;
| ------ borrow of `y` occurs here
...
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/borrowck/borrow-tuple-fields.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/borrow-tuple-fields.rs:12:13
|
LL | let x: (Box<_>, _) = (Box::new(1), 2);
| - binding `x` declared here
LL | let r = &x.0;
| ---- borrow of `x.0` occurs here
LL | let y = x;
Expand Down Expand Up @@ -32,6 +34,8 @@ LL | a.use_ref();
error[E0505]: cannot move out of `x` because it is borrowed
--> $DIR/borrow-tuple-fields.rs:28:13
|
LL | let x = Foo(Box::new(1), 2);
| - binding `x` declared here
LL | let r = &x.0;
| ---- borrow of `x.0` occurs here
LL | let y = x;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/borrowck/borrowck-anon-fields-variant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0503]: cannot use `y` because it was mutably borrowed
--> $DIR/borrowck-anon-fields-variant.rs:16:19
|
LL | Foo::Y(ref mut a, _) => a,
| --------- borrow of `y.0` occurs here
| --------- `y.0` is borrowed here
...
LL | let b = match y {
| ^ use of borrowed `y.0`
Expand All @@ -14,7 +14,7 @@ error[E0503]: cannot use `y` because it was mutably borrowed
--> $DIR/borrowck-anon-fields-variant.rs:34:19
|
LL | Foo::Y(ref mut a, _) => a,
| --------- borrow of `y.0` occurs here
| --------- `y.0` is borrowed here
...
LL | let b = match y {
| ^ use of borrowed `y.0`
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/borrowck/borrowck-assign-comp.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ error[E0506]: cannot assign to `p.x` because it is borrowed
--> $DIR/borrowck-assign-comp.rs:10:5
|
LL | let q = &p;
| -- borrow of `p.x` occurs here
| -- `p.x` is borrowed here
...
LL | p.x = 5;
| ^^^^^^^ assignment to borrowed `p.x` occurs here
| ^^^^^^^ `p.x` is assigned to here but it was already borrowed
LL | q.x;
| --- borrow later used here

error[E0506]: cannot assign to `p` because it is borrowed
--> $DIR/borrowck-assign-comp.rs:20:5
|
LL | let q = &p.y;
| ---- borrow of `p` occurs here
| ---- `p` is borrowed here
LL | p = Point {x: 5, y: 7};
| ^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `p` occurs here
| ^^^^^^^^^^^^^^^^^^^^^^ `p` is assigned to here but it was already borrowed
LL | p.x; // silence warning
LL | *q; // stretch loan
| -- borrow later used here
Expand All @@ -24,9 +24,9 @@ error[E0506]: cannot assign to `p.y` because it is borrowed
--> $DIR/borrowck-assign-comp.rs:31:5
|
LL | let q = &p.y;
| ---- borrow of `p.y` occurs here
| ---- `p.y` is borrowed here
LL | p.y = 5;
| ^^^^^^^ assignment to borrowed `p.y` occurs here
| ^^^^^^^ `p.y` is assigned to here but it was already borrowed
LL | *q;
| -- borrow later used here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0503]: cannot use `*y.pointer` because it was mutably borrowed
--> $DIR/borrowck-assign-to-andmut-in-borrowed-loc.rs:18:9
|
LL | let z = copy_borrowed_ptr(&mut y);
| ------ borrow of `y` occurs here
| ------ `y` is borrowed here
LL | *y.pointer += 1;
| ^^^^^^^^^^^^^^^ use of borrowed `y`
...
Expand All @@ -13,9 +13,9 @@ error[E0506]: cannot assign to `*y.pointer` because it is borrowed
--> $DIR/borrowck-assign-to-andmut-in-borrowed-loc.rs:18:9
|
LL | let z = copy_borrowed_ptr(&mut y);
| ------ borrow of `*y.pointer` occurs here
| ------ `*y.pointer` is borrowed here
LL | *y.pointer += 1;
| ^^^^^^^^^^^^^^^ assignment to borrowed `*y.pointer` occurs here
| ^^^^^^^^^^^^^^^ `*y.pointer` is assigned to here but it was already borrowed
...
LL | *z.pointer += 1;
| --------------- borrow later used here
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/borrowck/borrowck-bad-nested-calls-move.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0505]: cannot move out of `a` because it is borrowed
--> $DIR/borrowck-bad-nested-calls-move.rs:25:9
|
LL | let mut a: Box<_> = Box::new(1);
| ----- binding `a` declared here
...
LL | add(
| --- borrow later used by call
LL | &*a,
Expand All @@ -11,6 +14,8 @@ LL | a);
error[E0505]: cannot move out of `a` because it is borrowed
--> $DIR/borrowck-bad-nested-calls-move.rs:32:9
|
LL | let mut a: Box<_> = Box::new(1);
| ----- binding `a` declared here
LL | add(
| --- borrow later used by call
LL | &*a,
Expand Down
Loading

0 comments on commit 4d0e6aa

Please sign in to comment.