Only compute place if upvars can be resolved
This commit is contained in:
parent
0fa3190394
commit
9c32b5b3ba
4 changed files with 90 additions and 23 deletions
|
@ -155,12 +155,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
ascription: thir::Ascription { variance, user_ty, user_ty_span },
|
ascription: thir::Ascription { variance, user_ty, user_ty_span },
|
||||||
} => {
|
} => {
|
||||||
// Apply the type ascription to the value at `match_pair.place`, which is the
|
// Apply the type ascription to the value at `match_pair.place`, which is the
|
||||||
candidate.ascriptions.push(Ascription {
|
if let Ok(place_resolved) =
|
||||||
span: user_ty_span,
|
match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
|
||||||
user_ty,
|
{
|
||||||
source: match_pair.place.clone().into_place(self.tcx, self.typeck_results),
|
candidate.ascriptions.push(Ascription {
|
||||||
variance,
|
span: user_ty_span,
|
||||||
});
|
user_ty,
|
||||||
|
source: place_resolved.into_place(self.tcx, self.typeck_results),
|
||||||
|
variance,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
|
candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
|
||||||
|
|
||||||
|
@ -173,15 +177,19 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
PatKind::Binding { name, mutability, mode, var, ty, ref subpattern, is_primary: _ } => {
|
PatKind::Binding { name, mutability, mode, var, ty, ref subpattern, is_primary: _ } => {
|
||||||
candidate.bindings.push(Binding {
|
if let Ok(place_resolved) =
|
||||||
name,
|
match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
|
||||||
mutability,
|
{
|
||||||
span: match_pair.pattern.span,
|
candidate.bindings.push(Binding {
|
||||||
source: match_pair.place.clone().into_place(self.tcx, self.typeck_results),
|
name,
|
||||||
var_id: var,
|
mutability,
|
||||||
var_ty: ty,
|
span: match_pair.pattern.span,
|
||||||
binding_mode: mode,
|
source: place_resolved.into_place(self.tcx, self.typeck_results),
|
||||||
});
|
var_id: var,
|
||||||
|
var_ty: ty,
|
||||||
|
binding_mode: mode,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(subpattern) = subpattern.as_ref() {
|
if let Some(subpattern) = subpattern.as_ref() {
|
||||||
// this is the `x @ P` case; have to keep matching against `P` now
|
// this is the `x @ P` case; have to keep matching against `P` now
|
||||||
|
|
|
@ -31,15 +31,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||||
suffix: &'pat [Pat<'tcx>],
|
suffix: &'pat [Pat<'tcx>],
|
||||||
) {
|
) {
|
||||||
let tcx = self.tcx;
|
let tcx = self.tcx;
|
||||||
let (min_length, exact_size) = match place
|
let (min_length, exact_size) = if let Ok(place_resolved) =
|
||||||
.clone()
|
place.clone().try_upvars_resolved(tcx, self.typeck_results)
|
||||||
.into_place(tcx, self.typeck_results)
|
|
||||||
.ty(&self.local_decls, tcx)
|
|
||||||
.ty
|
|
||||||
.kind()
|
|
||||||
{
|
{
|
||||||
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
|
match place_resolved
|
||||||
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
|
.into_place(tcx, self.typeck_results)
|
||||||
|
.ty(&self.local_decls, tcx)
|
||||||
|
.ty
|
||||||
|
.kind()
|
||||||
|
{
|
||||||
|
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
|
||||||
|
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
((prefix.len() + suffix.len()).try_into().unwrap(), false)
|
||||||
};
|
};
|
||||||
|
|
||||||
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
|
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
|
||||||
|
|
30
src/test/ui/closures/2229_closure_analysis/issue-87987.rs
Normal file
30
src/test/ui/closures/2229_closure_analysis/issue-87987.rs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
// run-pass
|
||||||
|
// edition:2021
|
||||||
|
|
||||||
|
struct Props {
|
||||||
|
field_1: u32, //~ WARNING: field is never read: `field_1`
|
||||||
|
field_2: u32, //~ WARNING: field is never read: `field_2`
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Test 1
|
||||||
|
let props_2 = Props { //~ WARNING: unused variable: `props_2`
|
||||||
|
field_1: 1,
|
||||||
|
field_2: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = || {
|
||||||
|
let _: Props = props_2;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test 2
|
||||||
|
let mut arr = [1, 3, 4, 5];
|
||||||
|
|
||||||
|
let mref = &mut arr;
|
||||||
|
|
||||||
|
let _c = || match arr {
|
||||||
|
[_, _, _, _] => println!("A")
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{:#?}", mref);
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
warning: unused variable: `props_2`
|
||||||
|
--> $DIR/issue-87987.rs:11:9
|
||||||
|
|
|
||||||
|
LL | let props_2 = Props {
|
||||||
|
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_props_2`
|
||||||
|
|
|
||||||
|
= note: `#[warn(unused_variables)]` on by default
|
||||||
|
|
||||||
|
warning: field is never read: `field_1`
|
||||||
|
--> $DIR/issue-87987.rs:5:5
|
||||||
|
|
|
||||||
|
LL | field_1: u32,
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `#[warn(dead_code)]` on by default
|
||||||
|
|
||||||
|
warning: field is never read: `field_2`
|
||||||
|
--> $DIR/issue-87987.rs:6:5
|
||||||
|
|
|
||||||
|
LL | field_2: u32,
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: 3 warnings emitted
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue