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
}