migrate enums to new id

This commit is contained in:
Aleksey Kladov 2019-01-24 18:56:38 +03:00
parent cefc5cbb4a
commit 566c8e321e
9 changed files with 190 additions and 142 deletions

View File

@ -13,7 +13,7 @@ use crate::{
HirDatabase, DefKind, HirDatabase, DefKind,
SourceItemId, SourceItemId,
type_ref::TypeRef, type_ref::TypeRef,
ids::{StructLoc}, ids::{StructLoc, EnumLoc},
}; };
impl Struct { impl Struct {
@ -33,6 +33,19 @@ impl Struct {
} }
} }
impl Enum {
pub(crate) fn from_ast(
db: &impl HirDatabase,
module: Module,
file_id: HirFileId,
ast: &ast::EnumDef,
) -> Enum {
let loc: EnumLoc = EnumLoc::from_ast(db, module, file_id, ast);
let id = loc.id(db);
Enum { id }
}
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct StructData { pub struct StructData {
pub(crate) name: Option<Name>, pub(crate) name: Option<Name>,
@ -55,20 +68,20 @@ impl StructData {
fn get_def_id( fn get_def_id(
db: &impl HirDatabase, db: &impl HirDatabase,
same_file_loc: &DefLoc, module: Module,
file_id: HirFileId,
node: &SyntaxNode, node: &SyntaxNode,
expected_kind: DefKind, expected_kind: DefKind,
) -> DefId { ) -> DefId {
let file_id = same_file_loc.source_item_id.file_id;
let file_items = db.file_items(file_id); let file_items = db.file_items(file_id);
let item_id = file_items.id_of(file_id, node); let item_id = file_items.id_of(file_id, node);
let source_item_id = SourceItemId { let source_item_id = SourceItemId {
file_id,
item_id: Some(item_id), item_id: Some(item_id),
..same_file_loc.source_item_id
}; };
let loc = DefLoc { let loc = DefLoc {
module: same_file_loc.module, module,
kind: expected_kind, kind: expected_kind,
source_item_id, source_item_id,
}; };
@ -87,19 +100,22 @@ impl EnumData {
EnumData { name, variants } EnumData { name, variants }
} }
pub(crate) fn enum_data_query(db: &impl HirDatabase, def_id: DefId) -> Arc<EnumData> { pub(crate) fn enum_data_query(db: &impl HirDatabase, e: Enum) -> Arc<EnumData> {
let def_loc = def_id.loc(db); let (file_id, enum_def) = e.source(db);
assert!(def_loc.kind == DefKind::Enum); let module = e.module(db);
let syntax = db.file_item(def_loc.source_item_id);
let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node");
let variants = if let Some(vl) = enum_def.variant_list() { let variants = if let Some(vl) = enum_def.variant_list() {
vl.variants() vl.variants()
.filter_map(|variant_def| { .filter_map(|variant_def| {
let name = variant_def.name().map(|n| n.as_name()); let name = variant_def.name().map(|n| n.as_name());
name.map(|n| { name.map(|n| {
let def_id = let def_id = get_def_id(
get_def_id(db, &def_loc, variant_def.syntax(), DefKind::EnumVariant); db,
module,
file_id,
variant_def.syntax(),
DefKind::EnumVariant,
);
(n, EnumVariant::new(def_id)) (n, EnumVariant::new(def_id))
}) })
}) })
@ -107,7 +123,7 @@ impl EnumData {
} else { } else {
Vec::new() Vec::new()
}; };
Arc::new(EnumData::new(enum_def, variants)) Arc::new(EnumData::new(&*enum_def, variants))
} }
} }
@ -139,14 +155,10 @@ impl EnumVariantData {
let syntax = db.file_item(def_loc.source_item_id); let syntax = db.file_item(def_loc.source_item_id);
let variant_def = ast::EnumVariant::cast(&syntax) let variant_def = ast::EnumVariant::cast(&syntax)
.expect("enum variant def should point to EnumVariant node"); .expect("enum variant def should point to EnumVariant node");
let enum_node = syntax let enum_def = variant_def.parent_enum();
.parent() let e = Enum::from_ast(db, def_loc.module, def_loc.source_item_id.file_id, enum_def);
.expect("enum variant should have enum variant list ancestor")
.parent()
.expect("enum variant list should have enum ancestor");
let enum_def_id = get_def_id(db, &def_loc, enum_node, DefKind::Enum);
Arc::new(EnumVariantData::new(variant_def, Enum::new(enum_def_id))) Arc::new(EnumVariantData::new(variant_def, e))
} }
} }

View File

@ -16,7 +16,7 @@ use crate::{
code_model_impl::def_id_to_ast, code_model_impl::def_id_to_ast,
docs::{Documentation, Docs, docs_from_ast}, docs::{Documentation, Docs, docs_from_ast},
module_tree::ModuleId, module_tree::ModuleId,
ids::{FunctionId, StructId}, ids::{FunctionId, StructId, EnumId},
}; };
/// hir::Crate describes a single crate. It's the main interface with which /// hir::Crate describes a single crate. It's the main interface with which
@ -69,30 +69,37 @@ pub enum ModuleDef {
Module(Module), Module(Module),
Function(Function), Function(Function),
Struct(Struct), Struct(Struct),
Enum(Enum),
Def(DefId), Def(DefId),
} }
//FIXME: change to from
impl Into<ModuleDef> for Module { impl From<Module> for ModuleDef {
fn into(self) -> ModuleDef { fn from(it: Module) -> ModuleDef {
ModuleDef::Module(self) ModuleDef::Module(it)
} }
} }
impl Into<ModuleDef> for Function { impl From<Function> for ModuleDef {
fn into(self) -> ModuleDef { fn from(it: Function) -> ModuleDef {
ModuleDef::Function(self) ModuleDef::Function(it)
} }
} }
impl Into<ModuleDef> for Struct { impl From<Struct> for ModuleDef {
fn into(self) -> ModuleDef { fn from(it: Struct) -> ModuleDef {
ModuleDef::Struct(self) ModuleDef::Struct(it)
} }
} }
impl Into<ModuleDef> for DefId { impl From<Enum> for ModuleDef {
fn into(self) -> ModuleDef { fn from(it: Enum) -> ModuleDef {
ModuleDef::Def(self) ModuleDef::Enum(it)
}
}
impl From<DefId> for ModuleDef {
fn from(it: DefId) -> ModuleDef {
ModuleDef::Def(it)
} }
} }
@ -249,34 +256,30 @@ impl Docs for Struct {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Enum { pub struct Enum {
pub(crate) def_id: DefId, pub(crate) id: EnumId,
} }
impl Enum { impl Enum {
pub(crate) fn new(def_id: DefId) -> Self { pub fn module(&self, db: &impl HirDatabase) -> Module {
Enum { def_id } self.id.loc(db).module
}
pub fn def_id(&self) -> DefId {
self.def_id
} }
pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
db.enum_data(self.def_id).name.clone() db.enum_data(*self).name.clone()
} }
pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> { pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> {
db.enum_data(self.def_id).variants.clone() db.enum_data(*self).variants.clone()
} }
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) { pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
def_id_to_ast(db, self.def_id) self.id.loc(db).source(db)
} }
pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
db.generic_params(self.def_id.into()) db.generic_params((*self).into())
} }
} }

