aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-07-06 16:43:13 +0100
committerFlorian Diebold <[email protected]>2019-07-06 16:43:13 +0100
commitf854a29c9adcfeaa7164928ff91daab9ca9a063c (patch)
treecce758eabcbcfbb1bc1673601477a9c86580cb2b /crates
parent065d3987012b138b123f0544de193f8bb560b1b5 (diff)
Make EnumVariant a GenericDef and simplify some code
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/generics.rs12
-rw-r--r--crates/ra_hir/src/ty/infer.rs7
-rw-r--r--crates/ra_hir/src/ty/lower.rs10
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs6
4 files changed, 21 insertions, 14 deletions
diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs
index 9929331d3..521e47090 100644
--- a/crates/ra_hir/src/generics.rs
+++ b/crates/ra_hir/src/generics.rs
@@ -11,8 +11,8 @@ use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 path::Path, 12 path::Path,
13 type_ref::TypeRef, 13 type_ref::TypeRef,
14 AdtDef, AsName, Container, Enum, Function, HasSource, ImplBlock, Name, Struct, Trait, 14 AdtDef, AsName, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct,
15 TypeAlias, Union, 15 Trait, TypeAlias, Union,
16}; 16};
17 17
18/// Data about a generic parameter (to a function, struct, impl, ...). 18/// Data about a generic parameter (to a function, struct, impl, ...).
@@ -50,8 +50,11 @@ pub enum GenericDef {
50 Trait(Trait), 50 Trait(Trait),
51 TypeAlias(TypeAlias), 51 TypeAlias(TypeAlias),
52 ImplBlock(ImplBlock), 52 ImplBlock(ImplBlock),
53 // enum variants cannot have generics themselves, but their parent enums
54 // can, and this makes some code easier to write
55 EnumVariant(EnumVariant),
53} 56}
54impl_froms!(GenericDef: Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlock); 57impl_froms!(GenericDef: Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlock, EnumVariant);
55 58
56impl GenericParams { 59impl GenericParams {
57 pub(crate) fn generic_params_query( 60 pub(crate) fn generic_params_query(
@@ -62,6 +65,7 @@ impl GenericParams {
62 let parent = match def { 65 let parent = match def {
63 GenericDef::Function(it) => it.container(db).map(GenericDef::from), 66 GenericDef::Function(it) => it.container(db).map(GenericDef::from),
64 GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from), 67 GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from),
68 GenericDef::EnumVariant(it) => Some(it.parent_enum(db).into()),
65 GenericDef::Struct(_) 69 GenericDef::Struct(_)
66 | GenericDef::Union(_) 70 | GenericDef::Union(_)
67 | GenericDef::Enum(_) 71 | GenericDef::Enum(_)
@@ -86,6 +90,7 @@ impl GenericParams {
86 } 90 }
87 GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).ast, start), 91 GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).ast, start),
88 GenericDef::ImplBlock(it) => generics.fill(&*it.source(db).ast, start), 92 GenericDef::ImplBlock(it) => generics.fill(&*it.source(db).ast, start),
93 GenericDef::EnumVariant(_) => {}
89 } 94 }
90 95
91 Arc::new(generics) 96 Arc::new(generics)
@@ -184,6 +189,7 @@ impl GenericDef {
184 GenericDef::Trait(inner) => inner.resolver(db), 189 GenericDef::Trait(inner) => inner.resolver(db),
185 GenericDef::TypeAlias(inner) => inner.resolver(db), 190 GenericDef::TypeAlias(inner) => inner.resolver(db),
186 GenericDef::ImplBlock(inner) => inner.resolver(db), 191 GenericDef::ImplBlock(inner) => inner.resolver(db),
192 GenericDef::EnumVariant(inner) => inner.parent_enum(db).resolver(db),
187 } 193 }
188 } 194 }
189} 195}
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index f5d8cd4b1..e79e5e223 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -849,12 +849,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
849 fn register_obligations_for_call(&mut self, callable_ty: &Ty) { 849 fn register_obligations_for_call(&mut self, callable_ty: &Ty) {
850 if let Ty::Apply(a_ty) = callable_ty { 850 if let Ty::Apply(a_ty) = callable_ty {
851 if let TypeCtor::FnDef(def) = a_ty.ctor { 851 if let TypeCtor::FnDef(def) = a_ty.ctor {
852 let generic_predicates = self.db.generic_predicates(match def { 852 let generic_predicates = self.db.generic_predicates(def.into());
853 // TODO add helper function
854 CallableDef::Function(f) => f.into(),
855 CallableDef::Struct(s) => s.into(),
856 CallableDef::EnumVariant(_e) => unimplemented!(),
857 });
858 for predicate in generic_predicates.iter() { 853 for predicate in generic_predicates.iter() {
859 let predicate = predicate.clone().subst(&a_ty.parameters); 854 let predicate = predicate.clone().subst(&a_ty.parameters);
860 if let Some(obligation) = Obligation::from_predicate(predicate) { 855 if let Some(obligation) = Obligation::from_predicate(predicate) {
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 24755c6aa..b48ada760 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -509,3 +509,13 @@ pub enum CallableDef {
509 EnumVariant(EnumVariant), 509 EnumVariant(EnumVariant),
510} 510}
511impl_froms!(CallableDef: Function, Struct, EnumVariant); 511impl_froms!(CallableDef: Function, Struct, EnumVariant);
512
513impl From<CallableDef> for GenericDef {
514 fn from(def: CallableDef) -> GenericDef {
515 match def {
516 CallableDef::Function(f) => f.into(),
517 CallableDef::Struct(s) => s.into(),
518 CallableDef::EnumVariant(e) => e.into(),
519 }
520 }
521}
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index 2a0537bc3..4c3744b44 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -428,11 +428,7 @@ pub(crate) fn struct_datum_query(
428 CallableDef::Struct(s) => s.module(db).krate(db), 428 CallableDef::Struct(s) => s.module(db).krate(db),
429 CallableDef::EnumVariant(v) => v.parent_enum(db).module(db).krate(db), 429 CallableDef::EnumVariant(v) => v.parent_enum(db).module(db).krate(db),
430 } != Some(krate); 430 } != Some(krate);
431 let generic_def: GenericDef = match callable { 431 let generic_def: GenericDef = callable.into();
432 CallableDef::Function(f) => f.into(),
433 CallableDef::Struct(s) => s.into(),
434 CallableDef::EnumVariant(v) => v.parent_enum(db).into(),
435 };
436 let generic_params = generic_def.generic_params(db); 432 let generic_params = generic_def.generic_params(db);
437 let bound_vars = Substs::bound_vars(&generic_params); 433 let bound_vars = Substs::bound_vars(&generic_params);
438 let where_clauses = convert_where_clauses(db, generic_def, &bound_vars); 434 let where_clauses = convert_where_clauses(db, generic_def, &bound_vars);