aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty/lower.rs')
-rw-r--r--crates/ra_hir/src/ty/lower.rs59
1 files changed, 46 insertions, 13 deletions
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 09d26ce5a..b4496789b 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -18,7 +18,7 @@ use crate::{
18 nameres::Namespace, 18 nameres::Namespace,
19 resolve::{Resolver, Resolution}, 19 resolve::{Resolver, Resolution},
20 path::{PathSegment, GenericArg}, 20 path::{PathSegment, GenericArg},
21 generics::{GenericParams, HasGenericParams}, 21 generics::{HasGenericParams},
22 adt::VariantDef, Trait, generics::{ WherePredicate, GenericDef} 22 adt::VariantDef, Trait, generics::{ WherePredicate, GenericDef}
23}; 23};
24use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef, GenericPredicate}; 24use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef, GenericPredicate};
@@ -120,15 +120,15 @@ impl Ty {
120 segment: &PathSegment, 120 segment: &PathSegment,
121 resolved: TypableDef, 121 resolved: TypableDef,
122 ) -> Substs { 122 ) -> Substs {
123 let def_generics = match resolved { 123 let def_generic: Option<GenericDef> = match resolved {
124 TypableDef::Function(func) => func.generic_params(db), 124 TypableDef::Function(func) => Some(func.into()),
125 TypableDef::Struct(s) => s.generic_params(db), 125 TypableDef::Struct(s) => Some(s.into()),
126 TypableDef::Enum(e) => e.generic_params(db), 126 TypableDef::Enum(e) => Some(e.into()),
127 TypableDef::EnumVariant(var) => var.parent_enum(db).generic_params(db), 127 TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
128 TypableDef::TypeAlias(t) => t.generic_params(db), 128 TypableDef::TypeAlias(t) => Some(t.into()),
129 TypableDef::Const(_) | TypableDef::Static(_) => GenericParams::default().into(), 129 TypableDef::Const(_) | TypableDef::Static(_) => None,
130 }; 130 };
131 substs_from_path_segment(db, resolver, segment, &def_generics, false) 131 substs_from_path_segment(db, resolver, segment, def_generic, false)
132 } 132 }
133 133
134 /// Collect generic arguments from a path into a `Substs`. See also 134 /// Collect generic arguments from a path into a `Substs`. See also
@@ -172,10 +172,12 @@ pub(super) fn substs_from_path_segment(
172 db: &impl HirDatabase, 172 db: &impl HirDatabase,
173 resolver: &Resolver, 173 resolver: &Resolver,
174 segment: &PathSegment, 174 segment: &PathSegment,
175 def_generics: &GenericParams, 175 def_generic: Option<GenericDef>,
176 add_self_param: bool, 176 add_self_param: bool,
177) -> Substs { 177) -> Substs {
178 let mut substs = Vec::new(); 178 let mut substs = Vec::new();
179 let def_generics = def_generic.map(|def| def.generic_params(db)).unwrap_or_default();
180
179 let parent_param_count = def_generics.count_parent_params(); 181 let parent_param_count = def_generics.count_parent_params();
180 substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count)); 182 substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count));
181 if add_self_param { 183 if add_self_param {
@@ -199,13 +201,28 @@ pub(super) fn substs_from_path_segment(
199 } 201 }
200 } 202 }
201 // add placeholders for args that were not provided 203 // add placeholders for args that were not provided
202 // FIXME: handle defaults
203 let supplied_params = substs.len(); 204 let supplied_params = substs.len();
204 for _ in supplied_params..def_generics.count_params_including_parent() { 205 for _ in supplied_params..def_generics.count_params_including_parent() {
205 substs.push(Ty::Unknown); 206 substs.push(Ty::Unknown);
206 } 207 }
207 assert_eq!(substs.len(), def_generics.count_params_including_parent()); 208 assert_eq!(substs.len(), def_generics.count_params_including_parent());
208 Substs(substs.into()) 209 let mut substs = Substs(substs.into());
210
211 // handle defaults
212 if let Some(def_generic) = def_generic {
213 let default_substs = db.generic_defaults(def_generic);
214 assert_eq!(substs.len(), default_substs.len());
215
216 let mut i = 0;
217 substs.walk_mut(&mut |ty| {
218 if *ty == Ty::Unknown {
219 *ty = default_substs[i].clone();
220 }
221 i += 1;
222 });
223 }
224
225 substs
209} 226}
210 227
211impl TraitRef { 228impl TraitRef {
@@ -249,7 +266,7 @@ impl TraitRef {
249 resolved: Trait, 266 resolved: Trait,
250 ) -> Substs { 267 ) -> Substs {
251 let segment = path.segments.last().expect("path should have at least one segment"); 268 let segment = path.segments.last().expect("path should have at least one segment");
252 substs_from_path_segment(db, resolver, segment, &resolved.generic_params(db), true) 269 substs_from_path_segment(db, resolver, segment, Some(resolved.into()), true)
253 } 270 }
254 271
255 pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef { 272 pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef {
@@ -331,6 +348,22 @@ pub(crate) fn generic_predicates(
331 predicates.into() 348 predicates.into()
332} 349}
333 350
351/// Resolve the default type params from generics
352pub(crate) fn generic_defaults(db: &impl HirDatabase, def: GenericDef) -> Substs {
353 let resolver = def.resolver(db);
354 let generic_params = def.generic_params(db);
355
356 let defaults = generic_params
357 .params_including_parent()
358 .into_iter()
359 .map(|p| {
360 p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path))
361 })
362 .collect::<Vec<_>>();
363
364 Substs(defaults.into())
365}
366
334fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { 367fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
335 let signature = def.signature(db); 368 let signature = def.signature(db);
336 let resolver = def.resolver(db); 369 let resolver = def.resolver(db);