View File

@ -3,7 +3,7 @@ use ra_syntax::{ast, SyntaxNode, TreeArc};
use crate::{ use crate::{
Module, ModuleSource, Problem, ModuleDef, Module, ModuleSource, Problem, ModuleDef,
Crate, Name, Path, PathKind, PerNs, Def, Crate, Name, Path, PathKind, PerNs,
module_tree::ModuleId, module_tree::ModuleId,
nameres::{ModuleScope, lower::ImportId}, nameres::{ModuleScope, lower::ImportId},
db::HirDatabase, db::HirDatabase,
@ -135,10 +135,7 @@ impl Module {
None => PerNs::none(), None => PerNs::none(),
} }
} }
ModuleDef::Function(_) | ModuleDef::Struct(_) => PerNs::none(), ModuleDef::Enum(e) => {
ModuleDef::Def(def) => {
match def.resolve(db) {
Def::Enum(e) => {
// enum variant // enum variant
let matching_variant = e let matching_variant = e
.variants(db) .variants(db)
@ -146,20 +143,17 @@ impl Module {
.find(|(n, _variant)| n == &segment.name); .find(|(n, _variant)| n == &segment.name);
match matching_variant { match matching_variant {
Some((_n, variant)) => { Some((_n, variant)) => PerNs::both(variant.def_id().into(), (*e).into()),
PerNs::both(variant.def_id().into(), e.def_id().into())
}
None => PerNs::none(), None => PerNs::none(),
} }
} }
_ => { ModuleDef::Function(_) | ModuleDef::Struct(_) => {
// could be an inherent method call in UFCS form // could be an inherent method call in UFCS form
// (`Struct::method`), or some other kind of associated // (`Struct::method`), or some other kind of associated
// item... Which we currently don't handle (TODO) // item... Which we currently don't handle (TODO)
PerNs::none() PerNs::none()
} }
} ModuleDef::Def(_) => PerNs::none(),
}
}; };
} }
curr_per_ns curr_per_ns

