about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-06-18 15:48:08 +0000
committerbors <bors@rust-lang.org>2023-06-18 15:48:08 +0000
commit939786223f2d36b1af62e27e9a7a54bae6e30f3f (patch)
treef5cd868fc3a1d43fa210d42a3430d69339ea0470
parent677710eaf0a0bdb008959ee8717c9fe1c6d187b3 (diff)
parente72618a89755e8408b9937c053e6eca36ff9a9c7 (diff)
downloadrust-939786223f2d36b1af62e27e9a7a54bae6e30f3f.tar.gz
rust-939786223f2d36b1af62e27e9a7a54bae6e30f3f.zip
Auto merge of #112636 - clubby789:no-capture-array-ref, r=cjgillot
Don't capture `&[T; N]` when contents isn't read

Fixes the check in #111831
Fixes #112607, although I decided to test the root cause rather than including the example in the issue as a test.
cc `@BoxyUwU`
-rw-r--r--compiler/rustc_hir_typeck/src/expr_use_visitor.rs2
-rw-r--r--tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.rs25
-rw-r--r--tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.stderr58
3 files changed, 66 insertions, 19 deletions
diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs
index e14e8ac2ce0..82d9f03b145 100644
--- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs
+++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs
@@ -443,7 +443,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
                         if matches!((lhs, wild, rhs), (&[], Some(_), &[]))
                             // Arrays have a statically known size, so
                             // there is no need to read their length
-                            || discr_place.place.base_ty.is_array()
+                            || place.place.ty().peel_refs().is_array()
                         {
                         } else {
                             needs_to_be_read = true;
diff --git a/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.rs b/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.rs
index 41b09ba0370..c3898afa967 100644
--- a/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.rs
+++ b/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.rs
@@ -87,6 +87,31 @@ fn test_4_should_not_capture_array() {
         }
     };
     c();
+
+    // We also do not need to capture an array
+    // behind a reference (#112607)
+    let array: &[i32; 3] = &[0; 3];
+    let c = #[rustc_capture_analysis]
+    || {
+    //~^ First Pass analysis includes:
+        match array {
+            [_, _, _] => {}
+        }
+    };
+    c();
+
+    // We should still not insert a read if the array is inside an
+    // irrefutable pattern
+    struct Foo<T>(T);
+    let f = &Foo(&[10; 3]);
+    let c = #[rustc_capture_analysis]
+    || {
+    //~^ First Pass analysis includes:
+        match f {
+            Foo([_, _, _]) => ()
+        }
+    };
+    c();
 }
 
 // Testing MultiVariant patterns
diff --git a/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.stderr b/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.stderr
index e137af1a0bd..c3c3f8b8477 100644
--- a/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.stderr
+++ b/tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.stderr
@@ -109,7 +109,29 @@ LL | |     };
    | |_____^
 
 error: First Pass analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:105:5
+  --> $DIR/patterns-capture-analysis.rs:95:5
+   |
+LL | /     || {
+LL | |
+LL | |         match array {
+LL | |             [_, _, _] => {}
+LL | |         }
+LL | |     };
+   | |_____^
+
+error: First Pass analysis includes:
+  --> $DIR/patterns-capture-analysis.rs:108:5
+   |
+LL | /     || {
+LL | |
+LL | |         match f {
+LL | |             Foo([_, _, _]) => ()
+LL | |         }
+LL | |     };
+   | |_____^
+
+error: First Pass analysis includes:
+  --> $DIR/patterns-capture-analysis.rs:130:5
    |
 LL | /     || {
 LL | |
@@ -121,13 +143,13 @@ LL | |     };
    | |_____^
    |
 note: Capturing variant[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:108:15
+  --> $DIR/patterns-capture-analysis.rs:133:15
    |
 LL |         match variant {
    |               ^^^^^^^
 
 error: Min Capture analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:105:5
+  --> $DIR/patterns-capture-analysis.rs:130:5
    |
 LL | /     || {
 LL | |
@@ -139,13 +161,13 @@ LL | |     };
    | |_____^
    |
 note: Min Capture variant[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:108:15
+  --> $DIR/patterns-capture-analysis.rs:133:15
    |
 LL |         match variant {
    |               ^^^^^^^
 
 error: First Pass analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:123:5
+  --> $DIR/patterns-capture-analysis.rs:148:5
    |
 LL | /     || {
 LL | |
@@ -157,13 +179,13 @@ LL | |     };
    | |_____^
    |
 note: Capturing slice[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:126:15
+  --> $DIR/patterns-capture-analysis.rs:151:15
    |
 LL |         match slice {
    |               ^^^^^
 
 error: Min Capture analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:123:5
+  --> $DIR/patterns-capture-analysis.rs:148:5
    |
 LL | /     || {
 LL | |
@@ -175,13 +197,13 @@ LL | |     };
    | |_____^
    |
 note: Min Capture slice[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:126:15
+  --> $DIR/patterns-capture-analysis.rs:151:15
    |
 LL |         match slice {
    |               ^^^^^
 
 error: First Pass analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:135:5
+  --> $DIR/patterns-capture-analysis.rs:160:5
    |
 LL | /     || {
 LL | |
@@ -193,13 +215,13 @@ LL | |     };
    | |_____^
    |
 note: Capturing slice[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:138:15
+  --> $DIR/patterns-capture-analysis.rs:163:15
    |
 LL |         match slice {
    |               ^^^^^
 
 error: Min Capture analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:135:5
+  --> $DIR/patterns-capture-analysis.rs:160:5
    |
 LL | /     || {
 LL | |
@@ -211,13 +233,13 @@ LL | |     };
    | |_____^
    |
 note: Min Capture slice[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:138:15
+  --> $DIR/patterns-capture-analysis.rs:163:15
    |
 LL |         match slice {
    |               ^^^^^
 
 error: First Pass analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:147:5
+  --> $DIR/patterns-capture-analysis.rs:172:5
    |
 LL | /     || {
 LL | |
@@ -229,13 +251,13 @@ LL | |     };
    | |_____^
    |
 note: Capturing slice[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:150:15
+  --> $DIR/patterns-capture-analysis.rs:175:15
    |
 LL |         match slice {
    |               ^^^^^
 
 error: Min Capture analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:147:5
+  --> $DIR/patterns-capture-analysis.rs:172:5
    |
 LL | /     || {
 LL | |
@@ -247,13 +269,13 @@ LL | |     };
    | |_____^
    |
 note: Min Capture slice[] -> ImmBorrow
-  --> $DIR/patterns-capture-analysis.rs:150:15
+  --> $DIR/patterns-capture-analysis.rs:175:15
    |
 LL |         match slice {
    |               ^^^^^
 
 error: First Pass analysis includes:
-  --> $DIR/patterns-capture-analysis.rs:164:5
+  --> $DIR/patterns-capture-analysis.rs:189:5
    |
 LL | /     || {
 LL | |
@@ -264,5 +286,5 @@ LL | |         }
 LL | |     };
    | |_____^
 
-error: aborting due to 16 previous errors
+error: aborting due to 18 previous errors