From c6111e8d232964f11eb3540ed7228de6e13782df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 17 Jan 2023 02:47:50 +0000 Subject: [PATCH] Account for field access when looking for inner-most path in expression --- compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs | 3 ++- tests/ui/borrowck/borrow-tuple-fields.stderr | 4 ++++ tests/ui/borrowck/borrowck-field-sensitivity.stderr | 4 ++++ .../borrowck-local-borrow-with-panic-outlives-fn.stderr | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs index d60facb91b4..b6a676ef636 100644 --- a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs +++ b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs @@ -76,7 +76,8 @@ pub(crate) fn add_explanation_to_diagnostic( 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) = &expr.kind + | hir::ExprKind::Unary(hir::UnOp::Deref, inner) + | hir::ExprKind::Field(inner, _) = &expr.kind { expr = inner; } diff --git a/tests/ui/borrowck/borrow-tuple-fields.stderr b/tests/ui/borrowck/borrow-tuple-fields.stderr index befa751a600..d7d3efe492c 100644 --- a/tests/ui/borrowck/borrow-tuple-fields.stderr +++ b/tests/ui/borrowck/borrow-tuple-fields.stderr @@ -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; @@ -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; diff --git a/tests/ui/borrowck/borrowck-field-sensitivity.stderr b/tests/ui/borrowck/borrowck-field-sensitivity.stderr index e009f5913ed..11812847dd1 100644 --- a/tests/ui/borrowck/borrowck-field-sensitivity.stderr +++ b/tests/ui/borrowck/borrowck-field-sensitivity.stderr @@ -41,6 +41,8 @@ LL | let p = &x.b; error[E0505]: cannot move out of `x.b` because it is borrowed --> $DIR/borrowck-field-sensitivity.rs:34:10 | +LL | let x = A { a: 1, b: Box::new(2) }; + | - binding `x` declared here LL | let p = &x.b; | ---- borrow of `x.b` occurs here LL | drop(x.b); @@ -51,6 +53,8 @@ LL | drop(**p); error[E0505]: cannot move out of `x.b` because it is borrowed --> $DIR/borrowck-field-sensitivity.rs:41:14 | +LL | let x = A { a: 1, b: Box::new(2) }; + | - binding `x` declared here LL | let p = &x.b; | ---- borrow of `x.b` occurs here LL | let _y = A { a: 3, .. x }; diff --git a/tests/ui/borrowck/borrowck-local-borrow-with-panic-outlives-fn.stderr b/tests/ui/borrowck/borrowck-local-borrow-with-panic-outlives-fn.stderr index 6ea6951ad96..0fdb1dabbc5 100644 --- a/tests/ui/borrowck/borrowck-local-borrow-with-panic-outlives-fn.stderr +++ b/tests/ui/borrowck/borrowck-local-borrow-with-panic-outlives-fn.stderr @@ -1,6 +1,8 @@ error[E0597]: `z.1` does not live long enough --> $DIR/borrowck-local-borrow-with-panic-outlives-fn.rs:3:15 | +LL | let mut z = (0, 0); + | ----- binding `z` declared here LL | *x = Some(&mut z.1); | ----------^^^^^^^^- | | |