From 6294fd5ec9c6946bdd91f1274956c573f9f2a136 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Nov 2019 15:34:00 +0300 Subject: Unfork struct and union ids --- crates/ra_hir/src/adt.rs | 2 +- crates/ra_hir/src/code_model.rs | 12 ++++++------ crates/ra_hir/src/code_model/src.rs | 4 ++-- crates/ra_hir/src/expr/scope.rs | 5 +---- crates/ra_hir/src/from_source.rs | 10 ++++++---- crates/ra_hir/src/ty/lower.rs | 4 ++-- crates/ra_hir_def/src/adt.rs | 16 ++++++---------- crates/ra_hir_def/src/db.rs | 12 ++++-------- crates/ra_hir_def/src/lib.rs | 28 ++++++++++++++++------------ crates/ra_hir_def/src/nameres/collector.rs | 11 +++++++---- 10 files changed, 51 insertions(+), 53 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 0436d20b7..945f236c2 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -12,7 +12,7 @@ use crate::{ impl Struct { pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc { - db.struct_data(self.id).variant_data.clone() + db.struct_data(self.id.into()).variant_data.clone() } } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9d0db8024..e5bfad3ca 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -288,7 +288,7 @@ pub struct Struct { impl Struct { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.0.module(db) } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -296,11 +296,11 @@ impl Struct { } pub fn name(self, db: &impl DefDatabase) -> Option { - db.struct_data(self.id).name.clone() + db.struct_data(self.id.into()).name.clone() } pub fn fields(self, db: &impl HirDatabase) -> Vec { - db.struct_data(self.id) + db.struct_data(self.id.into()) .variant_data .fields() .into_iter() @@ -310,7 +310,7 @@ impl Struct { } pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option { - db.struct_data(self.id) + db.struct_data(self.id.into()) .variant_data .fields() .into_iter() @@ -346,11 +346,11 @@ pub struct Union { impl Union { pub fn name(self, db: &impl DefDatabase) -> Option { - db.union_data(self.id).name.clone() + db.struct_data(self.id.into()).name.clone() } pub fn module(self, db: &impl HirDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.0.module(db) } } pub fn ty(self, db: &impl HirDatabase) -> Ty { diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 6d116ee75..247ae3e55 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -78,13 +78,13 @@ impl HasSource for StructField { impl HasSource for Struct { type Ast = ast::StructDef; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - self.id.source(db) + self.id.0.source(db) } } impl HasSource for Union { type Ast = ast::StructDef; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - self.id.source(db) + self.id.0.source(db) } } impl HasSource for Enum { diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index c14c2ab66..5a1eade2c 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -67,10 +67,7 @@ impl ExprScopes { &self.scopes[scope].entries } - pub(crate) fn scope_chain<'a>( - &'a self, - scope: Option, - ) -> impl Iterator + 'a { + pub(crate) fn scope_chain(&self, scope: Option) -> impl Iterator + '_ { std::iter::successors(scope, move |&scope| self.scopes[scope].parent) } diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 9899bdbbc..c95d2cdd0 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,5 +1,6 @@ //! FIXME: write short doc here +use hir_def::{StructId, StructOrUnionId, UnionId}; use hir_expand::name::AsName; use ra_syntax::ast::{self, AstNode, NameOwner}; @@ -15,18 +16,19 @@ pub trait FromSource: Sized { fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option; } +// FIXIME: these two impls are wrong, `ast::StructDef` might produce either a struct or a union impl FromSource for Struct { type Ast = ast::StructDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { - let id = from_source(db, src)?; - Some(Struct { id }) + let id: StructOrUnionId = from_source(db, src)?; + Some(Struct { id: StructId(id) }) } } impl FromSource for Union { type Ast = ast::StructDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { - let id = from_source(db, src)?; - Some(Union { id }) + let id: StructOrUnionId = from_source(db, src)?; + Some(Union { id: UnionId(id) }) } } impl FromSource for Enum { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index d26b16cb2..1fed5025e 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -665,7 +665,7 @@ fn type_for_builtin(def: BuiltinType) -> Ty { } fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { - let struct_data = db.struct_data(def.id); + let struct_data = db.struct_data(def.id.into()); let fields = match struct_data.variant_data.fields() { Some(fields) => fields, None => panic!("fn_sig_for_struct_constructor called on unit struct"), @@ -681,7 +681,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { /// Build the type of a tuple struct constructor. fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { - let struct_data = db.struct_data(def.id); + let struct_data = db.struct_data(def.id.into()); if struct_data.variant_data.fields().is_none() { return type_for_adt(db, def); // Unit struct } diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 8f41e55d2..a29c4d41e 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -8,7 +8,7 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; use crate::{ db::DefDatabase2, type_ref::TypeRef, AstItemDef, EnumId, LocalEnumVariantId, - LocalStructFieldId, StructId, UnionId, + LocalStructFieldId, StructOrUnionId, }; /// Note that we use `StructData` for unions as well! @@ -49,15 +49,11 @@ pub struct StructFieldData { } impl StructData { - pub(crate) fn struct_data_query(db: &impl DefDatabase2, struct_: StructId) -> Arc { - let src = struct_.source(db); - let name = src.ast.name().map(|n| n.as_name()); - let variant_data = VariantData::new(src.ast.kind()); - let variant_data = Arc::new(variant_data); - Arc::new(StructData { name, variant_data }) - } - pub(crate) fn union_data_query(db: &impl DefDatabase2, struct_: UnionId) -> Arc { - let src = struct_.source(db); + pub(crate) fn struct_data_query( + db: &impl DefDatabase2, + id: StructOrUnionId, + ) -> Arc { + let src = id.source(db); let name = src.ast.name().map(|n| n.as_name()); let variant_data = VariantData::new(src.ast.kind()); let variant_data = Arc::new(variant_data); diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index a42348101..29cf71a59 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -11,7 +11,7 @@ use crate::{ raw::{ImportSourceMap, RawItems}, CrateDefMap, }, - EnumId, StructId, UnionId, + EnumId, StructOrUnionId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -19,9 +19,8 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_function(&self, loc: crate::ItemLoc) -> crate::FunctionId; #[salsa::interned] - fn intern_struct(&self, loc: crate::ItemLoc) -> crate::StructId; - #[salsa::interned] - fn intern_union(&self, loc: crate::ItemLoc) -> crate::UnionId; + fn intern_struct_or_union(&self, loc: crate::ItemLoc) + -> crate::StructOrUnionId; #[salsa::interned] fn intern_enum(&self, loc: crate::ItemLoc) -> crate::EnumId; #[salsa::interned] @@ -49,10 +48,7 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { fn crate_def_map(&self, krate: CrateId) -> Arc; #[salsa::invoke(StructData::struct_data_query)] - fn struct_data(&self, s: StructId) -> Arc; - - #[salsa::invoke(StructData::union_data_query)] - fn union_data(&self, s: UnionId) -> Arc; + fn struct_data(&self, id: StructOrUnionId) -> Arc; #[salsa::invoke(EnumData::enum_data_query)] fn enum_data(&self, e: EnumId) -> Arc; diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 63ed2a098..239317efe 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -205,26 +205,30 @@ impl AstItemDef for FunctionId { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct StructId(salsa::InternId); -impl_intern_key!(StructId); -impl AstItemDef for StructId { +pub struct StructOrUnionId(salsa::InternId); +impl_intern_key!(StructOrUnionId); +impl AstItemDef for StructOrUnionId { fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_struct(loc) + db.intern_struct_or_union(loc) } fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_struct(self) + db.lookup_intern_struct_or_union(self) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct UnionId(salsa::InternId); -impl_intern_key!(UnionId); -impl AstItemDef for UnionId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_union(loc) +pub struct StructId(pub StructOrUnionId); +impl From for StructOrUnionId { + fn from(id: StructId) -> StructOrUnionId { + id.0 } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_union(self) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct UnionId(pub StructOrUnionId); +impl From for StructOrUnionId { + fn from(id: UnionId) -> StructOrUnionId { + id.0 } } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index aacd50df8..7e6083961 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -19,7 +19,8 @@ use crate::{ }, path::{Path, PathKind}, AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId, - LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId, + LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, StructOrUnionId, TraitId, TypeAliasId, + UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap { @@ -664,12 +665,14 @@ where PerNs::values(FunctionId::from_ast_id(ctx, ast_id).into()) } raw::DefKind::Struct(ast_id) => { - let s = StructId::from_ast_id(ctx, ast_id).into(); + let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); + let s = StructId(id).into(); PerNs::both(s, s) } raw::DefKind::Union(ast_id) => { - let s = UnionId::from_ast_id(ctx, ast_id).into(); - PerNs::both(s, s) + let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); + let u = UnionId(id).into(); + PerNs::both(u, u) } raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), raw::DefKind::Const(ast_id) => PerNs::values(ConstId::from_ast_id(ctx, ast_id).into()), -- cgit v1.2.3