mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-30 15:03:42 +00:00
make various enums "inherit" from AdtDef
This commit is contained in:
parent
d8b621cf26
commit
45117c6388
17 changed files with 156 additions and 121 deletions
|
@ -9,29 +9,9 @@ use ra_syntax::ast::{self, NameOwner, StructKind, TypeAscriptionOwner};
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
AsName, Crate, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField,
|
AsName, Enum, EnumVariant, FieldSource, HasSource, Name, Source, Struct, StructField,
|
||||||
Union,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[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<Crate> {
|
|
||||||
match self {
|
|
||||||
AdtDef::Struct(s) => s.module(db),
|
|
||||||
AdtDef::Union(s) => s.module(db),
|
|
||||||
AdtDef::Enum(e) => e.module(db),
|
|
||||||
}
|
|
||||||
.krate(db)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Struct {
|
impl Struct {
|
||||||
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
|
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
|
||||||
db.struct_data(self).variant_data.clone()
|
db.struct_data(self).variant_data.clone()
|
||||||
|
|
|
@ -127,9 +127,7 @@ impl BuiltinType {
|
||||||
pub enum ModuleDef {
|
pub enum ModuleDef {
|
||||||
Module(Module),
|
Module(Module),
|
||||||
Function(Function),
|
Function(Function),
|
||||||
Struct(Struct),
|
AdtDef(AdtDef),
|
||||||
Union(Union),
|
|
||||||
Enum(Enum),
|
|
||||||
// Can't be directly declared, but can be imported.
|
// Can't be directly declared, but can be imported.
|
||||||
EnumVariant(EnumVariant),
|
EnumVariant(EnumVariant),
|
||||||
Const(Const),
|
Const(Const),
|
||||||
|
@ -141,9 +139,7 @@ pub enum ModuleDef {
|
||||||
impl_froms!(
|
impl_froms!(
|
||||||
ModuleDef: Module,
|
ModuleDef: Module,
|
||||||
Function,
|
Function,
|
||||||
Struct,
|
AdtDef,
|
||||||
Union,
|
|
||||||
Enum,
|
|
||||||
EnumVariant,
|
EnumVariant,
|
||||||
Const,
|
Const,
|
||||||
Static,
|
Static,
|
||||||
|
@ -152,6 +148,24 @@ impl_froms!(
|
||||||
BuiltinType
|
BuiltinType
|
||||||
);
|
);
|
||||||
|
|
||||||
|
impl From<Struct> for ModuleDef {
|
||||||
|
fn from(it: Struct) -> ModuleDef {
|
||||||
|
ModuleDef::AdtDef(AdtDef::Struct(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Enum> for ModuleDef {
|
||||||
|
fn from(it: Enum) -> ModuleDef {
|
||||||
|
ModuleDef::AdtDef(AdtDef::Enum(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Union> for ModuleDef {
|
||||||
|
fn from(it: Union) -> ModuleDef {
|
||||||
|
ModuleDef::AdtDef(AdtDef::Union(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum ModuleSource {
|
pub enum ModuleSource {
|
||||||
SourceFile(ast::SourceFile),
|
SourceFile(ast::SourceFile),
|
||||||
Module(ast::Module),
|
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<Crate> {
|
||||||
|
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.
|
/// The defs which have a body.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum DefWithBody {
|
pub enum DefWithBody {
|
||||||
|
|
|
@ -4,14 +4,13 @@ use ra_syntax::ast;
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
adt::AdtDef,
|
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr},
|
diagnostics::{DiagnosticSink, MissingFields, MissingOkInTailExpr},
|
||||||
expr::AstPtr,
|
expr::AstPtr,
|
||||||
name,
|
name,
|
||||||
path::{PathKind, PathSegment},
|
path::{PathKind, PathSegment},
|
||||||
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
|
ty::{ApplicationTy, InferenceResult, Ty, TypeCtor},
|
||||||
Function, Name, Path,
|
AdtDef, Function, Name, Path,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{Expr, ExprId, RecordLitField};
|
use super::{Expr, ExprId, RecordLitField};
|
||||||
|
|
|
@ -47,9 +47,7 @@ pub struct WherePredicate {
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
||||||
pub enum GenericDef {
|
pub enum GenericDef {
|
||||||
Function(Function),
|
Function(Function),
|
||||||
Struct(Struct),
|
AdtDef(AdtDef),
|
||||||
Union(Union),
|
|
||||||
Enum(Enum),
|
|
||||||
Trait(Trait),
|
Trait(Trait),
|
||||||
TypeAlias(TypeAlias),
|
TypeAlias(TypeAlias),
|
||||||
ImplBlock(ImplBlock),
|
ImplBlock(ImplBlock),
|
||||||
|
@ -57,7 +55,25 @@ pub enum GenericDef {
|
||||||
// can, and this makes some code easier to write
|
// can, and this makes some code easier to write
|
||||||
EnumVariant(EnumVariant),
|
EnumVariant(EnumVariant),
|
||||||
}
|
}
|
||||||
impl_froms!(GenericDef: Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlock, EnumVariant);
|
impl_froms!(GenericDef: Function, AdtDef, Trait, TypeAlias, ImplBlock, EnumVariant);
|
||||||
|
|
||||||
|
impl From<Struct> for GenericDef {
|
||||||
|
fn from(it: Struct) -> GenericDef {
|
||||||
|
GenericDef::AdtDef(AdtDef::Struct(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Enum> for GenericDef {
|
||||||
|
fn from(it: Enum) -> GenericDef {
|
||||||
|
GenericDef::AdtDef(AdtDef::Enum(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Union> for GenericDef {
|
||||||
|
fn from(it: Union) -> GenericDef {
|
||||||
|
GenericDef::AdtDef(AdtDef::Union(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl GenericParams {
|
impl GenericParams {
|
||||||
pub(crate) fn generic_params_query(
|
pub(crate) fn generic_params_query(
|
||||||
|
@ -69,10 +85,7 @@ impl GenericParams {
|
||||||
GenericDef::Function(it) => it.container(db).map(GenericDef::from),
|
GenericDef::Function(it) => it.container(db).map(GenericDef::from),
|
||||||
GenericDef::TypeAlias(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::EnumVariant(it) => Some(it.parent_enum(db).into()),
|
||||||
GenericDef::Struct(_)
|
GenericDef::AdtDef(_) | GenericDef::Trait(_) => None,
|
||||||
| GenericDef::Union(_)
|
|
||||||
| GenericDef::Enum(_)
|
|
||||||
| GenericDef::Trait(_) => None,
|
|
||||||
GenericDef::ImplBlock(_) => None,
|
GenericDef::ImplBlock(_) => None,
|
||||||
};
|
};
|
||||||
generics.parent_params = parent.map(|p| db.generic_params(p));
|
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
|
// FIXME: add `: Sized` bound for everything except for `Self` in traits
|
||||||
match def {
|
match def {
|
||||||
GenericDef::Function(it) => generics.fill(&it.source(db).ast, start),
|
GenericDef::Function(it) => generics.fill(&it.source(db).ast, start),
|
||||||
GenericDef::Struct(it) => generics.fill(&it.source(db).ast, start),
|
GenericDef::AdtDef(AdtDef::Struct(it)) => generics.fill(&it.source(db).ast, start),
|
||||||
GenericDef::Union(it) => generics.fill(&it.source(db).ast, start),
|
GenericDef::AdtDef(AdtDef::Union(it)) => generics.fill(&it.source(db).ast, start),
|
||||||
GenericDef::Enum(it) => generics.fill(&it.source(db).ast, start),
|
GenericDef::AdtDef(AdtDef::Enum(it)) => generics.fill(&it.source(db).ast, start),
|
||||||
GenericDef::Trait(it) => {
|
GenericDef::Trait(it) => {
|
||||||
// traits get the Self type as an implicit first type parameter
|
// traits get the Self type as an implicit first type parameter
|
||||||
generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None });
|
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 {
|
pub(crate) fn resolver(&self, db: &impl HirDatabase) -> crate::Resolver {
|
||||||
match self {
|
match self {
|
||||||
GenericDef::Function(inner) => inner.resolver(db),
|
GenericDef::Function(inner) => inner.resolver(db),
|
||||||
GenericDef::Struct(inner) => inner.resolver(db),
|
GenericDef::AdtDef(adt) => adt.resolver(db),
|
||||||
GenericDef::Union(inner) => inner.resolver(db),
|
|
||||||
GenericDef::Enum(inner) => inner.resolver(db),
|
|
||||||
GenericDef::Trait(inner) => inner.resolver(db),
|
GenericDef::Trait(inner) => inner.resolver(db),
|
||||||
GenericDef::TypeAlias(inner) => inner.resolver(db),
|
GenericDef::TypeAlias(inner) => inner.resolver(db),
|
||||||
GenericDef::ImplBlock(inner) => inner.resolver(db),
|
GenericDef::ImplBlock(inner) => inner.resolver(db),
|
||||||
|
@ -206,16 +217,6 @@ impl From<Container> for GenericDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<crate::adt::AdtDef> 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 {
|
pub trait HasGenericParams: Copy {
|
||||||
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
|
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use ra_syntax::{ast::AttrsOwner, SmolStr};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
@ -107,8 +107,12 @@ impl LangItems {
|
||||||
ModuleDef::Trait(trait_) => {
|
ModuleDef::Trait(trait_) => {
|
||||||
self.collect_lang_item(db, trait_, LangItemTarget::Trait)
|
self.collect_lang_item(db, trait_, LangItemTarget::Trait)
|
||||||
}
|
}
|
||||||
ModuleDef::Enum(e) => self.collect_lang_item(db, e, LangItemTarget::Enum),
|
ModuleDef::AdtDef(AdtDef::Enum(e)) => {
|
||||||
ModuleDef::Struct(s) => self.collect_lang_item(db, s, LangItemTarget::Struct),
|
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::Function(f) => self.collect_lang_item(db, f, LangItemTarget::Function),
|
||||||
ModuleDef::Static(s) => self.collect_lang_item(db, s, LangItemTarget::Static),
|
ModuleDef::Static(s) => self.collect_lang_item(db, s, LangItemTarget::Static),
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -57,7 +57,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
adt::{AdtDef, VariantDef},
|
adt::VariantDef,
|
||||||
either::Either,
|
either::Either,
|
||||||
expr::ExprScopes,
|
expr::ExprScopes,
|
||||||
generics::{GenericParam, GenericParams, HasGenericParams},
|
generics::{GenericParam, GenericParams, HasGenericParams},
|
||||||
|
@ -78,7 +78,7 @@ pub use self::{
|
||||||
pub use self::code_model::{
|
pub use self::code_model::{
|
||||||
docs::{DocDef, Docs, Documentation},
|
docs::{DocDef, Docs, Documentation},
|
||||||
src::{HasBodySource, HasSource, Source},
|
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,
|
EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, ModuleSource,
|
||||||
Static, Struct, StructField, Trait, TypeAlias, Union,
|
Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||||
};
|
};
|
||||||
|
|
|
@ -69,7 +69,8 @@ use crate::{
|
||||||
diagnostics::DiagnosticSink,
|
diagnostics::DiagnosticSink,
|
||||||
ids::MacroDefId,
|
ids::MacroDefId,
|
||||||
nameres::diagnostics::DefDiagnostic,
|
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};
|
pub(crate) use self::raw::{ImportSourceMap, RawItems};
|
||||||
|
@ -425,7 +426,7 @@ impl CrateDefMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ModuleDef::Enum(e) => {
|
ModuleDef::AdtDef(AdtDef::Enum(e)) => {
|
||||||
// enum variant
|
// enum variant
|
||||||
tested_by!(can_import_enum_variant);
|
tested_by!(can_import_enum_variant);
|
||||||
match e.variant(db, &segment.name) {
|
match e.variant(db, &segment.name) {
|
||||||
|
|
|
@ -13,8 +13,8 @@ use crate::{
|
||||||
raw, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef, PerNs, ReachedFixedPoint,
|
raw, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef, PerNs, ReachedFixedPoint,
|
||||||
Resolution, ResolveMode,
|
Resolution, ResolveMode,
|
||||||
},
|
},
|
||||||
AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static,
|
AdtDef, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind,
|
||||||
Struct, Trait, TypeAlias, Union,
|
Static, Struct, Trait, TypeAlias, Union,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
|
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
|
||||||
|
@ -314,7 +314,7 @@ where
|
||||||
.push((module_id, import_id));
|
.push((module_id, import_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(ModuleDef::Enum(e)) => {
|
Some(ModuleDef::AdtDef(AdtDef::Enum(e))) => {
|
||||||
tested_by!(glob_enum);
|
tested_by!(glob_enum);
|
||||||
// glob import from enum => just import all the variants
|
// glob import from enum => just import all the variants
|
||||||
let variants = e.variants(self.db);
|
let variants = e.variants(self.db);
|
||||||
|
|
|
@ -15,7 +15,7 @@ use crate::{
|
||||||
name::{Name, SELF_PARAM, SELF_TYPE},
|
name::{Name, SELF_PARAM, SELF_TYPE},
|
||||||
nameres::{CrateDefMap, CrateModuleId, PerNs},
|
nameres::{CrateDefMap, CrateModuleId, PerNs},
|
||||||
path::Path,
|
path::Path,
|
||||||
Enum, MacroDef, ModuleDef, Struct, Trait,
|
AdtDef, Enum, MacroDef, ModuleDef, Struct, Trait,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
|
@ -143,7 +143,7 @@ impl Resolver {
|
||||||
) -> Option<Struct> {
|
) -> Option<Struct> {
|
||||||
let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?;
|
let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?;
|
||||||
match res {
|
match res {
|
||||||
Resolution::Def(ModuleDef::Struct(it)) => Some(it),
|
Resolution::Def(ModuleDef::AdtDef(AdtDef::Struct(it))) => Some(it),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,7 +152,7 @@ impl Resolver {
|
||||||
pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> {
|
pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> {
|
||||||
let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?;
|
let res = self.resolve_path_segments(db, path).into_fully_resolved().take_types()?;
|
||||||
match res {
|
match res {
|
||||||
Resolution::Def(ModuleDef::Enum(it)) => Some(it),
|
Resolution::Def(ModuleDef::AdtDef(AdtDef::Enum(it))) => Some(it),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -668,7 +668,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
// FIXME remove the duplication between here and `Ty::from_path`?
|
// FIXME remove the duplication between here and `Ty::from_path`?
|
||||||
let substs = Ty::substs_from_path(self.db, resolver, path, def);
|
let substs = Ty::substs_from_path(self.db, resolver, path, def);
|
||||||
match def {
|
match def {
|
||||||
TypableDef::Struct(s) => {
|
TypableDef::AdtDef(AdtDef::Struct(s)) => {
|
||||||
let ty = s.ty(self.db);
|
let ty = s.ty(self.db);
|
||||||
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()))
|
||||||
|
@ -678,10 +678,10 @@ 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(var.into()))
|
(ty, Some(var.into()))
|
||||||
}
|
}
|
||||||
TypableDef::Union(_)
|
TypableDef::AdtDef(AdtDef::Enum(_))
|
||||||
|
| TypableDef::AdtDef(AdtDef::Union(_))
|
||||||
| TypableDef::TypeAlias(_)
|
| TypableDef::TypeAlias(_)
|
||||||
| TypableDef::Function(_)
|
| TypableDef::Function(_)
|
||||||
| TypableDef::Enum(_)
|
|
||||||
| TypableDef::Const(_)
|
| TypableDef::Const(_)
|
||||||
| TypableDef::Static(_)
|
| TypableDef::Static(_)
|
||||||
| TypableDef::BuiltinType(_) => (Ty::Unknown, None),
|
| TypableDef::BuiltinType(_) => (Ty::Unknown, None),
|
||||||
|
|
|
@ -172,9 +172,7 @@ impl Ty {
|
||||||
) -> Substs {
|
) -> Substs {
|
||||||
let def_generic: Option<GenericDef> = match resolved {
|
let def_generic: Option<GenericDef> = match resolved {
|
||||||
TypableDef::Function(func) => Some(func.into()),
|
TypableDef::Function(func) => Some(func.into()),
|
||||||
TypableDef::Struct(s) => Some(s.into()),
|
TypableDef::AdtDef(adt) => Some(adt.into()),
|
||||||
TypableDef::Union(u) => Some(u.into()),
|
|
||||||
TypableDef::Enum(e) => Some(e.into()),
|
|
||||||
TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
|
TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
|
||||||
TypableDef::TypeAlias(t) => Some(t.into()),
|
TypableDef::TypeAlias(t) => Some(t.into()),
|
||||||
TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None,
|
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 last = path.segments.last().expect("path should have at least one segment");
|
||||||
let segment = match resolved {
|
let segment = match resolved {
|
||||||
TypableDef::Function(_)
|
TypableDef::Function(_)
|
||||||
| TypableDef::Struct(_)
|
| TypableDef::AdtDef(_)
|
||||||
| TypableDef::Union(_)
|
|
||||||
| TypableDef::Enum(_)
|
|
||||||
| TypableDef::Const(_)
|
| TypableDef::Const(_)
|
||||||
| TypableDef::Static(_)
|
| TypableDef::Static(_)
|
||||||
| TypableDef::TypeAlias(_)
|
| 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 {
|
pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty {
|
||||||
match (def, ns) {
|
match (def, ns) {
|
||||||
(TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f),
|
(TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f),
|
||||||
(TypableDef::Struct(s), Namespace::Types) => type_for_adt(db, s),
|
(TypableDef::AdtDef(AdtDef::Struct(s)), Namespace::Values) => {
|
||||||
(TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s),
|
type_for_struct_constructor(db, s)
|
||||||
(TypableDef::Enum(e), Namespace::Types) => type_for_adt(db, e),
|
}
|
||||||
|
(TypableDef::AdtDef(adt), Namespace::Types) => type_for_adt(db, adt),
|
||||||
(TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v),
|
(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::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),
|
||||||
(TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
|
(TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
|
||||||
(TypableDef::Static(c), Namespace::Values) => type_for_static(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:
|
// 'error' cases:
|
||||||
(TypableDef::Function(_), Namespace::Types) => Ty::Unknown,
|
(TypableDef::Function(_), Namespace::Types) => Ty::Unknown,
|
||||||
(TypableDef::Union(_), Namespace::Values) => Ty::Unknown,
|
(TypableDef::AdtDef(AdtDef::Union(_)), Namespace::Values) => Ty::Unknown,
|
||||||
(TypableDef::Enum(_), Namespace::Values) => Ty::Unknown,
|
(TypableDef::AdtDef(AdtDef::Enum(_)), Namespace::Values) => Ty::Unknown,
|
||||||
(TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown,
|
(TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown,
|
||||||
(TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown,
|
(TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown,
|
||||||
(TypableDef::Const(_), Namespace::Types) => 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)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum TypableDef {
|
pub enum TypableDef {
|
||||||
Function(Function),
|
Function(Function),
|
||||||
Struct(Struct),
|
AdtDef(AdtDef),
|
||||||
Union(Union),
|
|
||||||
Enum(Enum),
|
|
||||||
EnumVariant(EnumVariant),
|
EnumVariant(EnumVariant),
|
||||||
TypeAlias(TypeAlias),
|
TypeAlias(TypeAlias),
|
||||||
Const(Const),
|
Const(Const),
|
||||||
Static(Static),
|
Static(Static),
|
||||||
BuiltinType(BuiltinType),
|
BuiltinType(BuiltinType),
|
||||||
}
|
}
|
||||||
impl_froms!(
|
impl_froms!(TypableDef: Function, AdtDef, EnumVariant, TypeAlias, Const, Static, BuiltinType);
|
||||||
TypableDef: Function,
|
|
||||||
Struct,
|
impl From<Struct> for TypableDef {
|
||||||
Union,
|
fn from(it: Struct) -> TypableDef {
|
||||||
Enum,
|
TypableDef::AdtDef(AdtDef::Struct(it))
|
||||||
EnumVariant,
|
}
|
||||||
TypeAlias,
|
}
|
||||||
Const,
|
|
||||||
Static,
|
impl From<Enum> for TypableDef {
|
||||||
BuiltinType
|
fn from(it: Enum) -> TypableDef {
|
||||||
);
|
TypableDef::AdtDef(AdtDef::Enum(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Union> for TypableDef {
|
||||||
|
fn from(it: Union) -> TypableDef {
|
||||||
|
TypableDef::AdtDef(AdtDef::Union(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<ModuleDef> for Option<TypableDef> {
|
impl From<ModuleDef> for Option<TypableDef> {
|
||||||
fn from(def: ModuleDef) -> Option<TypableDef> {
|
fn from(def: ModuleDef) -> Option<TypableDef> {
|
||||||
let res = match def {
|
let res = match def {
|
||||||
ModuleDef::Function(f) => f.into(),
|
ModuleDef::Function(f) => f.into(),
|
||||||
ModuleDef::Struct(s) => s.into(),
|
ModuleDef::AdtDef(adt) => adt.into(),
|
||||||
ModuleDef::Union(u) => u.into(),
|
|
||||||
ModuleDef::Enum(e) => e.into(),
|
|
||||||
ModuleDef::EnumVariant(v) => v.into(),
|
ModuleDef::EnumVariant(v) => v.into(),
|
||||||
ModuleDef::TypeAlias(t) => t.into(),
|
ModuleDef::TypeAlias(t) => t.into(),
|
||||||
ModuleDef::Const(v) => v.into(),
|
ModuleDef::Const(v) => v.into(),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use hir::{Either, Resolution};
|
use hir::{AdtDef, Either, Resolution};
|
||||||
use ra_syntax::AstNode;
|
use ra_syntax::AstNode;
|
||||||
use test_utils::tested_by;
|
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));
|
acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::ModuleDef::Enum(_)
|
hir::ModuleDef::AdtDef(_) | hir::ModuleDef::TypeAlias(_) => {
|
||||||
| hir::ModuleDef::Struct(_)
|
if let hir::ModuleDef::AdtDef(AdtDef::Enum(e)) = def {
|
||||||
| hir::ModuleDef::Union(_)
|
|
||||||
| hir::ModuleDef::TypeAlias(_) => {
|
|
||||||
if let hir::ModuleDef::Enum(e) = def {
|
|
||||||
for variant in e.variants(ctx.db) {
|
for variant in e.variants(ctx.db) {
|
||||||
acc.add_enum_variant(ctx, variant);
|
acc.add_enum_variant(ctx, variant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let ty = match def {
|
let ty = match def {
|
||||||
hir::ModuleDef::Enum(e) => e.ty(ctx.db),
|
hir::ModuleDef::AdtDef(adt) => adt.ty(ctx.db),
|
||||||
hir::ModuleDef::Struct(s) => s.ty(ctx.db),
|
|
||||||
hir::ModuleDef::Union(u) => u.ty(ctx.db),
|
|
||||||
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
|
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
match def {
|
match def {
|
||||||
hir::ModuleDef::Enum(..)
|
hir::ModuleDef::AdtDef(hir::AdtDef::Enum(..))
|
||||||
| hir::ModuleDef::EnumVariant(..)
|
| hir::ModuleDef::EnumVariant(..)
|
||||||
| hir::ModuleDef::Const(..)
|
| hir::ModuleDef::Const(..)
|
||||||
| hir::ModuleDef::Module(..) => (),
|
| hir::ModuleDef::Module(..) => (),
|
||||||
|
|
|
@ -67,9 +67,15 @@ impl Completions {
|
||||||
Resolution::Def(Function(func)) => {
|
Resolution::Def(Function(func)) => {
|
||||||
return self.add_function_with_name(ctx, Some(local_name), *func);
|
return self.add_function_with_name(ctx, Some(local_name), *func);
|
||||||
}
|
}
|
||||||
Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
Resolution::Def(AdtDef(hir::AdtDef::Struct(it))) => {
|
||||||
Resolution::Def(Union(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)),
|
(CompletionItemKind::Struct, it.docs(ctx.db))
|
||||||
Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, 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(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)),
|
||||||
Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)),
|
Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)),
|
||||||
Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)),
|
Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)),
|
||||||
|
|
|
@ -193,9 +193,7 @@ impl NavigationTarget {
|
||||||
let nav = match module_def {
|
let nav = match module_def {
|
||||||
hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
|
hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
|
||||||
hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func),
|
hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func),
|
||||||
hir::ModuleDef::Struct(it) => NavigationTarget::from_adt_def(db, it.into()),
|
hir::ModuleDef::AdtDef(it) => NavigationTarget::from_adt_def(db, it),
|
||||||
hir::ModuleDef::Enum(it) => NavigationTarget::from_adt_def(db, it.into()),
|
|
||||||
hir::ModuleDef::Union(it) => NavigationTarget::from_adt_def(db, it.into()),
|
|
||||||
hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it),
|
hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it),
|
||||||
hir::ModuleDef::Static(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),
|
hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use hir::{HasSource, HirDisplay};
|
use hir::{AdtDef, HasSource, HirDisplay};
|
||||||
use ra_db::SourceDatabase;
|
use ra_db::SourceDatabase;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::{
|
algo::{
|
||||||
|
@ -129,9 +129,13 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)),
|
hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)),
|
||||||
hir::ModuleDef::Struct(it) => res.extend(from_def_source(db, it)),
|
hir::ModuleDef::AdtDef(AdtDef::Struct(it)) => {
|
||||||
hir::ModuleDef::Union(it) => res.extend(from_def_source(db, it)),
|
res.extend(from_def_source(db, it))
|
||||||
hir::ModuleDef::Enum(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::EnumVariant(it) => res.extend(from_def_source(db, it)),
|
||||||
hir::ModuleDef::Const(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)),
|
hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)),
|
||||||
|
|
|
@ -107,9 +107,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
Some(AssocItem(hir::ImplItem::TypeAlias(_))) => "type",
|
Some(AssocItem(hir::ImplItem::TypeAlias(_))) => "type",
|
||||||
Some(Def(hir::ModuleDef::Module(_))) => "module",
|
Some(Def(hir::ModuleDef::Module(_))) => "module",
|
||||||
Some(Def(hir::ModuleDef::Function(_))) => "function",
|
Some(Def(hir::ModuleDef::Function(_))) => "function",
|
||||||
Some(Def(hir::ModuleDef::Struct(_))) => "type",
|
Some(Def(hir::ModuleDef::AdtDef(_))) => "type",
|
||||||
Some(Def(hir::ModuleDef::Union(_))) => "type",
|
|
||||||
Some(Def(hir::ModuleDef::Enum(_))) => "type",
|
|
||||||
Some(Def(hir::ModuleDef::EnumVariant(_))) => "constant",
|
Some(Def(hir::ModuleDef::EnumVariant(_))) => "constant",
|
||||||
Some(Def(hir::ModuleDef::Const(_))) => "constant",
|
Some(Def(hir::ModuleDef::Const(_))) => "constant",
|
||||||
Some(Def(hir::ModuleDef::Static(_))) => "constant",
|
Some(Def(hir::ModuleDef::Static(_))) => "constant",
|
||||||
|
|
Loading…
Reference in a new issue