Unfork struct and union ids

This commit is contained in:
Aleksey Kladov 2019-11-09 15:34:00 +03:00
parent defc7ad772
commit 6294fd5ec9
10 changed files with 51 additions and 53 deletions

View File

@ -12,7 +12,7 @@ use crate::{
impl Struct {
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
db.struct_data(self.id).variant_data.clone()
db.struct_data(self.id.into()).variant_data.clone()
}
}

View File

@ -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<Crate> {
@ -296,11 +296,11 @@ impl Struct {
}
pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
db.struct_data(self.id).name.clone()
db.struct_data(self.id.into()).name.clone()
}
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
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<StructField> {
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<Name> {
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 {

View File

@ -78,13 +78,13 @@ impl HasSource for StructField {
impl HasSource for Struct {
type Ast = ast::StructDef;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> {
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<ast::StructDef> {
self.id.source(db)
self.id.0.source(db)
}
}
impl HasSource for Enum {

View File

@ -67,10 +67,7 @@ impl ExprScopes {
&self.scopes[scope].entries
}
pub(crate) fn scope_chain<'a>(
&'a self,
scope: Option<ScopeId>,
) -> impl Iterator<Item = ScopeId> + 'a {
pub(crate) fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ {
std::iter::successors(scope, move |&scope| self.scopes[scope].parent)
}

View File

@ -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<Self::Ast>) -> Option<Self>;
}
// 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<Self::Ast>) -> Option<Self> {
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<Self::Ast>) -> Option<Self> {
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 {

View File

@ -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
}

View File

@ -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<StructData> {
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<StructData> {
let src = struct_.source(db);
pub(crate) fn struct_data_query(
db: &impl DefDatabase2,
id: StructOrUnionId,
) -> Arc<StructData> {
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);

View File

@ -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<ast::FnDef>) -> crate::FunctionId;
#[salsa::interned]
fn intern_struct(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::StructId;
#[salsa::interned]
fn intern_union(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::UnionId;
fn intern_struct_or_union(&self, loc: crate::ItemLoc<ast::StructDef>)
-> crate::StructOrUnionId;
#[salsa::interned]
fn intern_enum(&self, loc: crate::ItemLoc<ast::EnumDef>) -> crate::EnumId;
#[salsa::interned]
@ -49,10 +48,7 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>;
#[salsa::invoke(StructData::struct_data_query)]
fn struct_data(&self, s: StructId) -> Arc<StructData>;
#[salsa::invoke(StructData::union_data_query)]
fn union_data(&self, s: UnionId) -> Arc<StructData>;
fn struct_data(&self, id: StructOrUnionId) -> Arc<StructData>;
#[salsa::invoke(EnumData::enum_data_query)]
fn enum_data(&self, e: EnumId) -> Arc<EnumData>;

View File

@ -205,26 +205,30 @@ impl AstItemDef<ast::FnDef> for FunctionId {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StructId(salsa::InternId);
impl_intern_key!(StructId);
impl AstItemDef<ast::StructDef> for StructId {
pub struct StructOrUnionId(salsa::InternId);
impl_intern_key!(StructOrUnionId);
impl AstItemDef<ast::StructDef> for StructOrUnionId {
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
db.intern_struct(loc)
db.intern_struct_or_union(loc)
}
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
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<ast::StructDef> for UnionId {
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
db.intern_union(loc)
pub struct StructId(pub StructOrUnionId);
impl From<StructId> for StructOrUnionId {
fn from(id: StructId) -> StructOrUnionId {
id.0
}
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
db.lookup_intern_union(self)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnionId(pub StructOrUnionId);
impl From<UnionId> for StructOrUnionId {
fn from(id: UnionId) -> StructOrUnionId {
id.0
}
}

View File

@ -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()),