Rename StructField -> Field

This commit is contained in:
Aleksey Kladov 2020-04-25 14:23:34 +02:00
parent 7bc7173230
commit 970dbf8717
27 changed files with 132 additions and 148 deletions

View file

@ -13,8 +13,8 @@ use hir_def::{
resolver::{HasResolver, Resolver}, resolver::{HasResolver, Resolver},
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
ImplId, LocalEnumVariantId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId,
StructId, TraitId, TypeAliasId, TypeParamId, UnionId, TraitId, TypeAliasId, TypeParamId, UnionId,
}; };
use hir_expand::{ use hir_expand::{
diagnostics::DiagnosticSink, diagnostics::DiagnosticSink,
@ -294,9 +294,9 @@ impl Module {
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StructField { pub struct Field {
pub(crate) parent: VariantDef, pub(crate) parent: VariantDef,
pub(crate) id: LocalStructFieldId, pub(crate) id: LocalFieldId,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -305,7 +305,7 @@ pub enum FieldSource {
Pos(ast::TupleFieldDef), Pos(ast::TupleFieldDef),
} }
impl StructField { impl Field {
pub fn name(&self, db: &dyn HirDatabase) -> Name { pub fn name(&self, db: &dyn HirDatabase) -> Name {
self.parent.variant_data(db).fields()[self.id].name.clone() self.parent.variant_data(db).fields()[self.id].name.clone()
} }
@ -331,7 +331,7 @@ impl StructField {
} }
} }
impl HasVisibility for StructField { impl HasVisibility for Field {
fn visibility(&self, db: &dyn HirDatabase) -> Visibility { fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
let variant_data = self.parent.variant_data(db); let variant_data = self.parent.variant_data(db);
let visibility = &variant_data.fields()[self.id].visibility; let visibility = &variant_data.fields()[self.id].visibility;
@ -358,12 +358,12 @@ impl Struct {
db.struct_data(self.id).name.clone() db.struct_data(self.id).name.clone()
} }
pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> { pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
db.struct_data(self.id) db.struct_data(self.id)
.variant_data .variant_data
.fields() .fields()
.iter() .iter()
.map(|(id, _)| StructField { parent: self.into(), id }) .map(|(id, _)| Field { parent: self.into(), id })
.collect() .collect()
} }
@ -394,12 +394,12 @@ impl Union {
Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id)
} }
pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> { pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
db.union_data(self.id) db.union_data(self.id)
.variant_data .variant_data
.fields() .fields()
.iter() .iter()
.map(|(id, _)| StructField { parent: self.into(), id }) .map(|(id, _)| Field { parent: self.into(), id })
.collect() .collect()
} }
@ -457,11 +457,11 @@ impl EnumVariant {
db.enum_data(self.parent.id).variants[self.id].name.clone() db.enum_data(self.parent.id).variants[self.id].name.clone()
} }
pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> { pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
self.variant_data(db) self.variant_data(db)
.fields() .fields()
.iter() .iter()
.map(|(id, _)| StructField { parent: self.into(), id }) .map(|(id, _)| Field { parent: self.into(), id })
.collect() .collect()
} }
@ -527,7 +527,7 @@ pub enum VariantDef {
impl_froms!(VariantDef: Struct, Union, EnumVariant); impl_froms!(VariantDef: Struct, Union, EnumVariant);
impl VariantDef { impl VariantDef {
pub fn fields(self, db: &dyn HirDatabase) -> Vec<StructField> { pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
match self { match self {
VariantDef::Struct(it) => it.fields(db), VariantDef::Struct(it) => it.fields(db),
VariantDef::Union(it) => it.fields(db), VariantDef::Union(it) => it.fields(db),
@ -1148,7 +1148,7 @@ impl Type {
} }
} }
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(StructField, Type)> { pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
if let Ty::Apply(a_ty) = &self.ty.value { if let Ty::Apply(a_ty) = &self.ty.value {
if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor { if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor {
let var_def = s.into(); let var_def = s.into();
@ -1156,7 +1156,7 @@ impl Type {
.field_types(var_def) .field_types(var_def)
.iter() .iter()
.map(|(local_id, ty)| { .map(|(local_id, ty)| {
let def = StructField { parent: var_def.into(), id: local_id }; let def = Field { parent: var_def.into(), id: local_id };
let ty = ty.clone().subst(&a_ty.parameters); let ty = ty.clone().subst(&a_ty.parameters);
(def, self.derived(ty)) (def, self.derived(ty))
}) })
@ -1352,7 +1352,7 @@ impl ScopeDef {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AttrDef { pub enum AttrDef {
Module(Module), Module(Module),
StructField(StructField), Field(Field),
Adt(Adt), Adt(Adt),
Function(Function), Function(Function),
EnumVariant(EnumVariant), EnumVariant(EnumVariant),
@ -1365,7 +1365,7 @@ pub enum AttrDef {
impl_froms!( impl_froms!(
AttrDef: Module, AttrDef: Module,
StructField, Field,
Adt(Struct, Enum, Union), Adt(Struct, Enum, Union),
EnumVariant, EnumVariant,
Static, Static,

View file

@ -4,13 +4,13 @@
//! are splitting the hir. //! are splitting the hir.
use hir_def::{ use hir_def::{
expr::PatId, AdtId, AssocItemId, AttrDefId, DefWithBodyId, EnumVariantId, GenericDefId, expr::PatId, AdtId, AssocItemId, AttrDefId, DefWithBodyId, EnumVariantId, FieldId,
ModuleDefId, StructFieldId, VariantId, GenericDefId, ModuleDefId, VariantId,
}; };
use crate::{ use crate::{
code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local, code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, Field, GenericDef,
MacroDef, ModuleDef, StructField, VariantDef, Local, MacroDef, ModuleDef, VariantDef,
}; };
macro_rules! from_id { macro_rules! from_id {
@ -184,15 +184,15 @@ impl From<VariantDef> for VariantId {
} }
} }
impl From<StructField> for StructFieldId { impl From<Field> for FieldId {
fn from(def: StructField) -> Self { fn from(def: Field) -> Self {
StructFieldId { parent: def.parent.into(), local_id: def.id } FieldId { parent: def.parent.into(), local_id: def.id }
} }
} }
impl From<StructFieldId> for StructField { impl From<FieldId> for Field {
fn from(def: StructFieldId) -> Self { fn from(def: FieldId) -> Self {
StructField { parent: def.parent.into(), id: def.local_id } Field { parent: def.parent.into(), id: def.local_id }
} }
} }
@ -200,7 +200,7 @@ impl From<AttrDef> for AttrDefId {
fn from(def: AttrDef) -> Self { fn from(def: AttrDef) -> Self {
match def { match def {
AttrDef::Module(it) => AttrDefId::ModuleId(it.id), AttrDef::Module(it) => AttrDefId::ModuleId(it.id),
AttrDef::StructField(it) => AttrDefId::StructFieldId(it.into()), AttrDef::Field(it) => AttrDefId::FieldId(it.into()),
AttrDef::Adt(it) => AttrDefId::AdtId(it.into()), AttrDef::Adt(it) => AttrDefId::AdtId(it.into()),
AttrDef::Function(it) => AttrDefId::FunctionId(it.id), AttrDef::Function(it) => AttrDefId::FunctionId(it.id),
AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()), AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()),

View file

@ -9,8 +9,8 @@ use hir_def::{
use ra_syntax::ast; use ra_syntax::ast;
use crate::{ use crate::{
db::HirDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module, db::HirDatabase, Const, Enum, EnumVariant, Field, FieldSource, Function, ImplDef, MacroDef,
Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, Module, Static, Struct, Trait, TypeAlias, TypeParam, Union,
}; };
pub use hir_expand::InFile; pub use hir_expand::InFile;
@ -37,7 +37,7 @@ impl Module {
} }
} }
impl HasSource for StructField { impl HasSource for Field {
type Ast = FieldSource; type Ast = FieldSource;
fn source(self, db: &dyn HirDatabase) -> InFile<FieldSource> { fn source(self, db: &dyn HirDatabase) -> InFile<FieldSource> {
let var = VariantId::from(self.parent); let var = VariantId::from(self.parent);

View file

@ -52,9 +52,9 @@ mod has_source;
pub use crate::{ pub use crate::{
code_model::{ code_model::{
Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency, Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency,
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function, GenericDef, HasAttrs,
HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
}, },
has_source::HasSource, has_source::HasSource,
semantics::{original_range, PathResolution, Semantics, SemanticsScope}, semantics::{original_range, PathResolution, Semantics, SemanticsScope},

View file

@ -23,8 +23,8 @@ use crate::{
diagnostics::Diagnostic, diagnostics::Diagnostic,
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
source_analyzer::{resolve_hir_path, SourceAnalyzer}, source_analyzer::{resolve_hir_path, SourceAnalyzer},
AssocItem, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, Name, AssocItem, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef,
Origin, Path, ScopeDef, StructField, Trait, Type, TypeParam, Name, Origin, Path, ScopeDef, Trait, Type, TypeParam,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -184,18 +184,15 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.analyze(call.syntax()).resolve_method_call(self.db, call) self.analyze(call.syntax()).resolve_method_call(self.db, call)
} }
pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<StructField> { pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
self.analyze(field.syntax()).resolve_field(self.db, field) self.analyze(field.syntax()).resolve_field(self.db, field)
} }
pub fn resolve_record_field( pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option<Local>)> {
&self,
field: &ast::RecordField,
) -> Option<(StructField, Option<Local>)> {
self.analyze(field.syntax()).resolve_record_field(self.db, field) self.analyze(field.syntax()).resolve_record_field(self.db, field)
} }
pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option<StructField> { pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option<Field> {
self.analyze(field.syntax()).resolve_record_field_pat(self.db, field) self.analyze(field.syntax()).resolve_record_field_pat(self.db, field)
} }
@ -216,19 +213,13 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
// FIXME: use this instead? // FIXME: use this instead?
// pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>; // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
pub fn record_literal_missing_fields( pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> {
&self,
literal: &ast::RecordLit,
) -> Vec<(StructField, Type)> {
self.analyze(literal.syntax()) self.analyze(literal.syntax())
.record_literal_missing_fields(self.db, literal) .record_literal_missing_fields(self.db, literal)
.unwrap_or_default() .unwrap_or_default()
} }
pub fn record_pattern_missing_fields( pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> {
&self,
pattern: &ast::RecordPat,
) -> Vec<(StructField, Type)> {
self.analyze(pattern.syntax()) self.analyze(pattern.syntax())
.record_pattern_missing_fields(self.db, pattern) .record_pattern_missing_fields(self.db, pattern)
.unwrap_or_default() .unwrap_or_default()
@ -359,8 +350,8 @@ to_def_impls![
(crate::Const, ast::ConstDef, const_to_def), (crate::Const, ast::ConstDef, const_to_def),
(crate::Static, ast::StaticDef, static_to_def), (crate::Static, ast::StaticDef, static_to_def),
(crate::Function, ast::FnDef, fn_to_def), (crate::Function, ast::FnDef, fn_to_def),
(crate::StructField, ast::RecordFieldDef, record_field_to_def), (crate::Field, ast::RecordFieldDef, record_field_to_def),
(crate::StructField, ast::TupleFieldDef, tuple_field_to_def), (crate::Field, ast::TupleFieldDef, tuple_field_to_def),
(crate::EnumVariant, ast::EnumVariant, enum_variant_to_def), (crate::EnumVariant, ast::EnumVariant, enum_variant_to_def),
(crate::TypeParam, ast::TypeParam, type_param_to_def), (crate::TypeParam, ast::TypeParam, type_param_to_def),
(crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros

View file

@ -5,8 +5,8 @@ use hir_def::{
dyn_map::DynMap, dyn_map::DynMap,
expr::PatId, expr::PatId,
keys::{self, Key}, keys::{self, Key},
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, ImplId,
StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
}; };
use hir_expand::{name::AsName, AstId, MacroDefKind}; use hir_expand::{name::AsName, AstId, MacroDefKind};
use ra_db::FileId; use ra_db::FileId;
@ -97,13 +97,13 @@ impl SourceToDefCtx<'_, '_> {
pub(super) fn record_field_to_def( pub(super) fn record_field_to_def(
&mut self, &mut self,
src: InFile<ast::RecordFieldDef>, src: InFile<ast::RecordFieldDef>,
) -> Option<StructFieldId> { ) -> Option<FieldId> {
self.to_def(src, keys::RECORD_FIELD) self.to_def(src, keys::RECORD_FIELD)
} }
pub(super) fn tuple_field_to_def( pub(super) fn tuple_field_to_def(
&mut self, &mut self,
src: InFile<ast::TupleFieldDef>, src: InFile<ast::TupleFieldDef>,
) -> Option<StructFieldId> { ) -> Option<FieldId> {
self.to_def(src, keys::TUPLE_FIELD) self.to_def(src, keys::TUPLE_FIELD)
} }
pub(super) fn enum_variant_to_def( pub(super) fn enum_variant_to_def(

View file

@ -14,7 +14,7 @@ use hir_def::{
}, },
expr::{ExprId, Pat, PatId}, expr::{ExprId, Pat, PatId},
resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
AsMacroCall, DefWithBodyId, LocalStructFieldId, StructFieldId, VariantId, AsMacroCall, DefWithBodyId, FieldId, LocalFieldId, VariantId,
}; };
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
use hir_ty::{ use hir_ty::{
@ -27,8 +27,8 @@ use ra_syntax::{
}; };
use crate::{ use crate::{
db::HirDatabase, semantics::PathResolution, Adt, Const, EnumVariant, Function, Local, MacroDef, db::HirDatabase, semantics::PathResolution, Adt, Const, EnumVariant, Field, Function, Local,
ModPath, ModuleDef, Path, PathKind, Static, Struct, StructField, Trait, Type, TypeAlias, MacroDef, ModPath, ModuleDef, Path, PathKind, Static, Struct, Trait, Type, TypeAlias,
TypeParam, TypeParam,
}; };
use ra_db::CrateId; use ra_db::CrateId;
@ -140,7 +140,7 @@ impl SourceAnalyzer {
&self, &self,
db: &dyn HirDatabase, db: &dyn HirDatabase,
field: &ast::FieldExpr, field: &ast::FieldExpr,
) -> Option<StructField> { ) -> Option<Field> {
let expr_id = self.expr_id(db, &field.clone().into())?; let expr_id = self.expr_id(db, &field.clone().into())?;
self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into())
} }
@ -149,7 +149,7 @@ impl SourceAnalyzer {
&self, &self,
db: &dyn HirDatabase, db: &dyn HirDatabase,
field: &ast::RecordField, field: &ast::RecordField,
) -> Option<(StructField, Option<Local>)> { ) -> Option<(Field, Option<Local>)> {
let expr = field.expr()?; let expr = field.expr()?;
let expr_id = self.expr_id(db, &expr)?; let expr_id = self.expr_id(db, &expr)?;
let local = if field.name_ref().is_some() { let local = if field.name_ref().is_some() {
@ -172,7 +172,7 @@ impl SourceAnalyzer {
&self, &self,
_db: &dyn HirDatabase, _db: &dyn HirDatabase,
field: &ast::RecordFieldPat, field: &ast::RecordFieldPat,
) -> Option<StructField> { ) -> Option<Field> {
let pat_id = self.pat_id(&field.pat()?)?; let pat_id = self.pat_id(&field.pat()?)?;
let struct_field = self.infer.as_ref()?.record_field_pat_resolution(pat_id)?; let struct_field = self.infer.as_ref()?.record_field_pat_resolution(pat_id)?;
Some(struct_field.into()) Some(struct_field.into())
@ -232,7 +232,7 @@ impl SourceAnalyzer {
&self, &self,
db: &dyn HirDatabase, db: &dyn HirDatabase,
literal: &ast::RecordLit, literal: &ast::RecordLit,
) -> Option<Vec<(StructField, Type)>> { ) -> Option<Vec<(Field, Type)>> {
let krate = self.resolver.krate()?; let krate = self.resolver.krate()?;
let body = self.body.as_ref()?; let body = self.body.as_ref()?;
let infer = self.infer.as_ref()?; let infer = self.infer.as_ref()?;
@ -253,7 +253,7 @@ impl SourceAnalyzer {
&self, &self,
db: &dyn HirDatabase, db: &dyn HirDatabase,
pattern: &ast::RecordPat, pattern: &ast::RecordPat,
) -> Option<Vec<(StructField, Type)>> { ) -> Option<Vec<(Field, Type)>> {
let krate = self.resolver.krate()?; let krate = self.resolver.krate()?;
let body = self.body.as_ref()?; let body = self.body.as_ref()?;
let infer = self.infer.as_ref()?; let infer = self.infer.as_ref()?;
@ -276,14 +276,14 @@ impl SourceAnalyzer {
krate: CrateId, krate: CrateId,
substs: &Substs, substs: &Substs,
variant: VariantId, variant: VariantId,
missing_fields: Vec<LocalStructFieldId>, missing_fields: Vec<LocalFieldId>,
) -> Vec<(StructField, Type)> { ) -> Vec<(Field, Type)> {
let field_types = db.field_types(variant); let field_types = db.field_types(variant);
missing_fields missing_fields
.into_iter() .into_iter()
.map(|local_id| { .map(|local_id| {
let field = StructFieldId { parent: variant, local_id }; let field = FieldId { parent: variant, local_id };
let ty = field_types[local_id].clone().subst(substs); let ty = field_types[local_id].clone().subst(substs);
(field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty)) (field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty))
}) })

View file

@ -14,7 +14,7 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner};
use crate::{ use crate::{
body::CfgExpander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, body::CfgExpander, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace,
type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId, type_ref::TypeRef, visibility::RawVisibility, EnumId, HasModule, LocalEnumVariantId,
LocalStructFieldId, Lookup, ModuleId, StructId, UnionId, VariantId, LocalFieldId, Lookup, ModuleId, StructId, UnionId, VariantId,
}; };
/// Note that we use `StructData` for unions as well! /// Note that we use `StructData` for unions as well!
@ -38,14 +38,14 @@ pub struct EnumVariantData {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum VariantData { pub enum VariantData {
Record(Arena<StructFieldData>), Record(Arena<FieldData>),
Tuple(Arena<StructFieldData>), Tuple(Arena<FieldData>),
Unit, Unit,
} }
/// A single field of an enum variant or struct /// A single field of an enum variant or struct
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructFieldData { pub struct FieldData {
pub name: Name, pub name: Name,
pub type_ref: TypeRef, pub type_ref: TypeRef,
pub visibility: RawVisibility, pub visibility: RawVisibility,
@ -133,15 +133,15 @@ impl VariantData {
} }
} }
pub fn fields(&self) -> &Arena<StructFieldData> { pub fn fields(&self) -> &Arena<FieldData> {
const EMPTY: &Arena<StructFieldData> = &Arena::new(); const EMPTY: &Arena<FieldData> = &Arena::new();
match &self { match &self {
VariantData::Record(fields) | VariantData::Tuple(fields) => fields, VariantData::Record(fields) | VariantData::Tuple(fields) => fields,
_ => EMPTY, _ => EMPTY,
} }
} }
pub fn field(&self, name: &Name) -> Option<LocalStructFieldId> { pub fn field(&self, name: &Name) -> Option<LocalFieldId> {
self.fields().iter().find_map(|(id, data)| if &data.name == name { Some(id) } else { None }) self.fields().iter().find_map(|(id, data)| if &data.name == name { Some(id) } else { None })
} }
@ -155,7 +155,7 @@ impl VariantData {
} }
impl HasChildSource for VariantId { impl HasChildSource for VariantId {
type ChildId = LocalStructFieldId; type ChildId = LocalFieldId;
type Value = Either<ast::TupleFieldDef, ast::RecordFieldDef>; type Value = Either<ast::TupleFieldDef, ast::RecordFieldDef>;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
@ -195,7 +195,7 @@ pub enum StructKind {
fn lower_struct( fn lower_struct(
db: &dyn DefDatabase, db: &dyn DefDatabase,
expander: &mut CfgExpander, expander: &mut CfgExpander,
trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>, trace: &mut Trace<FieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>,
ast: &InFile<ast::StructKind>, ast: &InFile<ast::StructKind>,
) -> StructKind { ) -> StructKind {
match &ast.value { match &ast.value {
@ -208,7 +208,7 @@ fn lower_struct(
trace.alloc( trace.alloc(
|| Either::Left(fd.clone()), || Either::Left(fd.clone()),
|| StructFieldData { || FieldData {
name: Name::new_tuple_field(i), name: Name::new_tuple_field(i),
type_ref: TypeRef::from_ast_opt(fd.type_ref()), type_ref: TypeRef::from_ast_opt(fd.type_ref()),
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
@ -226,7 +226,7 @@ fn lower_struct(
trace.alloc( trace.alloc(
|| Either::Right(fd.clone()), || Either::Right(fd.clone()),
|| StructFieldData { || FieldData {
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),

View file

@ -43,7 +43,7 @@ impl Attrs {
}; };
Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner)) Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
} }
AttrDefId::StructFieldId(it) => { AttrDefId::FieldId(it) => {
let src = it.parent.child_source(db); let src = it.parent.child_source(db);
match &src.value[it.local_id] { match &src.value[it.local_id] {
Either::Left(_tuple) => Attrs::default(), Either::Left(_tuple) => Attrs::default(),

View file

@ -12,8 +12,8 @@ use crate::{
item_scope::ItemScope, item_scope::ItemScope,
keys, keys,
src::{HasChildSource, HasSource}, src::{HasChildSource, HasSource},
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, ModuleDefId,
ModuleId, StructFieldId, TraitId, VariantId, ModuleId, TraitId, VariantId,
}; };
pub trait ChildBySource { pub trait ChildBySource {
@ -140,7 +140,7 @@ impl ChildBySource for VariantId {
let arena_map = self.child_source(db); let arena_map = self.child_source(db);
let arena_map = arena_map.as_ref(); let arena_map = arena_map.as_ref();
for (local_id, source) in arena_map.value.iter() { for (local_id, source) in arena_map.value.iter() {
let id = StructFieldId { parent: *self, local_id }; let id = FieldId { parent: *self, local_id };
match source { match source {
Either::Left(source) => { Either::Left(source) => {
res[keys::TUPLE_FIELD].insert(arena_map.with_value(source.clone()), id) res[keys::TUPLE_FIELD].insert(arena_map.with_value(source.clone()), id)

View file

@ -43,7 +43,7 @@ impl Documentation {
let src = def_map[module.local_id].declaration_source(db)?; let src = def_map[module.local_id].declaration_source(db)?;
docs_from_ast(&src.value) docs_from_ast(&src.value)
} }
AttrDefId::StructFieldId(it) => { AttrDefId::FieldId(it) => {
let src = it.parent.child_source(db); let src = it.parent.child_source(db);
match &src.value[it.local_id] { match &src.value[it.local_id] {
Either::Left(_tuple) => None, Either::Left(_tuple) => None,

View file

@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
use crate::{ use crate::{
dyn_map::{DynMap, Policy}, dyn_map::{DynMap, Policy},
ConstId, EnumId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId, ConstId, EnumId, EnumVariantId, FieldId, FunctionId, ImplId, StaticId, StructId, TraitId,
TypeAliasId, TypeParamId, UnionId, TypeAliasId, TypeParamId, UnionId,
}; };
@ -25,8 +25,8 @@ pub const UNION: Key<ast::UnionDef, UnionId> = Key::new();
pub const ENUM: Key<ast::EnumDef, EnumId> = Key::new(); pub const ENUM: Key<ast::EnumDef, EnumId> = Key::new();
pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new(); pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); pub const TUPLE_FIELD: Key<ast::TupleFieldDef, FieldId> = Key::new();
pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); pub const RECORD_FIELD: Key<ast::RecordFieldDef, FieldId> = Key::new();
pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new(); pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
pub const MACRO: Key<ast::MacroCall, MacroDefId> = Key::new(); pub const MACRO: Key<ast::MacroCall, MacroDefId> = Key::new();

View file

@ -133,12 +133,12 @@ pub struct EnumVariantId {
pub type LocalEnumVariantId = Idx<adt::EnumVariantData>; pub type LocalEnumVariantId = Idx<adt::EnumVariantData>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StructFieldId { pub struct FieldId {
pub parent: VariantId, pub parent: VariantId,
pub local_id: LocalStructFieldId, pub local_id: LocalFieldId,
} }
pub type LocalStructFieldId = Idx<adt::StructFieldData>; pub type LocalFieldId = Idx<adt::FieldData>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ConstId(salsa::InternId); pub struct ConstId(salsa::InternId);
@ -299,7 +299,7 @@ impl From<AssocItemId> for GenericDefId {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AttrDefId { pub enum AttrDefId {
ModuleId(ModuleId), ModuleId(ModuleId),
StructFieldId(StructFieldId), FieldId(FieldId),
AdtId(AdtId), AdtId(AdtId),
FunctionId(FunctionId), FunctionId(FunctionId),
EnumVariantId(EnumVariantId), EnumVariantId(EnumVariantId),
@ -313,7 +313,7 @@ pub enum AttrDefId {
impl_froms!( impl_froms!(
AttrDefId: ModuleId, AttrDefId: ModuleId,
StructFieldId, FieldId,
AdtId(StructId, EnumId, UnionId), AdtId(StructId, EnumId, UnionId),
EnumVariantId, EnumVariantId,
StaticId, StaticId,

View file

@ -3,7 +3,7 @@
use std::sync::Arc; use std::sync::Arc;
use hir_def::{ use hir_def::{
db::DefDatabase, DefWithBodyId, GenericDefId, ImplId, LocalStructFieldId, TraitId, TypeParamId, db::DefDatabase, DefWithBodyId, GenericDefId, ImplId, LocalFieldId, TraitId, TypeParamId,
VariantId, VariantId,
}; };
use ra_arena::map::ArenaMap; use ra_arena::map::ArenaMap;
@ -43,7 +43,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>; fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
#[salsa::invoke(crate::lower::field_types_query)] #[salsa::invoke(crate::lower::field_types_query)]
fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalStructFieldId, Binders<Ty>>>; fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>>;
#[salsa::invoke(crate::callable_item_sig)] #[salsa::invoke(crate::callable_item_sig)]
fn callable_item_signature(&self, def: CallableDef) -> PolyFnSig; fn callable_item_signature(&self, def: CallableDef) -> PolyFnSig;

View file

@ -24,7 +24,7 @@ pub use hir_def::{
ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp,
MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
}, },
LocalStructFieldId, VariantId, LocalFieldId, VariantId,
}; };
pub struct ExprValidator<'a, 'b: 'a> { pub struct ExprValidator<'a, 'b: 'a> {
@ -83,7 +83,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
id: ExprId, id: ExprId,
db: &dyn HirDatabase, db: &dyn HirDatabase,
variant_def: VariantId, variant_def: VariantId,
missed_fields: Vec<LocalStructFieldId>, missed_fields: Vec<LocalFieldId>,
) { ) {
// XXX: only look at source_map if we do have missing fields // XXX: only look at source_map if we do have missing fields
let (_, source_map) = db.body_with_source_map(self.func.into()); let (_, source_map) = db.body_with_source_map(self.func.into());
@ -112,7 +112,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
id: PatId, id: PatId,
db: &dyn HirDatabase, db: &dyn HirDatabase,
variant_def: VariantId, variant_def: VariantId,
missed_fields: Vec<LocalStructFieldId>, missed_fields: Vec<LocalFieldId>,
) { ) {
// XXX: only look at source_map if we do have missing fields // XXX: only look at source_map if we do have missing fields
let (_, source_map) = db.body_with_source_map(self.func.into()); let (_, source_map) = db.body_with_source_map(self.func.into());
@ -256,7 +256,7 @@ pub fn record_literal_missing_fields(
infer: &InferenceResult, infer: &InferenceResult,
id: ExprId, id: ExprId,
expr: &Expr, expr: &Expr,
) -> Option<(VariantId, Vec<LocalStructFieldId>, /*exhaustive*/ bool)> { ) -> Option<(VariantId, Vec<LocalFieldId>, /*exhaustive*/ bool)> {
let (fields, exhausitve) = match expr { let (fields, exhausitve) = match expr {
Expr::RecordLit { path: _, fields, spread } => (fields, spread.is_none()), Expr::RecordLit { path: _, fields, spread } => (fields, spread.is_none()),
_ => return None, _ => return None,
@ -270,7 +270,7 @@ pub fn record_literal_missing_fields(
let variant_data = variant_data(db.upcast(), variant_def); let variant_data = variant_data(db.upcast(), variant_def);
let specified_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect(); let specified_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
let missed_fields: Vec<LocalStructFieldId> = variant_data let missed_fields: Vec<LocalFieldId> = variant_data
.fields() .fields()
.iter() .iter()
.filter_map(|(f, d)| if specified_fields.contains(&d.name) { None } else { Some(f) }) .filter_map(|(f, d)| if specified_fields.contains(&d.name) { None } else { Some(f) })
@ -286,7 +286,7 @@ pub fn record_pattern_missing_fields(
infer: &InferenceResult, infer: &InferenceResult,
id: PatId, id: PatId,
pat: &Pat, pat: &Pat,
) -> Option<(VariantId, Vec<LocalStructFieldId>, /*exhaustive*/ bool)> { ) -> Option<(VariantId, Vec<LocalFieldId>, /*exhaustive*/ bool)> {
let (fields, exhaustive) = match pat { let (fields, exhaustive) = match pat {
Pat::Record { path: _, args, ellipsis } => (args, !ellipsis), Pat::Record { path: _, args, ellipsis } => (args, !ellipsis),
_ => return None, _ => return None,
@ -300,7 +300,7 @@ pub fn record_pattern_missing_fields(
let variant_data = variant_data(db.upcast(), variant_def); let variant_data = variant_data(db.upcast(), variant_def);
let specified_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect(); let specified_fields: FxHashSet<_> = fields.iter().map(|f| &f.name).collect();
let missed_fields: Vec<LocalStructFieldId> = variant_data let missed_fields: Vec<LocalFieldId> = variant_data
.fields() .fields()
.iter() .iter()
.filter_map(|(f, d)| if specified_fields.contains(&d.name) { None } else { Some(f) }) .filter_map(|(f, d)| if specified_fields.contains(&d.name) { None } else { Some(f) })

View file

@ -28,7 +28,7 @@ use hir_def::{
path::{path, Path}, path::{path, Path},
resolver::{HasResolver, Resolver, TypeNs}, resolver::{HasResolver, Resolver, TypeNs},
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AdtId, AssocItemId, DefWithBodyId, FunctionId, StructFieldId, TraitId, TypeAliasId, VariantId, AdtId, AssocItemId, DefWithBodyId, FieldId, FunctionId, TraitId, TypeAliasId, VariantId,
}; };
use hir_expand::{diagnostics::DiagnosticSink, name::name}; use hir_expand::{diagnostics::DiagnosticSink, name::name};
use ra_arena::map::ArenaMap; use ra_arena::map::ArenaMap;
@ -124,10 +124,10 @@ pub struct InferenceResult {
/// For each method call expr, records the function it resolves to. /// For each method call expr, records the function it resolves to.
method_resolutions: FxHashMap<ExprId, FunctionId>, method_resolutions: FxHashMap<ExprId, FunctionId>,
/// For each field access expr, records the field it resolves to. /// For each field access expr, records the field it resolves to.
field_resolutions: FxHashMap<ExprId, StructFieldId>, field_resolutions: FxHashMap<ExprId, FieldId>,
/// For each field in record literal, records the field it resolves to. /// For each field in record literal, records the field it resolves to.
record_field_resolutions: FxHashMap<ExprId, StructFieldId>, record_field_resolutions: FxHashMap<ExprId, FieldId>,
record_field_pat_resolutions: FxHashMap<PatId, StructFieldId>, record_field_pat_resolutions: FxHashMap<PatId, FieldId>,
/// For each struct literal, records the variant it resolves to. /// For each struct literal, records the variant it resolves to.
variant_resolutions: FxHashMap<ExprOrPatId, VariantId>, variant_resolutions: FxHashMap<ExprOrPatId, VariantId>,
/// For each associated item record what it resolves to /// For each associated item record what it resolves to
@ -142,13 +142,13 @@ impl InferenceResult {
pub fn method_resolution(&self, expr: ExprId) -> Option<FunctionId> { pub fn method_resolution(&self, expr: ExprId) -> Option<FunctionId> {
self.method_resolutions.get(&expr).copied() self.method_resolutions.get(&expr).copied()
} }
pub fn field_resolution(&self, expr: ExprId) -> Option<StructFieldId> { pub fn field_resolution(&self, expr: ExprId) -> Option<FieldId> {
self.field_resolutions.get(&expr).copied() self.field_resolutions.get(&expr).copied()
} }
pub fn record_field_resolution(&self, expr: ExprId) -> Option<StructFieldId> { pub fn record_field_resolution(&self, expr: ExprId) -> Option<FieldId> {
self.record_field_resolutions.get(&expr).copied() self.record_field_resolutions.get(&expr).copied()
} }
pub fn record_field_pat_resolution(&self, pat: PatId) -> Option<StructFieldId> { pub fn record_field_pat_resolution(&self, pat: PatId) -> Option<FieldId> {
self.record_field_pat_resolutions.get(&pat).copied() self.record_field_pat_resolutions.get(&pat).copied()
} }
pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantId> { pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantId> {
@ -249,7 +249,7 @@ impl<'a> InferenceContext<'a> {
self.result.method_resolutions.insert(expr, func); self.result.method_resolutions.insert(expr, func);
} }
fn write_field_resolution(&mut self, expr: ExprId, field: StructFieldId) { fn write_field_resolution(&mut self, expr: ExprId, field: FieldId) {
self.result.field_resolutions.insert(expr, field); self.result.field_resolutions.insert(expr, field);
} }

View file

@ -8,7 +8,7 @@ use hir_def::{
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
path::{GenericArg, GenericArgs}, path::{GenericArg, GenericArgs},
resolver::resolver_for_expr, resolver::resolver_for_expr,
AdtId, AssocContainerId, Lookup, StructFieldId, AdtId, AssocContainerId, FieldId, Lookup,
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
use ra_syntax::ast::RangeOp; use ra_syntax::ast::RangeOp;
@ -216,9 +216,7 @@ impl<'a> InferenceContext<'a> {
for (field_idx, field) in fields.iter().enumerate() { for (field_idx, field) in fields.iter().enumerate() {
let field_def = let field_def =
variant_data.as_ref().and_then(|it| match it.field(&field.name) { variant_data.as_ref().and_then(|it| match it.field(&field.name) {
Some(local_id) => { Some(local_id) => Some(FieldId { parent: def_id.unwrap(), local_id }),
Some(StructFieldId { parent: def_id.unwrap(), local_id })
}
None => { None => {
self.push_diagnostic(InferenceDiagnostic::NoSuchField { self.push_diagnostic(InferenceDiagnostic::NoSuchField {
expr: tgt_expr, expr: tgt_expr,
@ -257,7 +255,7 @@ impl<'a> InferenceContext<'a> {
.and_then(|idx| a_ty.parameters.0.get(idx).cloned()), .and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
TypeCtor::Adt(AdtId::StructId(s)) => { TypeCtor::Adt(AdtId::StructId(s)) => {
self.db.struct_data(s).variant_data.field(name).map(|local_id| { self.db.struct_data(s).variant_data.field(name).map(|local_id| {
let field = StructFieldId { parent: s.into(), local_id }; let field = FieldId { parent: s.into(), local_id };
self.write_field_resolution(tgt_expr, field); self.write_field_resolution(tgt_expr, field);
self.db.field_types(s.into())[field.local_id] self.db.field_types(s.into())[field.local_id]
.clone() .clone()

View file

@ -7,7 +7,7 @@ use hir_def::{
expr::{BindingAnnotation, Pat, PatId, RecordFieldPat}, expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
path::Path, path::Path,
type_ref::Mutability, type_ref::Mutability,
StructFieldId, FieldId,
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
use test_utils::tested_by; use test_utils::tested_by;
@ -69,7 +69,7 @@ impl<'a> InferenceContext<'a> {
for subpat in subpats { for subpat in subpats {
let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name)); let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name));
if let Some(local_id) = matching_field { if let Some(local_id) = matching_field {
let field_def = StructFieldId { parent: def.unwrap(), local_id }; let field_def = FieldId { parent: def.unwrap(), local_id };
self.result.record_field_pat_resolutions.insert(subpat.pat, field_def); self.result.record_field_pat_resolutions.insert(subpat.pat, field_def);
} }

View file

@ -18,8 +18,8 @@ use hir_def::{
resolver::{HasResolver, Resolver, TypeNs}, resolver::{HasResolver, Resolver, TypeNs},
type_ref::{TypeBound, TypeRef}, type_ref::{TypeBound, TypeRef},
AdtId, AssocContainerId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, AdtId, AssocContainerId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule,
ImplId, LocalStructFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
UnionId, VariantId, VariantId,
}; };
use ra_arena::map::ArenaMap; use ra_arena::map::ArenaMap;
use ra_db::CrateId; use ra_db::CrateId;
@ -682,7 +682,7 @@ pub fn callable_item_sig(db: &dyn HirDatabase, def: CallableDef) -> PolyFnSig {
pub(crate) fn field_types_query( pub(crate) fn field_types_query(
db: &dyn HirDatabase, db: &dyn HirDatabase,
variant_id: VariantId, variant_id: VariantId,
) -> Arc<ArenaMap<LocalStructFieldId, Binders<Ty>>> { ) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>> {
let var_data = variant_data(db.upcast(), variant_id); let var_data = variant_data(db.upcast(), variant_id);
let (resolver, def): (_, GenericDefId) = match variant_id { let (resolver, def): (_, GenericDefId) = match variant_id {
VariantId::StructId(it) => (it.resolver(db.upcast()), it.into()), VariantId::StructId(it) => (it.resolver(db.upcast()), it.into()),

View file

@ -15,12 +15,7 @@ use crate::{
}; };
impl Completions { impl Completions {
pub(crate) fn add_field( pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
&mut self,
ctx: &CompletionContext,
field: hir::StructField,
ty: &Type,
) {
let is_deprecated = is_deprecated(field, ctx.db); let is_deprecated = is_deprecated(field, ctx.db);
let ty = ty.display(ctx.db).to_string(); let ty = ty.display(ctx.db).to_string();
let name = field.name(ctx.db); let name = field.name(ctx.db);

View file

@ -189,7 +189,7 @@ impl TryToNav for Definition {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
match self { match self {
Definition::Macro(it) => Some(it.to_nav(db)), Definition::Macro(it) => Some(it.to_nav(db)),
Definition::StructField(it) => Some(it.to_nav(db)), Definition::Field(it) => Some(it.to_nav(db)),
Definition::ModuleDef(it) => it.try_to_nav(db), Definition::ModuleDef(it) => it.try_to_nav(db),
Definition::SelfType(it) => Some(it.to_nav(db)), Definition::SelfType(it) => Some(it.to_nav(db)),
Definition::Local(it) => Some(it.to_nav(db)), Definition::Local(it) => Some(it.to_nav(db)),
@ -286,7 +286,7 @@ impl ToNav for hir::ImplDef {
} }
} }
impl ToNav for hir::StructField { impl ToNav for hir::Field {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db); let src = self.source(db);

View file

@ -76,7 +76,7 @@ fn hover_text(
fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> { fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> {
match def { match def {
Definition::StructField(f) => Some(f.parent_def(db).name(db)), Definition::Field(f) => Some(f.parent_def(db).name(db)),
Definition::Local(l) => l.parent(db).name(db), Definition::Local(l) => l.parent(db).name(db),
Definition::ModuleDef(md) => match md { Definition::ModuleDef(md) => match md {
ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) { ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) {
@ -116,7 +116,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
let src = it.source(db); let src = it.source(db);
hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)), mod_path) hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)), mod_path)
} }
Definition::StructField(it) => { Definition::Field(it) => {
let src = it.source(db); let src = it.source(db);
match src.value { match src.value {
FieldSource::Named(it) => { FieldSource::Named(it) => {

View file

@ -144,7 +144,7 @@ fn find_name(
fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
match def { match def {
Definition::Local(_) | Definition::StructField(_) => {} Definition::Local(_) | Definition::Field(_) => {}
_ => return None, _ => return None,
}; };

View file

@ -51,12 +51,12 @@ fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFil
let mut replacement_text = String::new(); let mut replacement_text = String::new();
let file_id = reference.file_range.file_id; let file_id = reference.file_range.file_id;
let range = match reference.kind { let range = match reference.kind {
ReferenceKind::StructFieldShorthandForField => { ReferenceKind::FieldShorthandForField => {
replacement_text.push_str(new_name); replacement_text.push_str(new_name);
replacement_text.push_str(": "); replacement_text.push_str(": ");
TextRange::new(reference.file_range.range.start(), reference.file_range.range.start()) TextRange::new(reference.file_range.range.start(), reference.file_range.range.start())
} }
ReferenceKind::StructFieldShorthandForLocal => { ReferenceKind::FieldShorthandForLocal => {
replacement_text.push_str(": "); replacement_text.push_str(": ");
replacement_text.push_str(new_name); replacement_text.push_str(new_name);
TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())

View file

@ -422,7 +422,7 @@ fn highlight_element(
fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight { fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
match def { match def {
Definition::Macro(_) => HighlightTag::Macro, Definition::Macro(_) => HighlightTag::Macro,
Definition::StructField(_) => HighlightTag::Field, Definition::Field(_) => HighlightTag::Field,
Definition::ModuleDef(def) => match def { Definition::ModuleDef(def) => match def {
hir::ModuleDef::Module(_) => HighlightTag::Module, hir::ModuleDef::Module(_) => HighlightTag::Module,
hir::ModuleDef::Function(_) => HighlightTag::Function, hir::ModuleDef::Function(_) => HighlightTag::Function,

View file

@ -6,8 +6,8 @@
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
use hir::{ use hir::{
HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
StructField, TypeParam, Visibility, Semantics, TypeParam, Visibility,
}; };
use ra_prof::profile; use ra_prof::profile;
use ra_syntax::{ use ra_syntax::{
@ -22,7 +22,7 @@ use crate::RootDatabase;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Definition { pub enum Definition {
Macro(MacroDef), Macro(MacroDef),
StructField(StructField), Field(Field),
ModuleDef(ModuleDef), ModuleDef(ModuleDef),
SelfType(ImplDef), SelfType(ImplDef),
Local(Local), Local(Local),
@ -33,7 +33,7 @@ impl Definition {
pub fn module(&self, db: &RootDatabase) -> Option<Module> { pub fn module(&self, db: &RootDatabase) -> Option<Module> {
match self { match self {
Definition::Macro(it) => it.module(db), Definition::Macro(it) => it.module(db),
Definition::StructField(it) => Some(it.parent_def(db).module(db)), Definition::Field(it) => Some(it.parent_def(db).module(db)),
Definition::ModuleDef(it) => it.module(db), Definition::ModuleDef(it) => it.module(db),
Definition::SelfType(it) => Some(it.module(db)), Definition::SelfType(it) => Some(it.module(db)),
Definition::Local(it) => Some(it.module(db)), Definition::Local(it) => Some(it.module(db)),
@ -46,7 +46,7 @@ impl Definition {
match self { match self {
Definition::Macro(_) => None, Definition::Macro(_) => None,
Definition::StructField(sf) => Some(sf.visibility(db)), Definition::Field(sf) => Some(sf.visibility(db)),
Definition::ModuleDef(def) => module?.visibility_of(db, def), Definition::ModuleDef(def) => module?.visibility_of(db, def),
Definition::SelfType(_) => None, Definition::SelfType(_) => None,
Definition::Local(_) => None, Definition::Local(_) => None,
@ -57,7 +57,7 @@ impl Definition {
pub fn name(&self, db: &RootDatabase) -> Option<Name> { pub fn name(&self, db: &RootDatabase) -> Option<Name> {
let name = match self { let name = match self {
Definition::Macro(it) => it.name(db)?, Definition::Macro(it) => it.name(db)?,
Definition::StructField(it) => it.name(db), Definition::Field(it) => it.name(db),
Definition::ModuleDef(def) => match def { Definition::ModuleDef(def) => match def {
hir::ModuleDef::Module(it) => it.name(db)?, hir::ModuleDef::Module(it) => it.name(db)?,
hir::ModuleDef::Function(it) => it.name(db), hir::ModuleDef::Function(it) => it.name(db),
@ -124,8 +124,8 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
Some(Definition::Local(local)) Some(Definition::Local(local))
}, },
ast::RecordFieldDef(it) => { ast::RecordFieldDef(it) => {
let field: hir::StructField = sema.to_def(&it)?; let field: hir::Field = sema.to_def(&it)?;
Some(Definition::StructField(field)) Some(Definition::Field(field))
}, },
ast::Module(it) => { ast::Module(it) => {
let def = sema.to_def(&it)?; let def = sema.to_def(&it)?;
@ -213,7 +213,7 @@ pub fn classify_name_ref(
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
tested_by!(goto_def_for_fields; force); tested_by!(goto_def_for_fields; force);
if let Some(field) = sema.resolve_field(&field_expr) { if let Some(field) = sema.resolve_field(&field_expr) {
return Some(NameRefClass::Definition(Definition::StructField(field))); return Some(NameRefClass::Definition(Definition::Field(field)));
} }
} }
@ -221,7 +221,7 @@ pub fn classify_name_ref(
tested_by!(goto_def_for_record_fields; force); tested_by!(goto_def_for_record_fields; force);
tested_by!(goto_def_for_field_init_shorthand; force); tested_by!(goto_def_for_field_init_shorthand; force);
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::StructField(field); let field = Definition::Field(field);
let res = match local { let res = match local {
None => NameRefClass::Definition(field), None => NameRefClass::Definition(field),
Some(local) => NameRefClass::FieldShorthand { field, local }, Some(local) => NameRefClass::FieldShorthand { field, local },
@ -233,7 +233,7 @@ pub fn classify_name_ref(
if let Some(record_field_pat) = ast::RecordFieldPat::cast(parent.clone()) { if let Some(record_field_pat) = ast::RecordFieldPat::cast(parent.clone()) {
tested_by!(goto_def_for_record_field_pats; force); tested_by!(goto_def_for_record_field_pats; force);
if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) { if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) {
let field = Definition::StructField(field); let field = Definition::Field(field);
return Some(NameRefClass::Definition(field)); return Some(NameRefClass::Definition(field));
} }
} }

View file

@ -28,8 +28,8 @@ pub struct Reference {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ReferenceKind { pub enum ReferenceKind {
StructFieldShorthandForField, FieldShorthandForField,
StructFieldShorthandForLocal, FieldShorthandForLocal,
StructLiteral, StructLiteral,
Other, Other,
} }
@ -242,14 +242,14 @@ impl Definition {
} }
Some(NameRefClass::FieldShorthand { local, field }) => { Some(NameRefClass::FieldShorthand { local, field }) => {
match self { match self {
Definition::StructField(_) if &field == self => refs.push(Reference { Definition::Field(_) if &field == self => refs.push(Reference {
file_range: sema.original_range(name_ref.syntax()), file_range: sema.original_range(name_ref.syntax()),
kind: ReferenceKind::StructFieldShorthandForField, kind: ReferenceKind::FieldShorthandForField,
access: reference_access(&field, &name_ref), access: reference_access(&field, &name_ref),
}), }),
Definition::Local(l) if &local == l => refs.push(Reference { Definition::Local(l) if &local == l => refs.push(Reference {
file_range: sema.original_range(name_ref.syntax()), file_range: sema.original_range(name_ref.syntax()),
kind: ReferenceKind::StructFieldShorthandForLocal, kind: ReferenceKind::FieldShorthandForLocal,
access: reference_access(&Definition::Local(local), &name_ref), access: reference_access(&Definition::Local(local), &name_ref),
}), }),
@ -267,7 +267,7 @@ impl Definition {
fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> { fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
// Only Locals and Fields have accesses for now. // Only Locals and Fields have accesses for now.
match def { match def {
Definition::Local(_) | Definition::StructField(_) => {} Definition::Local(_) | Definition::Field(_) => {}
_ => return None, _ => return None,
}; };