mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Implement unlabeled struct field pattern inference
This commit is contained in:
parent
3340807bd2
commit
ac216880f5
4 changed files with 26 additions and 47 deletions
|
@ -332,7 +332,7 @@ impl_arena_id!(PatId);
|
|||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct FieldPat {
|
||||
pub(crate) name: Name,
|
||||
pub(crate) pat: Option<PatId>,
|
||||
pub(crate) pat: PatId,
|
||||
}
|
||||
|
||||
/// Close relative to rustc's hir::PatKind
|
||||
|
@ -393,7 +393,7 @@ impl Pat {
|
|||
total_iter.map(|pat| *pat).for_each(f);
|
||||
}
|
||||
Pat::Struct { args, .. } => {
|
||||
args.iter().filter_map(|a| a.pat).for_each(f);
|
||||
args.iter().map(|f| f.pat).for_each(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -821,9 +821,10 @@ impl ExprCollector {
|
|||
.expect("every struct should have a field list")
|
||||
.field_pats()
|
||||
.into_iter()
|
||||
.map(|f| FieldPat {
|
||||
name: Name::new(f.ident),
|
||||
pat: f.pat.as_ref().map(|p| self.collect_pat(p)),
|
||||
.map(|f| {
|
||||
let name = Name::new(f.ident);
|
||||
let pat = self.collect_pat(&*f.pat);
|
||||
FieldPat { name, pat }
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
|
|
@ -937,19 +937,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||
};
|
||||
|
||||
for sub_pat in sub_pats {
|
||||
let tyref = fields
|
||||
.iter()
|
||||
.find(|field| field.name == sub_pat.name)
|
||||
.map(|field| &field.type_ref);
|
||||
let matching_field = fields.iter().find(|field| field.name == sub_pat.name);
|
||||
|
||||
if let Some(typeref) = tyref {
|
||||
if let Some(field) = matching_field {
|
||||
let typeref = &field.type_ref;
|
||||
let sub_ty = Ty::from_hir(self.db, &self.module, self.impl_block.as_ref(), typeref);
|
||||
|
||||
if let Some(pat) = sub_pat.pat {
|
||||
self.infer_pat(pat, &Expectation::has_type(sub_ty));
|
||||
} else {
|
||||
// TODO: deal with this case: S { x, y }
|
||||
}
|
||||
self.infer_pat(sub_pat.pat, &Expectation::has_type(sub_ty));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -389,6 +389,11 @@ fn test() {
|
|||
|
||||
let S(y, z) = foo;
|
||||
let E::A { x: new_var } = e;
|
||||
|
||||
match e {
|
||||
E::A { x } => x,
|
||||
E::B => 1,
|
||||
};
|
||||
}
|
||||
"#,
|
||||
"adt_pattern.txt",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue