aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/generics.rs4
-rw-r--r--crates/ra_hir/src/ty/lower.rs32
2 files changed, 16 insertions, 20 deletions
diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs
index a635c7184..79a7fa23a 100644
--- a/crates/ra_hir/src/generics.rs
+++ b/crates/ra_hir/src/generics.rs
@@ -197,13 +197,13 @@ impl From<crate::adt::AdtDef> for GenericDef {
197 } 197 }
198} 198}
199 199
200pub trait HasGenericParams { 200pub trait HasGenericParams: Copy {
201 fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>; 201 fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
202} 202}
203 203
204impl<T> HasGenericParams for T 204impl<T> HasGenericParams for T
205where 205where
206 T: Into<GenericDef>, 206 T: Into<GenericDef> + Copy,
207{ 207{
208 fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams> { 208 fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams> {
209 db.generic_params(self.into()) 209 db.generic_params(self.into())
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 8a8cc2a24..a1a2d0f6b 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -9,9 +9,7 @@ use std::sync::Arc;
9use std::iter; 9use std::iter;
10 10
11use crate::{ 11use crate::{
12 Function, Struct, StructField, Enum, EnumVariant, Path, 12 Function, Struct, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static,
13 ModuleDef, TypeAlias,
14 Const, Static,
15 HirDatabase, 13 HirDatabase,
16 type_ref::TypeRef, 14 type_ref::TypeRef,
17 name::KnownName, 15 name::KnownName,
@@ -19,7 +17,10 @@ use crate::{
19 resolve::{Resolver, Resolution}, 17 resolve::{Resolver, Resolution},
20 path::{PathSegment, GenericArg}, 18 path::{PathSegment, GenericArg},
21 generics::{HasGenericParams}, 19 generics::{HasGenericParams},
22 adt::VariantDef, Trait, generics::{ WherePredicate, GenericDef} 20 adt::VariantDef,
21 Trait,
22 generics::{WherePredicate, GenericDef},
23 ty::AdtDef,
23}; 24};
24use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef, GenericPredicate}; 25use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef, GenericPredicate};
25 26
@@ -288,9 +289,9 @@ impl TraitRef {
288pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty { 289pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace) -> Ty {
289 match (def, ns) { 290 match (def, ns) {
290 (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f), 291 (TypableDef::Function(f), Namespace::Values) => type_for_fn(db, f),
291 (TypableDef::Struct(s), Namespace::Types) => type_for_struct(db, s), 292 (TypableDef::Struct(s), Namespace::Types) => type_for_adt(db, s),
292 (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s), 293 (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s),
293 (TypableDef::Enum(e), Namespace::Types) => type_for_enum(db, e), 294 (TypableDef::Enum(e), Namespace::Types) => type_for_adt(db, e),
294 (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), 295 (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v),
295 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), 296 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),
296 (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), 297 (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
@@ -405,7 +406,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
405 .iter() 406 .iter()
406 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) 407 .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
407 .collect::<Vec<_>>(); 408 .collect::<Vec<_>>();
408 let ret = type_for_struct(db, def); 409 let ret = type_for_adt(db, def);
409 FnSig::from_params_and_return(params, ret) 410 FnSig::from_params_and_return(params, ret)
410} 411}
411 412
@@ -413,7 +414,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
413fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { 414fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
414 let var_data = def.variant_data(db); 415 let var_data = def.variant_data(db);
415 if var_data.fields().is_none() { 416 if var_data.fields().is_none() {
416 return type_for_struct(db, def); // Unit struct 417 return type_for_adt(db, def); // Unit struct
417 } 418 }
418 let generics = def.generic_params(db); 419 let generics = def.generic_params(db);
419 let substs = Substs::identity(&generics); 420 let substs = Substs::identity(&generics);
@@ -433,7 +434,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant)
433 .collect::<Vec<_>>(); 434 .collect::<Vec<_>>();
434 let generics = def.parent_enum(db).generic_params(db); 435 let generics = def.parent_enum(db).generic_params(db);
435 let substs = Substs::identity(&generics); 436 let substs = Substs::identity(&generics);
436 let ret = type_for_enum(db, def.parent_enum(db)).subst(&substs); 437 let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs);
437 FnSig::from_params_and_return(params, ret) 438 FnSig::from_params_and_return(params, ret)
438} 439}
439 440
@@ -441,21 +442,16 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant)
441fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty { 442fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> Ty {
442 let var_data = def.variant_data(db); 443 let var_data = def.variant_data(db);
443 if var_data.fields().is_none() { 444 if var_data.fields().is_none() {
444 return type_for_enum(db, def.parent_enum(db)); // Unit variant 445 return type_for_adt(db, def.parent_enum(db)); // Unit variant
445 } 446 }
446 let generics = def.parent_enum(db).generic_params(db); 447 let generics = def.parent_enum(db).generic_params(db);
447 let substs = Substs::identity(&generics); 448 let substs = Substs::identity(&generics);
448 Ty::apply(TypeCtor::FnDef(def.into()), substs) 449 Ty::apply(TypeCtor::FnDef(def.into()), substs)
449} 450}
450 451
451fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty { 452fn type_for_adt(db: &impl HirDatabase, adt: impl Into<AdtDef> + HasGenericParams) -> Ty {
452 let generics = s.generic_params(db); 453 let generics = adt.generic_params(db);
453 Ty::apply(TypeCtor::Adt(s.into()), Substs::identity(&generics)) 454 Ty::apply(TypeCtor::Adt(adt.into()), Substs::identity(&generics))
454}
455
456fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty {
457 let generics = s.generic_params(db);
458 Ty::apply(TypeCtor::Adt(s.into()), Substs::identity(&generics))
459} 455}
460 456
461fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { 457fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {