Get rid of field_type again

This commit is contained in:
Florian Diebold 2021-05-23 23:54:35 +02:00
parent e65803748d
commit b8262099cc
6 changed files with 16 additions and 24 deletions

View file

@ -514,8 +514,7 @@ impl Field {
/// Returns the type as in the signature of the struct (i.e., with
/// placeholder types for type parameters). Only use this in the context of
/// the field *definition*; if you've already got a variable of the struct
/// type, use `Type::field_type` to get to the field type.
/// the field definition.
pub fn ty(&self, db: &dyn HirDatabase) -> Type {
let var_id = self.parent.into();
let generic_def_id: GenericDefId = match self.parent {
@ -1944,18 +1943,6 @@ impl Type {
}
}
pub fn field_type(&self, db: &dyn HirDatabase, field: Field) -> Option<Type> {
let (adt_id, substs) = self.ty.as_adt()?;
let variant_id: hir_def::VariantId = field.parent.into();
if variant_id.adt_id() != adt_id {
return None;
}
let ty = db.field_types(variant_id).get(field.id)?.clone();
let ty = ty.substitute(&Interner, substs);
Some(self.derived(ty))
}
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
let (variant_id, substs) = match self.ty.kind(&Interner) {
&TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs),

View file

@ -227,7 +227,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
pub fn resolve_record_field(
&self,
field: &ast::RecordExprField,
) -> Option<(Field, Option<Local>)> {
) -> Option<(Field, Option<Local>, Type)> {
self.imp.resolve_record_field(field)
}
@ -518,7 +518,10 @@ impl<'db> SemanticsImpl<'db> {
self.analyze(field.syntax()).resolve_field(self.db, field)
}
fn resolve_record_field(&self, field: &ast::RecordExprField) -> Option<(Field, Option<Local>)> {
fn resolve_record_field(
&self,
field: &ast::RecordExprField,
) -> Option<(Field, Option<Local>, Type)> {
self.analyze(field.syntax()).resolve_record_field(self.db, field)
}

View file

@ -161,7 +161,7 @@ impl SourceAnalyzer {
&self,
db: &dyn HirDatabase,
field: &ast::RecordExprField,
) -> Option<(Field, Option<Local>)> {
) -> Option<(Field, Option<Local>, Type)> {
let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?;
let expr = ast::Expr::from(record_expr);
let expr_id = self.body_source_map.as_ref()?.node_expr(InFile::new(self.file_id, &expr))?;
@ -178,10 +178,13 @@ impl SourceAnalyzer {
_ => None,
}
};
let (_, subst) = self.infer.as_ref()?.type_of_expr.get(expr_id)?.as_adt()?;
let variant = self.infer.as_ref()?.variant_resolution_for_expr(expr_id)?;
let variant_data = variant.variant_data(db.upcast());
let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? };
Some((field.into(), local))
let field_ty =
db.field_types(variant).get(field.local_id)?.clone().substitute(&Interner, subst);
Some((field.into(), local, Type::new_with_resolver(db, &self.resolver, field_ty)?))
}
pub(crate) fn resolve_record_pat_field(

View file

@ -85,7 +85,7 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let record_field: ast::RecordExprField = ctx.find_node_at_offset()?;
let (record_field_def, _) = ctx.sema.resolve_record_field(&record_field)?;
let (record_field_def, _, _) = ctx.sema.resolve_record_field(&record_field)?;
let current_module = ctx.sema.scope(record_field.syntax()).module()?;
let visibility = record_field_def.visibility(ctx.db());

View file

@ -339,13 +339,12 @@ impl<'a> CompletionContext<'a> {
cov_mark::hit!(expected_type_struct_field_without_leading_char);
// wouldn't try {} be nice...
(|| {
let record_ty = self.sema.type_of_expr(&ast::Expr::cast(node.parent()?)?)?;
let expr_field = self.token.prev_sibling_or_token()?
.into_node()
.into_node()
.and_then(|node| ast::RecordExprField::cast(node))?;
let field = self.sema.resolve_record_field(&expr_field)?.0;
let (_, _, ty) = self.sema.resolve_record_field(&expr_field)?;
Some((
record_ty.field_type(self.db, field),
Some(ty),
expr_field.field_name().map(NameOrNameRef::NameRef),
))
})().unwrap_or((None, None))

View file

@ -311,7 +311,7 @@ impl NameRefClass {
}
if let Some(record_field) = ast::RecordExprField::for_field_name(name_ref) {
if let Some((field, local)) = sema.resolve_record_field(&record_field) {
if let Some((field, local, _)) = sema.resolve_record_field(&record_field) {
let field = Definition::Field(field);
let res = match local {
None => NameRefClass::Definition(field),