Skip to content

Commit b826209

Browse files
committed
Get rid of field_type again
1 parent e658037 commit b826209

File tree

6 files changed

+16
-24
lines changed

6 files changed

+16
-24
lines changed

crates/hir/src/lib.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,7 @@ impl Field {
514514

515515
/// Returns the type as in the signature of the struct (i.e., with
516516
/// placeholder types for type parameters). Only use this in the context of
517-
/// the field *definition*; if you've already got a variable of the struct
518-
/// type, use `Type::field_type` to get to the field type.
517+
/// the field definition.
519518
pub fn ty(&self, db: &dyn HirDatabase) -> Type {
520519
let var_id = self.parent.into();
521520
let generic_def_id: GenericDefId = match self.parent {
@@ -1944,18 +1943,6 @@ impl Type {
19441943
}
19451944
}
19461945

1947-
pub fn field_type(&self, db: &dyn HirDatabase, field: Field) -> Option<Type> {
1948-
let (adt_id, substs) = self.ty.as_adt()?;
1949-
let variant_id: hir_def::VariantId = field.parent.into();
1950-
if variant_id.adt_id() != adt_id {
1951-
return None;
1952-
}
1953-
1954-
let ty = db.field_types(variant_id).get(field.id)?.clone();
1955-
let ty = ty.substitute(&Interner, substs);
1956-
Some(self.derived(ty))
1957-
}
1958-
19591946
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
19601947
let (variant_id, substs) = match self.ty.kind(&Interner) {
19611948
&TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs),

crates/hir/src/semantics.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
227227
pub fn resolve_record_field(
228228
&self,
229229
field: &ast::RecordExprField,
230-
) -> Option<(Field, Option<Local>)> {
230+
) -> Option<(Field, Option<Local>, Type)> {
231231
self.imp.resolve_record_field(field)
232232
}
233233

@@ -518,7 +518,10 @@ impl<'db> SemanticsImpl<'db> {
518518
self.analyze(field.syntax()).resolve_field(self.db, field)
519519
}
520520

521-
fn resolve_record_field(&self, field: &ast::RecordExprField) -> Option<(Field, Option<Local>)> {
521+
fn resolve_record_field(
522+
&self,
523+
field: &ast::RecordExprField,
524+
) -> Option<(Field, Option<Local>, Type)> {
522525
self.analyze(field.syntax()).resolve_record_field(self.db, field)
523526
}
524527

crates/hir/src/source_analyzer.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl SourceAnalyzer {
161161
&self,
162162
db: &dyn HirDatabase,
163163
field: &ast::RecordExprField,
164-
) -> Option<(Field, Option<Local>)> {
164+
) -> Option<(Field, Option<Local>, Type)> {
165165
let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?;
166166
let expr = ast::Expr::from(record_expr);
167167
let expr_id = self.body_source_map.as_ref()?.node_expr(InFile::new(self.file_id, &expr))?;
@@ -178,10 +178,13 @@ impl SourceAnalyzer {
178178
_ => None,
179179
}
180180
};
181+
let (_, subst) = self.infer.as_ref()?.type_of_expr.get(expr_id)?.as_adt()?;
181182
let variant = self.infer.as_ref()?.variant_resolution_for_expr(expr_id)?;
182183
let variant_data = variant.variant_data(db.upcast());
183184
let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? };
184-
Some((field.into(), local))
185+
let field_ty =
186+
db.field_types(variant).get(field.local_id)?.clone().substitute(&Interner, subst);
187+
Some((field.into(), local, Type::new_with_resolver(db, &self.resolver, field_ty)?))
185188
}
186189

187190
pub(crate) fn resolve_record_pat_field(

crates/ide_assists/src/handlers/fix_visibility.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
8585

8686
fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
8787
let record_field: ast::RecordExprField = ctx.find_node_at_offset()?;
88-
let (record_field_def, _) = ctx.sema.resolve_record_field(&record_field)?;
88+
let (record_field_def, _, _) = ctx.sema.resolve_record_field(&record_field)?;
8989

9090
let current_module = ctx.sema.scope(record_field.syntax()).module()?;
9191
let visibility = record_field_def.visibility(ctx.db());

crates/ide_completion/src/context.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,12 @@ impl<'a> CompletionContext<'a> {
339339
cov_mark::hit!(expected_type_struct_field_without_leading_char);
340340
// wouldn't try {} be nice...
341341
(|| {
342-
let record_ty = self.sema.type_of_expr(&ast::Expr::cast(node.parent()?)?)?;
343342
let expr_field = self.token.prev_sibling_or_token()?
344-
.into_node()
343+
.into_node()
345344
.and_then(|node| ast::RecordExprField::cast(node))?;
346-
let field = self.sema.resolve_record_field(&expr_field)?.0;
345+
let (_, _, ty) = self.sema.resolve_record_field(&expr_field)?;
347346
Some((
348-
record_ty.field_type(self.db, field),
347+
Some(ty),
349348
expr_field.field_name().map(NameOrNameRef::NameRef),
350349
))
351350
})().unwrap_or((None, None))

crates/ide_db/src/defs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl NameRefClass {
311311
}
312312

313313
if let Some(record_field) = ast::RecordExprField::for_field_name(name_ref) {
314-
if let Some((field, local)) = sema.resolve_record_field(&record_field) {
314+
if let Some((field, local, _)) = sema.resolve_record_field(&record_field) {
315315
let field = Definition::Field(field);
316316
let res = match local {
317317
None => NameRefClass::Definition(field),

0 commit comments

Comments
 (0)