Skip to content

Commit 9c32b5b

Browse files
committed
Only compute place if upvars can be resolved
1 parent 0fa3190 commit 9c32b5b

File tree

4 files changed

+90
-23
lines changed

4 files changed

+90
-23
lines changed

compiler/rustc_mir_build/src/build/matches/simplify.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
155155
ascription: thir::Ascription { variance, user_ty, user_ty_span },
156156
} => {
157157
// Apply the type ascription to the value at `match_pair.place`, which is the
158-
candidate.ascriptions.push(Ascription {
159-
span: user_ty_span,
160-
user_ty,
161-
source: match_pair.place.clone().into_place(self.tcx, self.typeck_results),
162-
variance,
163-
});
158+
if let Ok(place_resolved) =
159+
match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
160+
{
161+
candidate.ascriptions.push(Ascription {
162+
span: user_ty_span,
163+
user_ty,
164+
source: place_resolved.into_place(self.tcx, self.typeck_results),
165+
variance,
166+
});
167+
}
164168

165169
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
166170

@@ -173,15 +177,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
173177
}
174178

175179
PatKind::Binding { name, mutability, mode, var, ty, ref subpattern, is_primary: _ } => {
176-
candidate.bindings.push(Binding {
177-
name,
178-
mutability,
179-
span: match_pair.pattern.span,
180-
source: match_pair.place.clone().into_place(self.tcx, self.typeck_results),
181-
var_id: var,
182-
var_ty: ty,
183-
binding_mode: mode,
184-
});
180+
if let Ok(place_resolved) =
181+
match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
182+
{
183+
candidate.bindings.push(Binding {
184+
name,
185+
mutability,
186+
span: match_pair.pattern.span,
187+
source: place_resolved.into_place(self.tcx, self.typeck_results),
188+
var_id: var,
189+
var_ty: ty,
190+
binding_mode: mode,
191+
});
192+
}
185193

186194
if let Some(subpattern) = subpattern.as_ref() {
187195
// this is the `x @ P` case; have to keep matching against `P` now

compiler/rustc_mir_build/src/build/matches/util.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3131
suffix: &'pat [Pat<'tcx>],
3232
) {
3333
let tcx = self.tcx;
34-
let (min_length, exact_size) = match place
35-
.clone()
36-
.into_place(tcx, self.typeck_results)
37-
.ty(&self.local_decls, tcx)
38-
.ty
39-
.kind()
34+
let (min_length, exact_size) = if let Ok(place_resolved) =
35+
place.clone().try_upvars_resolved(tcx, self.typeck_results)
4036
{
41-
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
42-
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
37+
match place_resolved
38+
.into_place(tcx, self.typeck_results)
39+
.ty(&self.local_decls, tcx)
40+
.ty
41+
.kind()
42+
{
43+
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
44+
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
45+
}
46+
} else {
47+
((prefix.len() + suffix.len()).try_into().unwrap(), false)
4348
};
4449

4550
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// run-pass
2+
// edition:2021
3+
4+
struct Props {
5+
field_1: u32, //~ WARNING: field is never read: `field_1`
6+
field_2: u32, //~ WARNING: field is never read: `field_2`
7+
}
8+
9+
fn main() {
10+
// Test 1
11+
let props_2 = Props { //~ WARNING: unused variable: `props_2`
12+
field_1: 1,
13+
field_2: 1,
14+
};
15+
16+
let _ = || {
17+
let _: Props = props_2;
18+
};
19+
20+
// Test 2
21+
let mut arr = [1, 3, 4, 5];
22+
23+
let mref = &mut arr;
24+
25+
let _c = || match arr {
26+
[_, _, _, _] => println!("A")
27+
};
28+
29+
println!("{:#?}", mref);
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: unused variable: `props_2`
2+
--> $DIR/issue-87987.rs:11:9
3+
|
4+
LL | let props_2 = Props {
5+
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_props_2`
6+
|
7+
= note: `#[warn(unused_variables)]` on by default
8+
9+
warning: field is never read: `field_1`
10+
--> $DIR/issue-87987.rs:5:5
11+
|
12+
LL | field_1: u32,
13+
| ^^^^^^^^^^^^
14+
|
15+
= note: `#[warn(dead_code)]` on by default
16+
17+
warning: field is never read: `field_2`
18+
--> $DIR/issue-87987.rs:6:5
19+
|
20+
LL | field_2: u32,
21+
| ^^^^^^^^^^^^
22+
23+
warning: 3 warnings emitted
24+

0 commit comments

Comments
 (0)