From 45117c63884366ee82102a782a62a09fefff746b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Sep 2019 00:10:16 +0300 Subject: make various enums "inherit" from AdtDef --- crates/ra_hir/src/adt.rs | 22 +------- crates/ra_hir/src/code_model.rs | 61 +++++++++++++++++++--- crates/ra_hir/src/expr/validation.rs | 3 +- crates/ra_hir/src/generics.rs | 49 ++++++++--------- crates/ra_hir/src/lang_item.rs | 10 ++-- crates/ra_hir/src/lib.rs | 4 +- crates/ra_hir/src/nameres.rs | 5 +- crates/ra_hir/src/nameres/collector.rs | 6 +-- crates/ra_hir/src/resolve.rs | 6 +-- crates/ra_hir/src/ty/infer.rs | 6 +-- crates/ra_hir/src/ty/lower.rs | 58 ++++++++++---------- crates/ra_ide_api/src/completion/complete_path.rs | 13 ++--- .../ra_ide_api/src/completion/complete_pattern.rs | 2 +- crates/ra_ide_api/src/completion/presentation.rs | 12 +++-- crates/ra_ide_api/src/display/navigation_target.rs | 4 +- crates/ra_ide_api/src/hover.rs | 12 +++-- crates/ra_ide_api/src/syntax_highlighting.rs | 4 +- 17 files changed, 156 insertions(+), 121 deletions(-) diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 728046b0d..56f2b7aa3 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -9,29 +9,9 @@ use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, type_ref::TypeRef, - AsName, Crate, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField, - Union, + AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField, }; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum AdtDef { - Struct(Struct), - Union(Union), - Enum(Enum), -} -impl_froms!(AdtDef: Struct, Union, Enum); - -impl AdtDef { - pub(crate) fn krate(self, db: &impl HirDatabase) -> Option { - match self { - AdtDef::Struct(s) => s.module(db), - AdtDef::Union(s) => s.module(db), - AdtDef::Enum(e) => e.module(db), - } - .krate(db) - } -} - impl Struct { pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc { db.struct_data(self).variant_data.clone() diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 2bac6122b..026753c9f 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -127,9 +127,7 @@ impl BuiltinType { pub enum ModuleDef { Module(Module), Function(Function), - Struct(Struct), - Union(Union), - Enum(Enum), + AdtDef(AdtDef), // Can't be directly declared, but can be imported. EnumVariant(EnumVariant), Const(Const), @@ -141,9 +139,7 @@ pub enum ModuleDef { impl_froms!( ModuleDef: Module, Function, - Struct, - Union, - Enum, + AdtDef, EnumVariant, Const, Static, @@ -152,6 +148,24 @@ impl_froms!( BuiltinType ); +impl From for ModuleDef { + fn from(it: Struct) -> ModuleDef { + ModuleDef::AdtDef(AdtDef::Struct(it)) + } +} + +impl From for ModuleDef { + fn from(it: Enum) -> ModuleDef { + ModuleDef::AdtDef(AdtDef::Enum(it)) + } +} + +impl From for ModuleDef { + fn from(it: Union) -> ModuleDef { + ModuleDef::AdtDef(AdtDef::Union(it)) + } +} + pub enum ModuleSource { SourceFile(ast::SourceFile), Module(ast::Module), @@ -500,6 +514,41 @@ impl EnumVariant { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum AdtDef { + Struct(Struct), + Union(Union), + Enum(Enum), +} +impl_froms!(AdtDef: Struct, Union, Enum); + +impl AdtDef { + pub fn ty(self, db: &impl HirDatabase) -> Ty { + match self { + AdtDef::Struct(it) => it.ty(db), + AdtDef::Union(it) => it.ty(db), + AdtDef::Enum(it) => it.ty(db), + } + } + + pub(crate) fn krate(self, db: &impl HirDatabase) -> Option { + match self { + AdtDef::Struct(s) => s.module(db), + AdtDef::Union(s) => s.module(db), + AdtDef::Enum(e) => e.module(db), + } + .krate(db) + } + + pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { + match self { + AdtDef::Struct(it) => it.resolver(db), + AdtDef::Union(it) => it.resolver(db), + AdtDef::Enum(it) => it.resolver(db), + } + } +} + /// The defs which have a body. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum DefWithBody { diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index c985dbdad..447b9264a 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -4,14 +4,13 @@ use ra_syntax::ast; use rustc_hash::FxHashSet; use crate::{ - adt::AdtDef, db::HirDatabase, diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr}, expr::AstPtr, name, path::{PathKind, PathSegment}, ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, - Function, Name, Path, + AdtDef, Function, Name, Path, }; use super::{Expr, ExprId, RecordLitField}; diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index c76df0698..0e89941f3 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -47,9 +47,7 @@ pub struct WherePredicate { #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] pub enum GenericDef { Function(Function), - Struct(Struct), - Union(Union), - Enum(Enum), + AdtDef(AdtDef), Trait(Trait), TypeAlias(TypeAlias), ImplBlock(ImplBlock), @@ -57,7 +55,25 @@ pub enum GenericDef { // can, and this makes some code easier to write EnumVariant(EnumVariant), } -impl_froms!(GenericDef: Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlock, EnumVariant); +impl_froms!(GenericDef: Function, AdtDef, Trait, TypeAlias, ImplBlock, EnumVariant); + +impl From for GenericDef { + fn from(it: Struct) -> GenericDef { + GenericDef::AdtDef(AdtDef::Struct(it)) + } +} + +impl From for GenericDef { + fn from(it: Enum) -> GenericDef { + GenericDef::AdtDef(AdtDef::Enum(it)) + } +} + +impl From for GenericDef { + fn from(it: Union) -> GenericDef { + GenericDef::AdtDef(AdtDef::Union(it)) + } +} impl GenericParams { pub(crate) fn generic_params_query( @@ -69,10 +85,7 @@ impl GenericParams { GenericDef::Function(it) => it.container(db).map(GenericDef::from), GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from), GenericDef::EnumVariant(it) => Some(it.parent_enum(db).into()), - GenericDef::Struct(_) - | GenericDef::Union(_) - | GenericDef::Enum(_) - | GenericDef::Trait(_) => None, + GenericDef::AdtDef(_) | GenericDef::Trait(_) => None, GenericDef::ImplBlock(_) => None, }; generics.parent_params = parent.map(|p| db.generic_params(p)); @@ -80,9 +93,9 @@ impl GenericParams { // FIXME: add `: Sized` bound for everything except for `Self` in traits match def { GenericDef::Function(it) => generics.fill(&it.source(db).ast, start), - GenericDef::Struct(it) => generics.fill(&it.source(db).ast, start), - GenericDef::Union(it) => generics.fill(&it.source(db).ast, start), - GenericDef::Enum(it) => generics.fill(&it.source(db).ast, start), + GenericDef::AdtDef(AdtDef::Struct(it)) => generics.fill(&it.source(db).ast, start), + GenericDef::AdtDef(AdtDef::Union(it)) => generics.fill(&it.source(db).ast, start), + GenericDef::AdtDef(AdtDef::Enum(it)) => generics.fill(&it.source(db).ast, start), GenericDef::Trait(it) => { // traits get the Self type as an implicit first type parameter generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None }); @@ -186,9 +199,7 @@ impl GenericDef { pub(crate) fn resolver(&self, db: &impl HirDatabase) -> crate::Resolver { match self { GenericDef::Function(inner) => inner.resolver(db), - GenericDef::Struct(inner) => inner.resolver(db), - GenericDef::Union(inner) => inner.resolver(db), - GenericDef::Enum(inner) => inner.resolver(db), + GenericDef::AdtDef(adt) => adt.resolver(db), GenericDef::Trait(inner) => inner.resolver(db), GenericDef::TypeAlias(inner) => inner.resolver(db), GenericDef::ImplBlock(inner) => inner.resolver(db), @@ -206,16 +217,6 @@ impl From for GenericDef { } } -impl From for GenericDef { - fn from(adt: crate::adt::AdtDef) -> Self { - match adt { - AdtDef::Struct(s) => s.into(), - AdtDef::Union(u) => u.into(), - AdtDef::Enum(e) => e.into(), - } - } -} - pub trait HasGenericParams: Copy { fn generic_params(self, db: &impl DefDatabase) -> Arc; } diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 832fdf1f5..87484d892 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs @@ -5,7 +5,7 @@ use ra_syntax::{ast::AttrsOwner, SmolStr}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, - Crate, Enum, Function, HasSource, ImplBlock, Module, ModuleDef, Static, Struct, Trait, + AdtDef, Crate, Enum, Function, HasSource, ImplBlock, Module, ModuleDef, Static, Struct, Trait, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -107,8 +107,12 @@ impl LangItems { ModuleDef::Trait(trait_) => { self.collect_lang_item(db, trait_, LangItemTarget::Trait) } - ModuleDef::Enum(e) => self.collect_lang_item(db, e, LangItemTarget::Enum), - ModuleDef::Struct(s) => self.collect_lang_item(db, s, LangItemTarget::Struct), + ModuleDef::AdtDef(AdtDef::Enum(e)) => { + self.collect_lang_item(db, e, LangItemTarget::Enum) + } + ModuleDef::AdtDef(AdtDef::Struct(s)) => { + self.collect_lang_item(db, s, LangItemTarget::Struct) + } ModuleDef::Function(f) => self.collect_lang_item(db, f, LangItemTarget::Function), ModuleDef::Static(s) => self.collect_lang_item(db, s, LangItemTarget::Static), _ => {} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 24ee84f86..2fe1a658e 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -57,7 +57,7 @@ use crate::{ }; pub use self::{ - adt::{AdtDef, VariantDef}, + adt::VariantDef, either::Either, expr::ExprScopes, generics::{GenericParam, GenericParams, HasGenericParams}, @@ -78,7 +78,7 @@ pub use self::{ pub use self::code_model::{ docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource, Source}, - BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, + AdtDef, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 3d8691f53..1efaad4bc 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -69,7 +69,8 @@ use crate::{ diagnostics::DiagnosticSink, ids::MacroDefId, nameres::diagnostics::DefDiagnostic, - AstId, BuiltinType, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait, + AdtDef, AstId, BuiltinType, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, + PathKind, Trait, }; pub(crate) use self::raw::{ImportSourceMap, RawItems}; @@ -425,7 +426,7 @@ impl CrateDefMap { } } } - ModuleDef::Enum(e) => { + ModuleDef::AdtDef(AdtDef::Enum(e)) => { // enum variant tested_by!(can_import_enum_variant); match e.variant(db, &segment.name) { diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 03fbbd33f..ced1ebf1e 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -13,8 +13,8 @@ use crate::{ raw, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode, }, - AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, - Struct, Trait, TypeAlias, Union, + AdtDef, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, + Static, Struct, Trait, TypeAlias, Union, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -314,7 +314,7 @@ where .push((module_id, import_id)); } } - Some(ModuleDef::Enum(e)) => { + Some(ModuleDef::AdtDef(AdtDef::Enum(e))) => { tested_by!(glob_enum); // glob import from enum => just import all the variants let variants = e.variants(self.db); diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 1ed150f5a..251945112 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -15,7 +15,7 @@ use crate::{ name::{Name, SELF_PARAM, SELF_TYPE}, nameres::{CrateDefMap, CrateModuleId, PerNs}, path::Path, - Enum, MacroDef, ModuleDef, Struct, Trait, + AdtDef, Enum, MacroDef, ModuleDef, Struct, Trait, }; #[derive(Debug, Clone, Default)] @@ -143,7 +143,7 @@ impl Resolver { ) -> Option { let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?; match res { - Resolution::Def(ModuleDef::Struct(it)) => Some(it), + Resolution::Def(ModuleDef::AdtDef(AdtDef::Struct(it))) => Some(it), _ => None, } } @@ -152,7 +152,7 @@ impl Resolver { pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option { let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?; match res { - Resolution::Def(ModuleDef::Enum(it)) => Some(it), + Resolution::Def(ModuleDef::AdtDef(AdtDef::Enum(it))) => Some(it), _ => None, } } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 3970c49e3..725eec834 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -668,7 +668,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // FIXME remove the duplication between here and `Ty::from_path`? let substs = Ty::substs_from_path(self.db, resolver, path, def); match def { - TypableDef::Struct(s) => { + TypableDef::AdtDef(AdtDef::Struct(s)) => { let ty = s.ty(self.db); let ty = self.insert_type_vars(ty.apply_substs(substs)); (ty, Some(s.into())) @@ -678,10 +678,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = self.insert_type_vars(ty.apply_substs(substs)); (ty, Some(var.into())) } - TypableDef::Union(_) + TypableDef::AdtDef(AdtDef::Enum(_)) + | TypableDef::AdtDef(AdtDef::Union(_)) | TypableDef::TypeAlias(_) | TypableDef::Function(_) - | TypableDef::Enum(_) | TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => (Ty::Unknown, None), diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index e67525a74..587dc12ee 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -172,9 +172,7 @@ impl Ty { ) -> Substs { let def_generic: Option = match resolved { TypableDef::Function(func) => Some(func.into()), - TypableDef::Struct(s) => Some(s.into()), - TypableDef::Union(u) => Some(u.into()), - TypableDef::Enum(e) => Some(e.into()), + TypableDef::AdtDef(adt) => Some(adt.into()), TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), TypableDef::TypeAlias(t) => Some(t.into()), TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None, @@ -193,9 +191,7 @@ impl Ty { let last = path.segments.last().expect("path should have at least one segment"); let segment = match resolved { TypableDef::Function(_) - | TypableDef::Struct(_) - | TypableDef::Union(_) - | TypableDef::Enum(_) + | TypableDef::AdtDef(_) | TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::TypeAlias(_) @@ -410,11 +406,11 @@ fn assoc_type_bindings_from_type_bound<'a>( pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty { match (def, ns) { (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f), - (TypableDef::Struct(s), Namespace::Types) => type_for_adt(db, s), - (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s), - (TypableDef::Enum(e), Namespace::Types) => type_for_adt(db, e), + (TypableDef::AdtDef(AdtDef::Struct(s)), Namespace::Values) => { + type_for_struct_constructor(db, s) + } + (TypableDef::AdtDef(adt), Namespace::Types) => type_for_adt(db, adt), (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), - (TypableDef::Union(u), Namespace::Types) => type_for_adt(db, u), (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), @@ -422,8 +418,8 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace // 'error' cases: (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, - (TypableDef::Union(_), Namespace::Values) => Ty::Unknown, - (TypableDef::Enum(_), Namespace::Values) => Ty::Unknown, + (TypableDef::AdtDef(AdtDef::Union(_)), Namespace::Values) => Ty::Unknown, + (TypableDef::AdtDef(AdtDef::Enum(_)), Namespace::Values) => Ty::Unknown, (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, @@ -608,34 +604,38 @@ fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum TypableDef { Function(Function), - Struct(Struct), - Union(Union), - Enum(Enum), + AdtDef(AdtDef), EnumVariant(EnumVariant), TypeAlias(TypeAlias), Const(Const), Static(Static), BuiltinType(BuiltinType), } -impl_froms!( - TypableDef: Function, - Struct, - Union, - Enum, - EnumVariant, - TypeAlias, - Const, - Static, - BuiltinType -); +impl_froms!(TypableDef: Function, AdtDef, EnumVariant, TypeAlias, Const, Static, BuiltinType); + +impl From for TypableDef { + fn from(it: Struct) -> TypableDef { + TypableDef::AdtDef(AdtDef::Struct(it)) + } +} + +impl From for TypableDef { + fn from(it: Enum) -> TypableDef { + TypableDef::AdtDef(AdtDef::Enum(it)) + } +} + +impl From for TypableDef { + fn from(it: Union) -> TypableDef { + TypableDef::AdtDef(AdtDef::Union(it)) + } +} impl From for Option { fn from(def: ModuleDef) -> Option { let res = match def { ModuleDef::Function(f) => f.into(), - ModuleDef::Struct(s) => s.into(), - ModuleDef::Union(u) => u.into(), - ModuleDef::Enum(e) => e.into(), + ModuleDef::AdtDef(adt) => adt.into(), ModuleDef::EnumVariant(v) => v.into(), ModuleDef::TypeAlias(t) => t.into(), ModuleDef::Const(v) => v.into(), diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 457a3d10c..df14f465a 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,4 +1,4 @@ -use hir::{Either, Resolution}; +use hir::{AdtDef, Either, Resolution}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -37,19 +37,14 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); } } - hir::ModuleDef::Enum(_) - | hir::ModuleDef::Struct(_) - | hir::ModuleDef::Union(_) - | hir::ModuleDef::TypeAlias(_) => { - if let hir::ModuleDef::Enum(e) = def { + hir::ModuleDef::AdtDef(_) | hir::ModuleDef::TypeAlias(_) => { + if let hir::ModuleDef::AdtDef(AdtDef::Enum(e)) = def { for variant in e.variants(ctx.db) { acc.add_enum_variant(ctx, variant); } } let ty = match def { - hir::ModuleDef::Enum(e) => e.ty(ctx.db), - hir::ModuleDef::Struct(s) => s.ty(ctx.db), - hir::ModuleDef::Union(u) => u.ty(ctx.db), + hir::ModuleDef::AdtDef(adt) => adt.ty(ctx.db), hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; diff --git a/crates/ra_ide_api/src/completion/complete_pattern.rs b/crates/ra_ide_api/src/completion/complete_pattern.rs index fb02844fa..05db973e4 100644 --- a/crates/ra_ide_api/src/completion/complete_pattern.rs +++ b/crates/ra_ide_api/src/completion/complete_pattern.rs @@ -15,7 +15,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { _ => continue, }; match def { - hir::ModuleDef::Enum(..) + hir::ModuleDef::AdtDef(hir::AdtDef::Enum(..)) | hir::ModuleDef::EnumVariant(..) | hir::ModuleDef::Const(..) | hir::ModuleDef::Module(..) => (), diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 5cabe9a32..e9bf2b96d 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -67,9 +67,15 @@ impl Completions { Resolution::Def(Function(func)) => { return self.add_function_with_name(ctx, Some(local_name), *func); } - Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), - Resolution::Def(Union(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), - Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), + Resolution::Def(AdtDef(hir::AdtDef::Struct(it))) => { + (CompletionItemKind::Struct, it.docs(ctx.db)) + } + Resolution::Def(AdtDef(hir::AdtDef::Union(it))) => { + (CompletionItemKind::Struct, it.docs(ctx.db)) + } + Resolution::Def(AdtDef(hir::AdtDef::Enum(it))) => { + (CompletionItemKind::Enum, it.docs(ctx.db)) + } Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index c85214bb3..b8085f077 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -193,9 +193,7 @@ impl NavigationTarget { let nav = match module_def { hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), - hir::ModuleDef::Struct(it) => NavigationTarget::from_adt_def(db, it.into()), - hir::ModuleDef::Enum(it) => NavigationTarget::from_adt_def(db, it.into()), - hir::ModuleDef::Union(it) => NavigationTarget::from_adt_def(db, it.into()), + hir::ModuleDef::AdtDef(it) => NavigationTarget::from_adt_def(db, it), hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 4b7847de8..f7b7cb8f8 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,4 +1,4 @@ -use hir::{HasSource, HirDisplay}; +use hir::{AdtDef, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::{ @@ -129,9 +129,13 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option res.extend(from_def_source(db, it)), - hir::ModuleDef::Struct(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Union(it) => res.extend(from_def_source(db, it)), - hir::ModuleDef::Enum(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::AdtDef(AdtDef::Struct(it)) => { + res.extend(from_def_source(db, it)) + } + hir::ModuleDef::AdtDef(AdtDef::Union(it)) => { + res.extend(from_def_source(db, it)) + } + hir::ModuleDef::AdtDef(AdtDef::Enum(it)) => res.extend(from_def_source(db, it)), hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)), diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 06ccf0728..ee7531d37 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -107,9 +107,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "type", Some(Def(hir::ModuleDef::Module(_))) => "module", Some(Def(hir::ModuleDef::Function(_))) => "function", - Some(Def(hir::ModuleDef::Struct(_))) => "type", - Some(Def(hir::ModuleDef::Union(_))) => "type", - Some(Def(hir::ModuleDef::Enum(_))) => "type", + Some(Def(hir::ModuleDef::AdtDef(_))) => "type", Some(Def(hir::ModuleDef::EnumVariant(_))) => "constant", Some(Def(hir::ModuleDef::Const(_))) => "constant", Some(Def(hir::ModuleDef::Static(_))) => "constant", -- cgit v1.2.3 From bcf30d389c3744ab517372431eb7e1e1dfcc50df Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Sep 2019 00:31:04 +0300 Subject: generalize impl_froms to nested enums --- crates/ra_hir/src/code_model.rs | 20 +------------------- crates/ra_hir/src/generics.rs | 27 ++++++++------------------- crates/ra_hir/src/lib.rs | 9 ++++++++- crates/ra_hir/src/ty/lower.rs | 28 +++++++++------------------- 4 files changed, 26 insertions(+), 58 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 026753c9f..a10e795ce 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -139,7 +139,7 @@ pub enum ModuleDef { impl_froms!( ModuleDef: Module, Function, - AdtDef, + AdtDef(Struct, Enum, Union), EnumVariant, Const, Static, @@ -148,24 +148,6 @@ impl_froms!( BuiltinType ); -impl From for ModuleDef { - fn from(it: Struct) -> ModuleDef { - ModuleDef::AdtDef(AdtDef::Struct(it)) - } -} - -impl From for ModuleDef { - fn from(it: Enum) -> ModuleDef { - ModuleDef::AdtDef(AdtDef::Enum(it)) - } -} - -impl From for ModuleDef { - fn from(it: Union) -> ModuleDef { - ModuleDef::AdtDef(AdtDef::Union(it)) - } -} - pub enum ModuleSource { SourceFile(ast::SourceFile), Module(ast::Module), diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 0e89941f3..74435c20a 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -55,25 +55,14 @@ pub enum GenericDef { // can, and this makes some code easier to write EnumVariant(EnumVariant), } -impl_froms!(GenericDef: Function, AdtDef, Trait, TypeAlias, ImplBlock, EnumVariant); - -impl From for GenericDef { - fn from(it: Struct) -> GenericDef { - GenericDef::AdtDef(AdtDef::Struct(it)) - } -} - -impl From for GenericDef { - fn from(it: Enum) -> GenericDef { - GenericDef::AdtDef(AdtDef::Enum(it)) - } -} - -impl From for GenericDef { - fn from(it: Union) -> GenericDef { - GenericDef::AdtDef(AdtDef::Union(it)) - } -} +impl_froms!( + GenericDef: Function, + AdtDef(Struct, Enum, Union), + Trait, + TypeAlias, + ImplBlock, + EnumVariant +); impl GenericParams { pub(crate) fn generic_params_query( diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 2fe1a658e..639e97c3d 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -8,13 +8,20 @@ //! applied. So, the relation between syntax and HIR is many-to-one. macro_rules! impl_froms { - ($e:ident: $($v:ident),*) => { + ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { $( impl From<$v> for $e { fn from(it: $v) -> $e { $e::$v(it) } } + $($( + impl From<$sv> for $e { + fn from(it: $sv) -> $e { + $e::$v($v::$sv(it)) + } + } + )*)? )* } } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 587dc12ee..105c893f0 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -611,25 +611,15 @@ pub enum TypableDef { Static(Static), BuiltinType(BuiltinType), } -impl_froms!(TypableDef: Function, AdtDef, EnumVariant, TypeAlias, Const, Static, BuiltinType); - -impl From for TypableDef { - fn from(it: Struct) -> TypableDef { - TypableDef::AdtDef(AdtDef::Struct(it)) - } -} - -impl From for TypableDef { - fn from(it: Enum) -> TypableDef { - TypableDef::AdtDef(AdtDef::Enum(it)) - } -} - -impl From for TypableDef { - fn from(it: Union) -> TypableDef { - TypableDef::AdtDef(AdtDef::Union(it)) - } -} +impl_froms!( + TypableDef: Function, + AdtDef(Struct, Enum, Union), + EnumVariant, + TypeAlias, + Const, + Static, + BuiltinType +); impl From for Option { fn from(def: ModuleDef) -> Option { -- cgit v1.2.3 From 114a1b878e95c20490af574550ea0825b7a8f9d1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Sep 2019 00:34:52 +0300 Subject: rename AdtDef -> Adt --- crates/ra_assists/src/fill_match_arms.rs | 4 +-- crates/ra_hir/src/code_model.rs | 29 +++++++++++----------- crates/ra_hir/src/expr/validation.rs | 6 ++--- crates/ra_hir/src/generics.rs | 18 +++++++------- crates/ra_hir/src/lang_item.rs | 8 +++--- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/nameres.rs | 6 ++--- crates/ra_hir/src/nameres/collector.rs | 6 ++--- crates/ra_hir/src/resolve.rs | 6 ++--- crates/ra_hir/src/ty.rs | 12 ++++----- crates/ra_hir/src/ty/infer.rs | 14 +++++------ crates/ra_hir/src/ty/lower.rs | 24 ++++++++---------- crates/ra_ide_api/src/completion/complete_dot.rs | 4 +-- crates/ra_ide_api/src/completion/complete_path.rs | 8 +++--- .../ra_ide_api/src/completion/complete_pattern.rs | 2 +- crates/ra_ide_api/src/completion/presentation.rs | 8 +++--- crates/ra_ide_api/src/display/navigation_target.rs | 10 ++++---- crates/ra_ide_api/src/hover.rs | 18 ++++++-------- crates/ra_ide_api/src/name_ref_kind.rs | 2 +- crates/ra_ide_api/src/syntax_highlighting.rs | 2 +- 20 files changed, 90 insertions(+), 99 deletions(-) diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index cc3879562..543ed164c 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs @@ -1,6 +1,6 @@ use std::iter; -use hir::{db::HirDatabase, AdtDef, HasSource}; +use hir::{db::HirDatabase, Adt, HasSource}; use ra_syntax::ast::{self, AstNode, NameOwner}; use crate::{ast_editor::AstBuilder, Assist, AssistCtx, AssistId}; @@ -60,7 +60,7 @@ fn resolve_enum_def( let expr_ty = analyzer.type_of(db, &expr)?; analyzer.autoderef(db, expr_ty).find_map(|ty| match ty.as_adt() { - Some((AdtDef::Enum(e), _)) => Some(e.source(db).ast), + Some((Adt::Enum(e), _)) => Some(e.source(db).ast), _ => None, }) } diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a10e795ce..c1938bd86 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -127,7 +127,7 @@ impl BuiltinType { pub enum ModuleDef { Module(Module), Function(Function), - AdtDef(AdtDef), + Adt(Adt), // Can't be directly declared, but can be imported. EnumVariant(EnumVariant), Const(Const), @@ -139,7 +139,7 @@ pub enum ModuleDef { impl_froms!( ModuleDef: Module, Function, - AdtDef(Struct, Enum, Union), + Adt(Struct, Enum, Union), EnumVariant, Const, Static, @@ -496,37 +496,38 @@ impl EnumVariant { } } +/// A Data Type #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum AdtDef { +pub enum Adt { Struct(Struct), Union(Union), Enum(Enum), } -impl_froms!(AdtDef: Struct, Union, Enum); +impl_froms!(Adt: Struct, Union, Enum); -impl AdtDef { +impl Adt { pub fn ty(self, db: &impl HirDatabase) -> Ty { match self { - AdtDef::Struct(it) => it.ty(db), - AdtDef::Union(it) => it.ty(db), - AdtDef::Enum(it) => it.ty(db), + Adt::Struct(it) => it.ty(db), + Adt::Union(it) => it.ty(db), + Adt::Enum(it) => it.ty(db), } } pub(crate) fn krate(self, db: &impl HirDatabase) -> Option { match self { - AdtDef::Struct(s) => s.module(db), - AdtDef::Union(s) => s.module(db), - AdtDef::Enum(e) => e.module(db), + Adt::Struct(s) => s.module(db), + Adt::Union(s) => s.module(db), + Adt::Enum(e) => e.module(db), } .krate(db) } pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { match self { - AdtDef::Struct(it) => it.resolver(db), - AdtDef::Union(it) => it.resolver(db), - AdtDef::Enum(it) => it.resolver(db), + Adt::Struct(it) => it.resolver(db), + Adt::Union(it) => it.resolver(db), + Adt::Enum(it) => it.resolver(db), } } } diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index 447b9264a..030ec53a2 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -10,7 +10,7 @@ use crate::{ name, path::{PathKind, PathSegment}, ty::{ApplicationTy, InferenceResult, Ty, TypeCtor}, - AdtDef, Function, Name, Path, + Adt, Function, Name, Path, }; use super::{Expr, ExprId, RecordLitField}; @@ -58,7 +58,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { } let struct_def = match self.infer[id].as_adt() { - Some((AdtDef::Struct(s), _)) => s, + Some((Adt::Struct(s), _)) => s, _ => return, }; @@ -123,7 +123,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { _ => return, }; - let std_result_ctor = TypeCtor::Adt(AdtDef::Enum(std_result_enum)); + let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum)); let params = match &mismatch.expected { Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters, _ => return, diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 74435c20a..77fb76bfc 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -12,8 +12,8 @@ use crate::{ name::SELF_TYPE, path::Path, type_ref::{TypeBound, TypeRef}, - AdtDef, AsName, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct, - Trait, TypeAlias, Union, + Adt, AsName, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct, Trait, + TypeAlias, Union, }; /// Data about a generic parameter (to a function, struct, impl, ...). @@ -47,7 +47,7 @@ pub struct WherePredicate { #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] pub enum GenericDef { Function(Function), - AdtDef(AdtDef), + Adt(Adt), Trait(Trait), TypeAlias(TypeAlias), ImplBlock(ImplBlock), @@ -57,7 +57,7 @@ pub enum GenericDef { } impl_froms!( GenericDef: Function, - AdtDef(Struct, Enum, Union), + Adt(Struct, Enum, Union), Trait, TypeAlias, ImplBlock, @@ -74,7 +74,7 @@ impl GenericParams { GenericDef::Function(it) => it.container(db).map(GenericDef::from), GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from), GenericDef::EnumVariant(it) => Some(it.parent_enum(db).into()), - GenericDef::AdtDef(_) | GenericDef::Trait(_) => None, + GenericDef::Adt(_) | GenericDef::Trait(_) => None, GenericDef::ImplBlock(_) => None, }; generics.parent_params = parent.map(|p| db.generic_params(p)); @@ -82,9 +82,9 @@ impl GenericParams { // FIXME: add `: Sized` bound for everything except for `Self` in traits match def { GenericDef::Function(it) => generics.fill(&it.source(db).ast, start), - GenericDef::AdtDef(AdtDef::Struct(it)) => generics.fill(&it.source(db).ast, start), - GenericDef::AdtDef(AdtDef::Union(it)) => generics.fill(&it.source(db).ast, start), - GenericDef::AdtDef(AdtDef::Enum(it)) => generics.fill(&it.source(db).ast, start), + GenericDef::Adt(Adt::Struct(it)) => generics.fill(&it.source(db).ast, start), + GenericDef::Adt(Adt::Union(it)) => generics.fill(&it.source(db).ast, start), + GenericDef::Adt(Adt::Enum(it)) => generics.fill(&it.source(db).ast, start), GenericDef::Trait(it) => { // traits get the Self type as an implicit first type parameter generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None }); @@ -188,7 +188,7 @@ impl GenericDef { pub(crate) fn resolver(&self, db: &impl HirDatabase) -> crate::Resolver { match self { GenericDef::Function(inner) => inner.resolver(db), - GenericDef::AdtDef(adt) => adt.resolver(db), + GenericDef::Adt(adt) => adt.resolver(db), GenericDef::Trait(inner) => inner.resolver(db), GenericDef::TypeAlias(inner) => inner.resolver(db), GenericDef::ImplBlock(inner) => inner.resolver(db), diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 87484d892..e3b71cec3 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs @@ -5,7 +5,7 @@ use ra_syntax::{ast::AttrsOwner, SmolStr}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, - AdtDef, Crate, Enum, Function, HasSource, ImplBlock, Module, ModuleDef, Static, Struct, Trait, + Adt, Crate, Enum, Function, HasSource, ImplBlock, Module, ModuleDef, Static, Struct, Trait, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -107,10 +107,8 @@ impl LangItems { ModuleDef::Trait(trait_) => { self.collect_lang_item(db, trait_, LangItemTarget::Trait) } - ModuleDef::AdtDef(AdtDef::Enum(e)) => { - self.collect_lang_item(db, e, LangItemTarget::Enum) - } - ModuleDef::AdtDef(AdtDef::Struct(s)) => { + ModuleDef::Adt(Adt::Enum(e)) => self.collect_lang_item(db, e, LangItemTarget::Enum), + ModuleDef::Adt(Adt::Struct(s)) => { self.collect_lang_item(db, s, LangItemTarget::Struct) } ModuleDef::Function(f) => self.collect_lang_item(db, f, LangItemTarget::Function), diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 639e97c3d..508da3623 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -85,7 +85,7 @@ pub use self::{ pub use self::code_model::{ docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource, Source}, - AdtDef, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, + Adt, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 1efaad4bc..3a3bf6b5f 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -69,8 +69,8 @@ use crate::{ diagnostics::DiagnosticSink, ids::MacroDefId, nameres::diagnostics::DefDiagnostic, - AdtDef, AstId, BuiltinType, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, - PathKind, Trait, + Adt, AstId, BuiltinType, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, + Trait, }; pub(crate) use self::raw::{ImportSourceMap, RawItems}; @@ -426,7 +426,7 @@ impl CrateDefMap { } } } - ModuleDef::AdtDef(AdtDef::Enum(e)) => { + ModuleDef::Adt(Adt::Enum(e)) => { // enum variant tested_by!(can_import_enum_variant); match e.variant(db, &segment.name) { diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index ced1ebf1e..7d2f313c1 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -13,8 +13,8 @@ use crate::{ raw, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode, }, - AdtDef, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, - Static, Struct, Trait, TypeAlias, Union, + Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, + Struct, Trait, TypeAlias, Union, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -314,7 +314,7 @@ where .push((module_id, import_id)); } } - Some(ModuleDef::AdtDef(AdtDef::Enum(e))) => { + Some(ModuleDef::Adt(Adt::Enum(e))) => { tested_by!(glob_enum); // glob import from enum => just import all the variants let variants = e.variants(self.db); diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 251945112..a77a9aeb1 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -15,7 +15,7 @@ use crate::{ name::{Name, SELF_PARAM, SELF_TYPE}, nameres::{CrateDefMap, CrateModuleId, PerNs}, path::Path, - AdtDef, Enum, MacroDef, ModuleDef, Struct, Trait, + Adt, Enum, MacroDef, ModuleDef, Struct, Trait, }; #[derive(Debug, Clone, Default)] @@ -143,7 +143,7 @@ impl Resolver { ) -> Option { let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?; match res { - Resolution::Def(ModuleDef::AdtDef(AdtDef::Struct(it))) => Some(it), + Resolution::Def(ModuleDef::Adt(Adt::Struct(it))) => Some(it), _ => None, } } @@ -152,7 +152,7 @@ impl Resolver { pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option { let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?; match res { - Resolution::Def(ModuleDef::AdtDef(AdtDef::Enum(it))) => Some(it), + Resolution::Def(ModuleDef::Adt(Adt::Enum(it))) => Some(it), _ => None, } } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index a3df08827..375850b92 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -16,7 +16,7 @@ use std::ops::Deref; use std::sync::Arc; use std::{fmt, mem}; -use crate::{db::HirDatabase, type_ref::Mutability, AdtDef, GenericParams, Name, Trait, TypeAlias}; +use crate::{db::HirDatabase, type_ref::Mutability, Adt, GenericParams, Name, Trait, TypeAlias}; use display::{HirDisplay, HirFormatter}; pub(crate) use autoderef::autoderef; @@ -47,7 +47,7 @@ pub enum TypeCtor { Float(primitive::UncertainFloatTy), /// Structures, enumerations and unions. - Adt(AdtDef), + Adt(Adt), /// The pointee of a string slice. Written as `str`. Str, @@ -458,7 +458,7 @@ impl Ty { } } - pub fn as_adt(&self) -> Option<(AdtDef, &Substs)> { + pub fn as_adt(&self) -> Option<(Adt, &Substs)> { match self { Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => { Some((*adt_def, parameters)) @@ -726,9 +726,9 @@ impl HirDisplay for ApplicationTy { } TypeCtor::Adt(def_id) => { let name = match def_id { - AdtDef::Struct(s) => s.name(f.db), - AdtDef::Union(u) => u.name(f.db), - AdtDef::Enum(e) => e.name(f.db), + Adt::Struct(s) => s.name(f.db), + Adt::Union(u) => u.name(f.db), + Adt::Enum(e) => e.name(f.db), } .unwrap_or_else(Name::missing); write!(f, "{}", name)?; diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 725eec834..540a99b15 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -48,7 +48,7 @@ use crate::{ resolve::{Resolution, Resolver}, ty::infer::diagnostics::InferenceDiagnostic, type_ref::{Mutability, TypeRef}, - AdtDef, ConstData, DefWithBody, FnData, Function, HasBody, ImplItem, ModuleDef, Name, Path, + Adt, ConstData, DefWithBody, FnData, Function, HasBody, ImplItem, ModuleDef, Name, Path, StructField, }; @@ -668,7 +668,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // FIXME remove the duplication between here and `Ty::from_path`? let substs = Ty::substs_from_path(self.db, resolver, path, def); match def { - TypableDef::AdtDef(AdtDef::Struct(s)) => { + TypableDef::Adt(Adt::Struct(s)) => { let ty = s.ty(self.db); let ty = self.insert_type_vars(ty.apply_substs(substs)); (ty, Some(s.into())) @@ -678,8 +678,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = self.insert_type_vars(ty.apply_substs(substs)); (ty, Some(var.into())) } - TypableDef::AdtDef(AdtDef::Enum(_)) - | TypableDef::AdtDef(AdtDef::Union(_)) + TypableDef::Adt(Adt::Enum(_)) + | TypableDef::Adt(Adt::Union(_)) | TypableDef::TypeAlias(_) | TypableDef::Function(_) | TypableDef::Const(_) @@ -1185,7 +1185,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let i = name.to_string().parse::().ok(); i.and_then(|i| a_ty.parameters.0.get(i).cloned()) } - TypeCtor::Adt(AdtDef::Struct(s)) => s.field(self.db, name).map(|field| { + TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| { self.write_field_resolution(tgt_expr, field); field.ty(self.db).subst(&a_ty.parameters) }), @@ -1489,7 +1489,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { trait_.associated_type_by_name(self.db, &name::OUTPUT) } - fn resolve_boxed_box(&self) -> Option { + fn resolve_boxed_box(&self) -> Option { let boxed_box_path = Path { kind: PathKind::Abs, segments: vec![ @@ -1499,7 +1499,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { ], }; let struct_ = self.resolver.resolve_known_struct(self.db, &boxed_box_path)?; - Some(AdtDef::Struct(struct_)) + Some(Adt::Struct(struct_)) } } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 105c893f0..946e9e9fb 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -20,7 +20,7 @@ use crate::{ nameres::Namespace, path::{GenericArg, PathSegment}, resolve::{Resolution, Resolver}, - ty::AdtDef, + ty::Adt, type_ref::{TypeBound, TypeRef}, BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, TypeAlias, Union, @@ -172,7 +172,7 @@ impl Ty { ) -> Substs { let def_generic: Option = match resolved { TypableDef::Function(func) => Some(func.into()), - TypableDef::AdtDef(adt) => Some(adt.into()), + TypableDef::Adt(adt) => Some(adt.into()), TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), TypableDef::TypeAlias(t) => Some(t.into()), TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None, @@ -191,7 +191,7 @@ impl Ty { let last = path.segments.last().expect("path should have at least one segment"); let segment = match resolved { TypableDef::Function(_) - | TypableDef::AdtDef(_) + | TypableDef::Adt(_) | TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::TypeAlias(_) @@ -406,10 +406,8 @@ fn assoc_type_bindings_from_type_bound<'a>( pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty { match (def, ns) { (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f), - (TypableDef::AdtDef(AdtDef::Struct(s)), Namespace::Values) => { - type_for_struct_constructor(db, s) - } - (TypableDef::AdtDef(adt), Namespace::Types) => type_for_adt(db, adt), + (TypableDef::Adt(Adt::Struct(s)), Namespace::Values) => type_for_struct_constructor(db, s), + (TypableDef::Adt(adt), Namespace::Types) => type_for_adt(db, adt), (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), @@ -418,8 +416,8 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace // 'error' cases: (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, - (TypableDef::AdtDef(AdtDef::Union(_)), Namespace::Values) => Ty::Unknown, - (TypableDef::AdtDef(AdtDef::Enum(_)), Namespace::Values) => Ty::Unknown, + (TypableDef::Adt(Adt::Union(_)), Namespace::Values) => Ty::Unknown, + (TypableDef::Adt(Adt::Enum(_)), Namespace::Values) => Ty::Unknown, (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, @@ -587,7 +585,7 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty::apply(TypeCtor::FnDef(def.into()), substs) } -fn type_for_adt(db: &impl HirDatabase, adt: impl Into + HasGenericParams) -> Ty { +fn type_for_adt(db: &impl HirDatabase, adt: impl Into + HasGenericParams) -> Ty { let generics = adt.generic_params(db); Ty::apply(TypeCtor::Adt(adt.into()), Substs::identity(&generics)) } @@ -604,7 +602,7 @@ fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum TypableDef { Function(Function), - AdtDef(AdtDef), + Adt(Adt), EnumVariant(EnumVariant), TypeAlias(TypeAlias), Const(Const), @@ -613,7 +611,7 @@ pub enum TypableDef { } impl_froms!( TypableDef: Function, - AdtDef(Struct, Enum, Union), + Adt(Struct, Enum, Union), EnumVariant, TypeAlias, Const, @@ -625,7 +623,7 @@ impl From for Option { fn from(def: ModuleDef) -> Option { let res = match def { ModuleDef::Function(f) => f.into(), - ModuleDef::AdtDef(adt) => adt.into(), + ModuleDef::Adt(adt) => adt.into(), ModuleDef::EnumVariant(v) => v.into(), ModuleDef::TypeAlias(t) => t.into(), ModuleDef::Const(v) => v.into(), diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 2325cdf08..9e64c8659 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,4 +1,4 @@ -use hir::{AdtDef, Ty, TypeCtor}; +use hir::{Adt, Ty, TypeCtor}; use crate::completion::completion_item::CompletionKind; use crate::{ @@ -37,7 +37,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) for receiver in ctx.analyzer.autoderef(ctx.db, receiver) { if let Ty::Apply(a_ty) = receiver { match a_ty.ctor { - TypeCtor::Adt(AdtDef::Struct(s)) => { + TypeCtor::Adt(Adt::Struct(s)) => { for field in s.fields(ctx.db) { acc.add_field(ctx, field, &a_ty.parameters); } diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index df14f465a..4d3414f33 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,4 +1,4 @@ -use hir::{AdtDef, Either, Resolution}; +use hir::{Adt, Either, Resolution}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -37,14 +37,14 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); } } - hir::ModuleDef::AdtDef(_) | hir::ModuleDef::TypeAlias(_) => { - if let hir::ModuleDef::AdtDef(AdtDef::Enum(e)) = def { + hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => { + if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { for variant in e.variants(ctx.db) { acc.add_enum_variant(ctx, variant); } } let ty = match def { - hir::ModuleDef::AdtDef(adt) => adt.ty(ctx.db), + hir::ModuleDef::Adt(adt) => adt.ty(ctx.db), hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; diff --git a/crates/ra_ide_api/src/completion/complete_pattern.rs b/crates/ra_ide_api/src/completion/complete_pattern.rs index 05db973e4..fb7f9feb8 100644 --- a/crates/ra_ide_api/src/completion/complete_pattern.rs +++ b/crates/ra_ide_api/src/completion/complete_pattern.rs @@ -15,7 +15,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { _ => continue, }; match def { - hir::ModuleDef::AdtDef(hir::AdtDef::Enum(..)) + hir::ModuleDef::Adt(hir::Adt::Enum(..)) | hir::ModuleDef::EnumVariant(..) | hir::ModuleDef::Const(..) | hir::ModuleDef::Module(..) => (), diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index e9bf2b96d..95bbd34b7 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -67,15 +67,13 @@ impl Completions { Resolution::Def(Function(func)) => { return self.add_function_with_name(ctx, Some(local_name), *func); } - Resolution::Def(AdtDef(hir::AdtDef::Struct(it))) => { + Resolution::Def(Adt(hir::Adt::Struct(it))) => { (CompletionItemKind::Struct, it.docs(ctx.db)) } - Resolution::Def(AdtDef(hir::AdtDef::Union(it))) => { + Resolution::Def(Adt(hir::Adt::Union(it))) => { (CompletionItemKind::Struct, it.docs(ctx.db)) } - Resolution::Def(AdtDef(hir::AdtDef::Enum(it))) => { - (CompletionItemKind::Enum, it.docs(ctx.db)) - } + Resolution::Def(Adt(hir::Adt::Enum(it))) => (CompletionItemKind::Enum, it.docs(ctx.db)), Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index b8085f077..03382ab3c 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -178,11 +178,11 @@ impl NavigationTarget { ) } - pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::AdtDef) -> NavigationTarget { + pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::Adt) -> NavigationTarget { match adt_def { - hir::AdtDef::Struct(it) => NavigationTarget::from_def_source(db, it), - hir::AdtDef::Union(it) => NavigationTarget::from_def_source(db, it), - hir::AdtDef::Enum(it) => NavigationTarget::from_def_source(db, it), + hir::Adt::Struct(it) => NavigationTarget::from_def_source(db, it), + hir::Adt::Union(it) => NavigationTarget::from_def_source(db, it), + hir::Adt::Enum(it) => NavigationTarget::from_def_source(db, it), } } @@ -193,7 +193,7 @@ impl NavigationTarget { let nav = match module_def { hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), - hir::ModuleDef::AdtDef(it) => NavigationTarget::from_adt_def(db, it), + hir::ModuleDef::Adt(it) => NavigationTarget::from_adt_def(db, it), hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index f7b7cb8f8..28a6bef12 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,4 +1,4 @@ -use hir::{AdtDef, HasSource, HirDisplay}; +use hir::{Adt, HasSource, HirDisplay}; use ra_db::SourceDatabase; use ra_syntax::{ algo::{ @@ -129,13 +129,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option res.extend(from_def_source(db, it)), - hir::ModuleDef::AdtDef(AdtDef::Struct(it)) => { - res.extend(from_def_source(db, it)) - } - hir::ModuleDef::AdtDef(AdtDef::Union(it)) => { - res.extend(from_def_source(db, it)) - } - hir::ModuleDef::AdtDef(AdtDef::Enum(it)) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Adt(Adt::Struct(it)) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Adt(Adt::Union(it)) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Adt(Adt::Enum(it)) => res.extend(from_def_source(db, it)), hir::ModuleDef::EnumVariant(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)), @@ -149,9 +145,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { if let Some((adt_def, _)) = ty.as_adt() { res.extend(match adt_def { - hir::AdtDef::Struct(it) => from_def_source(db, it), - hir::AdtDef::Union(it) => from_def_source(db, it), - hir::AdtDef::Enum(it) => from_def_source(db, it), + hir::Adt::Struct(it) => from_def_source(db, it), + hir::Adt::Union(it) => from_def_source(db, it), + hir::Adt::Enum(it) => from_def_source(db, it), }) } } diff --git a/crates/ra_ide_api/src/name_ref_kind.rs b/crates/ra_ide_api/src/name_ref_kind.rs index 34a8bcc36..6c2a7b260 100644 --- a/crates/ra_ide_api/src/name_ref_kind.rs +++ b/crates/ra_ide_api/src/name_ref_kind.rs @@ -60,7 +60,7 @@ pub(crate) fn classify_name_ref( let record_lit = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast); if let Some(ty) = record_lit.and_then(|lit| analyzer.type_of(db, &lit.into())) { - if let Some((hir::AdtDef::Struct(s), _)) = ty.as_adt() { + if let Some((hir::Adt::Struct(s), _)) = ty.as_adt() { let hir_path = hir::Path::from_name_ref(name_ref); let hir_name = hir_path.as_ident().unwrap(); diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index ee7531d37..86ab3a260 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -107,7 +107,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec "type", Some(Def(hir::ModuleDef::Module(_))) => "module", Some(Def(hir::ModuleDef::Function(_))) => "function", - Some(Def(hir::ModuleDef::AdtDef(_))) => "type", + Some(Def(hir::ModuleDef::Adt(_))) => "type", Some(Def(hir::ModuleDef::EnumVariant(_))) => "constant", Some(Def(hir::ModuleDef::Const(_))) => "constant", Some(Def(hir::ModuleDef::Static(_))) => "constant", -- cgit v1.2.3