Skip to content

Commit e72618a

Browse files
committed
Don't capture &[T; N] when contents isn't read
1 parent 0b475c7 commit e72618a

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
443443
if matches!((lhs, wild, rhs), (&[], Some(_), &[]))
444444
// Arrays have a statically known size, so
445445
// there is no need to read their length
446-
|| discr_place.place.base_ty.is_array()
446+
|| place.place.ty().peel_refs().is_array()
447447
{
448448
} else {
449449
needs_to_be_read = true;

tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.rs

+25
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@ fn test_4_should_not_capture_array() {
8787
}
8888
};
8989
c();
90+
91+
// We also do not need to capture an array
92+
// behind a reference (#112607)
93+
let array: &[i32; 3] = &[0; 3];
94+
let c = #[rustc_capture_analysis]
95+
|| {
96+
//~^ First Pass analysis includes:
97+
match array {
98+
[_, _, _] => {}
99+
}
100+
};
101+
c();
102+
103+
// We should still not insert a read if the array is inside an
104+
// irrefutable pattern
105+
struct Foo<T>(T);
106+
let f = &Foo(&[10; 3]);
107+
let c = #[rustc_capture_analysis]
108+
|| {
109+
//~^ First Pass analysis includes:
110+
match f {
111+
Foo([_, _, _]) => ()
112+
}
113+
};
114+
c();
90115
}
91116

92117
// Testing MultiVariant patterns

tests/ui/closures/2229_closure_analysis/match/patterns-capture-analysis.stderr

+40-18
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,29 @@ LL | | };
109109
| |_____^
110110

111111
error: First Pass analysis includes:
112-
--> $DIR/patterns-capture-analysis.rs:105:5
112+
--> $DIR/patterns-capture-analysis.rs:95:5
113+
|
114+
LL | / || {
115+
LL | |
116+
LL | | match array {
117+
LL | | [_, _, _] => {}
118+
LL | | }
119+
LL | | };
120+
| |_____^
121+
122+
error: First Pass analysis includes:
123+
--> $DIR/patterns-capture-analysis.rs:108:5
124+
|
125+
LL | / || {
126+
LL | |
127+
LL | | match f {
128+
LL | | Foo([_, _, _]) => ()
129+
LL | | }
130+
LL | | };
131+
| |_____^
132+
133+
error: First Pass analysis includes:
134+
--> $DIR/patterns-capture-analysis.rs:130:5
113135
|
114136
LL | / || {
115137
LL | |
@@ -121,13 +143,13 @@ LL | | };
121143
| |_____^
122144
|
123145
note: Capturing variant[] -> ImmBorrow
124-
--> $DIR/patterns-capture-analysis.rs:108:15
146+
--> $DIR/patterns-capture-analysis.rs:133:15
125147
|
126148
LL | match variant {
127149
| ^^^^^^^
128150

129151
error: Min Capture analysis includes:
130-
--> $DIR/patterns-capture-analysis.rs:105:5
152+
--> $DIR/patterns-capture-analysis.rs:130:5
131153
|
132154
LL | / || {
133155
LL | |
@@ -139,13 +161,13 @@ LL | | };
139161
| |_____^
140162
|
141163
note: Min Capture variant[] -> ImmBorrow
142-
--> $DIR/patterns-capture-analysis.rs:108:15
164+
--> $DIR/patterns-capture-analysis.rs:133:15
143165
|
144166
LL | match variant {
145167
| ^^^^^^^
146168

147169
error: First Pass analysis includes:
148-
--> $DIR/patterns-capture-analysis.rs:123:5
170+
--> $DIR/patterns-capture-analysis.rs:148:5
149171
|
150172
LL | / || {
151173
LL | |
@@ -157,13 +179,13 @@ LL | | };
157179
| |_____^
158180
|
159181
note: Capturing slice[] -> ImmBorrow
160-
--> $DIR/patterns-capture-analysis.rs:126:15
182+
--> $DIR/patterns-capture-analysis.rs:151:15
161183
|
162184
LL | match slice {
163185
| ^^^^^
164186

165187
error: Min Capture analysis includes:
166-
--> $DIR/patterns-capture-analysis.rs:123:5
188+
--> $DIR/patterns-capture-analysis.rs:148:5
167189
|
168190
LL | / || {
169191
LL | |
@@ -175,13 +197,13 @@ LL | | };
175197
| |_____^
176198
|
177199
note: Min Capture slice[] -> ImmBorrow
178-
--> $DIR/patterns-capture-analysis.rs:126:15
200+
--> $DIR/patterns-capture-analysis.rs:151:15
179201
|
180202
LL | match slice {
181203
| ^^^^^
182204

183205
error: First Pass analysis includes:
184-
--> $DIR/patterns-capture-analysis.rs:135:5
206+
--> $DIR/patterns-capture-analysis.rs:160:5
185207
|
186208
LL | / || {
187209
LL | |
@@ -193,13 +215,13 @@ LL | | };
193215
| |_____^
194216
|
195217
note: Capturing slice[] -> ImmBorrow
196-
--> $DIR/patterns-capture-analysis.rs:138:15
218+
--> $DIR/patterns-capture-analysis.rs:163:15
197219
|
198220
LL | match slice {
199221
| ^^^^^
200222

201223
error: Min Capture analysis includes:
202-
--> $DIR/patterns-capture-analysis.rs:135:5
224+
--> $DIR/patterns-capture-analysis.rs:160:5
203225
|
204226
LL | / || {
205227
LL | |
@@ -211,13 +233,13 @@ LL | | };
211233
| |_____^
212234
|
213235
note: Min Capture slice[] -> ImmBorrow
214-
--> $DIR/patterns-capture-analysis.rs:138:15
236+
--> $DIR/patterns-capture-analysis.rs:163:15
215237
|
216238
LL | match slice {
217239
| ^^^^^
218240

219241
error: First Pass analysis includes:
220-
--> $DIR/patterns-capture-analysis.rs:147:5
242+
--> $DIR/patterns-capture-analysis.rs:172:5
221243
|
222244
LL | / || {
223245
LL | |
@@ -229,13 +251,13 @@ LL | | };
229251
| |_____^
230252
|
231253
note: Capturing slice[] -> ImmBorrow
232-
--> $DIR/patterns-capture-analysis.rs:150:15
254+
--> $DIR/patterns-capture-analysis.rs:175:15
233255
|
234256
LL | match slice {
235257
| ^^^^^
236258

237259
error: Min Capture analysis includes:
238-
--> $DIR/patterns-capture-analysis.rs:147:5
260+
--> $DIR/patterns-capture-analysis.rs:172:5
239261
|
240262
LL | / || {
241263
LL | |
@@ -247,13 +269,13 @@ LL | | };
247269
| |_____^
248270
|
249271
note: Min Capture slice[] -> ImmBorrow
250-
--> $DIR/patterns-capture-analysis.rs:150:15
272+
--> $DIR/patterns-capture-analysis.rs:175:15
251273
|
252274
LL | match slice {
253275
| ^^^^^
254276

255277
error: First Pass analysis includes:
256-
--> $DIR/patterns-capture-analysis.rs:164:5
278+
--> $DIR/patterns-capture-analysis.rs:189:5
257279
|
258280
LL | / || {
259281
LL | |
@@ -264,5 +286,5 @@ LL | | }
264286
LL | | };
265287
| |_____^
266288

267-
error: aborting due to 16 previous errors
289+
error: aborting due to 18 previous errors
268290

0 commit comments

Comments
 (0)