mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-22 12:37:32 +00:00
Move pattern type inference from adt::StructField to core_model_impl (WIP)
This commit is contained in:
parent
b5466f3fb3
commit
d2769837f1
@ -51,6 +51,7 @@ pub use self::code_model_api::{
|
|||||||
Module, ModuleSource, Problem,
|
Module, ModuleSource, Problem,
|
||||||
Struct, Enum, EnumVariant,
|
Struct, Enum, EnumVariant,
|
||||||
Function, FnSignature, ScopeEntryWithSyntax,
|
Function, FnSignature, ScopeEntryWithSyntax,
|
||||||
|
StructField,
|
||||||
Static, Const,
|
Static, Const,
|
||||||
Trait, Type,
|
Trait, Type,
|
||||||
};
|
};
|
||||||
|
@ -31,9 +31,8 @@ use join_to_string::join;
|
|||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Def, DefId, Module, Function, Struct, Enum, EnumVariant, Path, Name, ImplBlock,
|
Def, DefId, Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock,
|
||||||
FnSignature, FnScopes,
|
FnSignature, FnScopes,
|
||||||
adt::StructField,
|
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
type_ref::{TypeRef, Mutability},
|
type_ref::{TypeRef, Mutability},
|
||||||
name::KnownName,
|
name::KnownName,
|
||||||
@ -873,24 +872,22 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add fields method for tuple like structs and variants
|
||||||
|
// TODO: and add tests!
|
||||||
|
|
||||||
fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> {
|
fn resolve_fields(&self, path: Option<&Path>) -> Option<(Ty, Vec<StructField>)> {
|
||||||
let def_id = self.module.resolve_path(self.db, path?).take_types()?;
|
let def_id = self.module.resolve_path(self.db, path?).take_types()?;
|
||||||
let def = def_id.resolve(self.db);
|
let def = def_id.resolve(self.db);
|
||||||
|
|
||||||
match def {
|
match def {
|
||||||
Def::Struct(s) => {
|
Def::Struct(s) => {
|
||||||
let fields: Vec<_> = self
|
let fields: Vec<_> = s.fields(self.db);
|
||||||
.db
|
|
||||||
.struct_data(s.def_id())
|
|
||||||
.variant_data
|
|
||||||
.fields()
|
|
||||||
.to_owned();
|
|
||||||
Some((type_for_struct(self.db, s), fields))
|
Some((type_for_struct(self.db, s), fields))
|
||||||
}
|
}
|
||||||
Def::EnumVariant(ev) => {
|
// Def::EnumVariant(ev) => {
|
||||||
let fields: Vec<_> = ev.variant_data(self.db).fields().to_owned();
|
// let fields: Vec<_> = ev.variant_data(self.db).fields().to_owned();
|
||||||
Some((type_for_enum_variant(self.db, ev), fields))
|
// Some((type_for_enum_variant(self.db, ev), fields))
|
||||||
}
|
// }
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -903,7 +900,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
for (i, &subpat) in subpats.iter().enumerate() {
|
for (i, &subpat) in subpats.iter().enumerate() {
|
||||||
let expected_ty = fields
|
let expected_ty = fields
|
||||||
.get(i)
|
.get(i)
|
||||||
.map_or(Ty::Unknown, |field| self.make_ty(&field.type_ref));
|
.and_then(|field| field.ty(self.db))
|
||||||
|
.unwrap_or(Ty::Unknown);
|
||||||
self.infer_pat(subpat, &Expectation::has_type(expected_ty));
|
self.infer_pat(subpat, &Expectation::has_type(expected_ty));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -916,9 +914,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
.unwrap_or((Ty::Unknown, Vec::new()));
|
.unwrap_or((Ty::Unknown, Vec::new()));
|
||||||
|
|
||||||
for subpat in subpats {
|
for subpat in subpats {
|
||||||
let matching_field = fields.iter().find(|field| field.name == subpat.name);
|
let matching_field = fields.iter().find(|field| field.name() == &subpat.name);
|
||||||
let expected_ty =
|
let expected_ty = matching_field
|
||||||
matching_field.map_or(Ty::Unknown, |field| self.make_ty(&field.type_ref));
|
.and_then(|field| field.ty(self.db))
|
||||||
|
.unwrap_or(Ty::Unknown);
|
||||||
self.infer_pat(subpat.pat, &Expectation::has_type(expected_ty));
|
self.infer_pat(subpat.pat, &Expectation::has_type(expected_ty));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user