View File

@ -8,7 +8,7 @@ use crate::{
SourceFileItems, SourceItemId, Crate, Module, HirInterner, SourceFileItems, SourceItemId, Crate, Module, HirInterner,
query_definitions, query_definitions,
Function, FnSignature, FnScopes, Function, FnSignature, FnScopes,
Struct, Struct, Enum,
macros::MacroExpansion, macros::MacroExpansion,
module_tree::ModuleTree, module_tree::ModuleTree,
nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}}, nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}},
@ -30,10 +30,10 @@ pub trait HirDatabase: SyntaxDatabase + AsRef<HirInterner> {
fn fn_scopes(&self, func: Function) -> Arc<FnScopes>; fn fn_scopes(&self, func: Function) -> Arc<FnScopes>;
#[salsa::invoke(crate::adt::StructData::struct_data_query)] #[salsa::invoke(crate::adt::StructData::struct_data_query)]
fn struct_data(&self, struct_: Struct) -> Arc<StructData>; fn struct_data(&self, s: Struct) -> Arc<StructData>;
#[salsa::invoke(crate::adt::EnumData::enum_data_query)] #[salsa::invoke(crate::adt::EnumData::enum_data_query)]
fn enum_data(&self, def_id: DefId) -> Arc<EnumData>; fn enum_data(&self, e: Enum) -> Arc<EnumData>;
#[salsa::invoke(crate::adt::EnumVariantData::enum_variant_data_query)] #[salsa::invoke(crate::adt::EnumVariantData::enum_variant_data_query)]
fn enum_variant_data(&self, def_id: DefId) -> Arc<EnumVariantData>; fn enum_variant_data(&self, def_id: DefId) -> Arc<EnumVariantData>;

View File

@ -7,7 +7,7 @@ use std::sync::Arc;
use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner}; use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner};
use crate::{db::HirDatabase, DefId, Name, AsName, Function, Struct}; use crate::{db::HirDatabase, DefId, Name, AsName, Function, Struct, Enum};
/// Data about a generic parameter (to a function, struct, impl, ...). /// Data about a generic parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
@ -26,6 +26,7 @@ pub struct GenericParams {
pub enum GenericDef { pub enum GenericDef {
Function(Function), Function(Function),
Struct(Struct), Struct(Struct),
Enum(Enum),
Def(DefId), Def(DefId),
} }
@ -36,8 +37,14 @@ impl From<Function> for GenericDef {
} }
impl From<Struct> for GenericDef { impl From<Struct> for GenericDef {
fn from(func: Struct) -> GenericDef { fn from(s: Struct) -> GenericDef {
GenericDef::Struct(func) GenericDef::Struct(s)
}
}
impl From<Enum> for GenericDef {
fn from(e: Enum) -> GenericDef {
GenericDef::Enum(e)
} }
} }
@ -54,22 +61,13 @@ impl GenericParams {
) -> Arc<GenericParams> { ) -> Arc<GenericParams> {
let mut generics = GenericParams::default(); let mut generics = GenericParams::default();
match def { match def {
GenericDef::Function(func) => { GenericDef::Function(it) => generics.fill(&*it.source(db).1),
let (_, fn_def) = func.source(db); GenericDef::Struct(it) => generics.fill(&*it.source(db).1),
if let Some(type_param_list) = fn_def.type_param_list() { GenericDef::Enum(it) => generics.fill(&*it.source(db).1),
generics.fill(type_param_list)
}
}
GenericDef::Struct(s) => {
let (_, struct_def) = s.source(db);
if let Some(type_param_list) = struct_def.type_param_list() {
generics.fill(type_param_list)
}
}
GenericDef::Def(def_id) => { GenericDef::Def(def_id) => {
let (_file_id, node) = def_id.source(db); let (_file_id, node) = def_id.source(db);
if let Some(type_param_list) = node.children().find_map(ast::TypeParamList::cast) { if let Some(type_param_list) = node.children().find_map(ast::TypeParamList::cast) {
generics.fill(type_param_list) generics.fill_params(type_param_list)
} }
} }
} }
@ -77,7 +75,13 @@ impl GenericParams {
Arc::new(generics) Arc::new(generics)
} }
fn fill(&mut self, params: &ast::TypeParamList) { fn fill(&mut self, node: &impl TypeParamsOwner) {
if let Some(params) = node.type_param_list() {
self.fill_params(params)
}
}
fn fill_params(&mut self, params: &ast::TypeParamList) {
for (idx, type_param) in params.type_params().enumerate() { for (idx, type_param) in params.type_params().enumerate() {
let name = type_param let name = type_param
.name() .name()

View File

@ -5,7 +5,7 @@ use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, ast};
use ra_arena::{Arena, RawId, impl_arena_id}; use ra_arena::{Arena, RawId, impl_arena_id};
use crate::{ use crate::{
HirDatabase, Def, Enum, EnumVariant, Crate, HirDatabase, Def, EnumVariant, Crate,
Module, Trait, Type, Static, Const, Module, Trait, Type, Static, Const,
}; };
@ -247,25 +247,22 @@ pub struct DefLoc {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DefKind { pub(crate) enum DefKind {
Struct,
Enum,
EnumVariant, EnumVariant,
Const, Const,
Static, Static,
Trait, Trait,
Type, Type,
Item, Item,
// /// The constructor of a struct. E.g. if we have `struct Foo(usize)`, the
/// The constructor of a struct. E.g. if we have `struct Foo(usize)`, the // /// name `Foo` needs to resolve to different types depending on whether we
/// name `Foo` needs to resolve to different types depending on whether we // /// are in the types or values namespace: As a type, `Foo` of course refers
/// are in the types or values namespace: As a type, `Foo` of course refers // /// to the struct `Foo`; as a value, `Foo` is a callable type with signature
/// to the struct `Foo`; as a value, `Foo` is a callable type with signature // /// `(usize) -> Foo`. The cleanest approach to handle this seems to be to
/// `(usize) -> Foo`. The cleanest approach to handle this seems to be to // /// have different defs in the two namespaces.
/// have different defs in the two namespaces. // ///
/// // /// rustc does the same; note that it even creates a struct constructor if
/// rustc does the same; note that it even creates a struct constructor if // /// the struct isn't a tuple struct (see `CtorKind::Fictive` in rustc).
/// the struct isn't a tuple struct (see `CtorKind::Fictive` in rustc). // StructCtor,
StructCtor,
} }
impl DefId { impl DefId {
@ -276,8 +273,6 @@ impl DefId {
pub fn resolve(self, db: &impl HirDatabase) -> Def { pub fn resolve(self, db: &impl HirDatabase) -> Def {
let loc = self.loc(db); let loc = self.loc(db);
match loc.kind { match loc.kind {
DefKind::Struct => unreachable!(),
DefKind::Enum => Def::Enum(Enum::new(self)),
DefKind::EnumVariant => Def::EnumVariant(EnumVariant::new(self)), DefKind::EnumVariant => Def::EnumVariant(EnumVariant::new(self)),
DefKind::Const => { DefKind::Const => {
let def = Const::new(self); let def = Const::new(self);
@ -295,8 +290,6 @@ impl DefId {
let def = Type::new(self); let def = Type::new(self);
Def::Type(def) Def::Type(def)
} }
DefKind::StructCtor => Def::Item,
DefKind::Item => Def::Item, DefKind::Item => Def::Item,
} }
} }

View File

@ -10,7 +10,7 @@ use rustc_hash::FxHashMap;
use crate::{ use crate::{
SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems, SourceItemId, Path, ModuleSource, HirDatabase, Name, SourceFileItems,
HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function, HirFileId, MacroCallLoc, AsName, PerNs, DefKind, DefLoc, Function,
ModuleDef, Module, Struct, ModuleDef, Module, Struct, Enum,
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -155,7 +155,14 @@ impl LoweredModule {
} }
return; return;
} }
ast::ModuleItemKind::EnumDef(it) => it.name(), ast::ModuleItemKind::EnumDef(it) => {
if let Some(name) = it.name() {
let e = Enum::from_ast(db, module, file_id, it);
let e: ModuleDef = e.into();
self.declarations.insert(name.as_name(), PerNs::types(e));
}
return;
}
ast::ModuleItemKind::FnDef(it) => { ast::ModuleItemKind::FnDef(it) => {
if let Some(name) = it.name() { if let Some(name) = it.name() {
let func = Function::from_ast(db, module, file_id, it); let func = Function::from_ast(db, module, file_id, it);
@ -233,8 +240,8 @@ impl DefKind {
fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> { fn for_syntax_kind(kind: SyntaxKind) -> PerNs<DefKind> {
match kind { match kind {
SyntaxKind::FN_DEF => unreachable!(), SyntaxKind::FN_DEF => unreachable!(),
SyntaxKind::STRUCT_DEF => PerNs::both(DefKind::Struct, DefKind::StructCtor), SyntaxKind::STRUCT_DEF => unreachable!(),
SyntaxKind::ENUM_DEF => PerNs::types(DefKind::Enum), SyntaxKind::ENUM_DEF => unreachable!(),
SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait), SyntaxKind::TRAIT_DEF => PerNs::types(DefKind::Trait),
SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type), SyntaxKind::TYPE_DEF => PerNs::types(DefKind::Type),
SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const), SyntaxKind::CONST_DEF => PerNs::values(DefKind::Const),

View File

@ -166,27 +166,28 @@ impl Substs {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AdtDef { pub enum AdtDef {
Struct(Struct), Struct(Struct),
Def(DefId), // Enum Enum(Enum),
} }
impl From<Struct> for AdtDef { impl From<Struct> for AdtDef {
fn from(struct_: Struct) -> AdtDef { fn from(s: Struct) -> AdtDef {
AdtDef::Struct(struct_) AdtDef::Struct(s)
} }
} }
impl From<DefId> for AdtDef { impl From<Enum> for AdtDef {
fn from(def_id: DefId) -> AdtDef { fn from(e: Enum) -> AdtDef {
AdtDef::Def(def_id) AdtDef::Enum(e)
} }
} }
impl AdtDef { impl AdtDef {
fn krate(self, db: &impl HirDatabase) -> Option<Crate> { fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
match self { match self {
AdtDef::Struct(s) => s.module(db).krate(db), AdtDef::Struct(s) => s.module(db),
AdtDef::Def(def_id) => def_id.krate(db), AdtDef::Enum(e) => e.module(db),
} }
.krate(db)
} }
} }
@ -408,14 +409,16 @@ impl Ty {
} }
// Resolve in module (in type namespace) // Resolve in module (in type namespace)
let resolved = match module.resolve_path(db, path).take_types() { let typable: TypableDef = match module
Some(ModuleDef::Def(r)) => r.into(), .resolve_path(db, path)
Some(ModuleDef::Function(f)) => f.into(), .take_types()
Some(ModuleDef::Struct(s)) => s.into(), .and_then(|it| it.into())
None | Some(ModuleDef::Module(_)) => return Ty::Unknown, {
None => return Ty::Unknown,
Some(it) => it,
}; };
let ty = db.type_for_def(resolved); let ty = db.type_for_def(typable);
let substs = Ty::substs_from_path(db, module, impl_block, generics, path, resolved); let substs = Ty::substs_from_path(db, module, impl_block, generics, path, typable);
ty.apply_substs(substs) ty.apply_substs(substs)
} }
@ -438,9 +441,8 @@ impl Ty {
let (def_generics, segment) = match resolved { let (def_generics, segment) = match resolved {
TypableDef::Function(func) => (func.generic_params(db), last), TypableDef::Function(func) => (func.generic_params(db), last),
TypableDef::Struct(s) => (s.generic_params(db), last), TypableDef::Struct(s) => (s.generic_params(db), last),
TypableDef::Enum(e) => (e.generic_params(db), last),
TypableDef::Def(def_id) => match def_id.resolve(db) { TypableDef::Def(def_id) => match def_id.resolve(db) {
Def::Struct(s) => (s.generic_params(db), last),
Def::Enum(e) => (e.generic_params(db), last),
Def::Trait(t) => (t.generic_params(db), last), Def::Trait(t) => (t.generic_params(db), last),
Def::EnumVariant(ev) => { Def::EnumVariant(ev) => {
// the generic args for an enum variant may be either specified // the generic args for an enum variant may be either specified
@ -680,7 +682,7 @@ fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty {
pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty { pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty {
let generics = s.generic_params(db); let generics = s.generic_params(db);
Ty::Adt { Ty::Adt {
def_id: s.def_id().into(), def_id: s.into(),
name: s.name(db).unwrap_or_else(Name::missing), name: s.name(db).unwrap_or_else(Name::missing),
substs: make_substs(&generics), substs: make_substs(&generics),
} }
@ -696,6 +698,7 @@ pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> T
pub enum TypableDef { pub enum TypableDef {
Function(Function), Function(Function),
Struct(Struct), Struct(Struct),
Enum(Enum),
Def(DefId), Def(DefId),
} }
@ -706,8 +709,14 @@ impl From<Function> for TypableDef {
} }
impl From<Struct> for TypableDef { impl From<Struct> for TypableDef {
fn from(struct_: Struct) -> TypableDef { fn from(s: Struct) -> TypableDef {
TypableDef::Struct(struct_) TypableDef::Struct(s)
}
}
impl From<Enum> for TypableDef {
fn from(e: Enum) -> TypableDef {
TypableDef::Enum(e)
} }
} }
@ -717,10 +726,24 @@ impl From<DefId> for TypableDef {
} }
} }
impl From<ModuleDef> for Option<TypableDef> {
fn from(def: ModuleDef) -> Option<TypableDef> {
let res = match def {
ModuleDef::Def(r) => r.into(),
ModuleDef::Function(f) => f.into(),
ModuleDef::Struct(s) => s.into(),
ModuleDef::Enum(e) => e.into(),
ModuleDef::Module(_) => return None,
};
Some(res)
}
}
pub(super) fn type_for_def(db: &impl HirDatabase, def: TypableDef) -> Ty { pub(super) fn type_for_def(db: &impl HirDatabase, def: TypableDef) -> Ty {
match def { match def {
TypableDef::Function(f) => type_for_fn(db, f), TypableDef::Function(f) => type_for_fn(db, f),
TypableDef::Struct(s) => type_for_struct(db, s), TypableDef::Struct(s) => type_for_struct(db, s),
TypableDef::Enum(e) => type_for_enum(db, e),
TypableDef::Def(def_id) => match def_id.resolve(db) { TypableDef::Def(def_id) => match def_id.resolve(db) {
Def::Enum(e) => type_for_enum(db, e), Def::Enum(e) => type_for_enum(db, e),
Def::EnumVariant(ev) => type_for_enum_variant(db, ev), Def::EnumVariant(ev) => type_for_enum_variant(db, ev),
@ -1134,12 +1157,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}; };
// resolve in module // resolve in module
let typable = match self.module.resolve_path(self.db, &path).take_values()? { let typable: Option<TypableDef> = self
ModuleDef::Def(it) => it.into(), .module
ModuleDef::Function(func) => func.into(), .resolve_path(self.db, &path)
ModuleDef::Struct(s) => s.into(), .take_values()?
ModuleDef::Module(_) => return None, .into();
}; let typable = typable?;
let ty = self.db.type_for_def(typable); let ty = self.db.type_for_def(typable);
let ty = self.insert_type_vars(ty); let ty = self.insert_type_vars(ty);
Some(ty) Some(ty)
@ -1150,11 +1173,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
Some(path) => path, Some(path) => path,
None => return (Ty::Unknown, None), None => return (Ty::Unknown, None),
}; };
let def = match self.module.resolve_path(self.db, &path).take_types() { let typable: Option<TypableDef> = self
Some(ModuleDef::Def(def_id)) => def_id.into(), .module
Some(ModuleDef::Function(func)) => func.into(), .resolve_path(self.db, &path)
Some(ModuleDef::Struct(s)) => s.into(), .take_types()
None | Some(ModuleDef::Module(_)) => return (Ty::Unknown, None), .and_then(|it| it.into());
let def = match typable {
None => return (Ty::Unknown, None),
Some(it) => it,
}; };
// TODO remove the duplication between here and `Ty::from_path`? // TODO remove the duplication between here and `Ty::from_path`?
// TODO provide generics of function // TODO provide generics of function
@ -1182,6 +1208,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let ty = self.insert_type_vars(ty.apply_substs(substs)); let ty = self.insert_type_vars(ty.apply_substs(substs));
(ty, Some(s.into())) (ty, Some(s.into()))
} }
TypableDef::Enum(_) => (Ty::Unknown, None),
} }
} }
@ -1300,6 +1327,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
ModuleDef::Def(it) => Some(it.into()), ModuleDef::Def(it) => Some(it.into()),
ModuleDef::Function(func) => Some(func.into()), ModuleDef::Function(func) => Some(func.into()),
ModuleDef::Struct(s) => Some(s.into()), ModuleDef::Struct(s) => Some(s.into()),
ModuleDef::Enum(e) => Some(e.into()),
ModuleDef::Module(_) => None, ModuleDef::Module(_) => None,
}) })
.map_or(Ty::Unknown, |resolved| self.db.type_for_def(resolved)), .map_or(Ty::Unknown, |resolved| self.db.type_for_def(resolved)),

View File

@ -430,6 +430,13 @@ impl StructDef {
} }
impl EnumVariant { impl EnumVariant {
pub fn parent_enum(&self) -> &EnumDef {
self.syntax()
.parent()
.and_then(|it| it.parent())
.and_then(EnumDef::cast)
.expect("EnumVariants are always nested in Enums")
}
pub fn flavor(&self) -> StructFlavor { pub fn flavor(&self) -> StructFlavor {
StructFlavor::from_node(self) StructFlavor::from_node(self)
} }