aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/lower.rs59
-rw-r--r--crates/ra_hir/src/ty/tests.rs29
2 files changed, 75 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);
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index f8364203d..cd24faba5 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -1449,6 +1449,35 @@ fn test() {
1449} 1449}
1450 1450
1451#[test] 1451#[test]
1452fn infer_associated_method_generics_with_default_param() {
1453 assert_snapshot_matches!(
1454 infer(r#"
1455struct Gen<T=u32> {
1456 val: T
1457}
1458
1459impl<T> Gen<T> {
1460 pub fn make() -> Gen<T> {
1461 loop { }
1462 }
1463}
1464
1465fn test() {
1466 let a = Gen::make();
1467}
1468"#),
1469 @r###"
1470[80; 104) '{ ... }': !
1471[90; 98) 'loop { }': !
1472[95; 98) '{ }': ()
1473[118; 146) '{ ...e(); }': ()
1474[128; 129) 'a': Gen<u32>
1475[132; 141) 'Gen::make': fn make<u32>() -> Gen<T>
1476[132; 143) 'Gen::make()': Gen<u32>"###
1477 );
1478}
1479
1480#[test]
1452fn infer_associated_method_generics_without_args() { 1481fn infer_associated_method_generics_without_args() {
1453 assert_snapshot_matches!( 1482 assert_snapshot_matches!(
1454 infer(r#" 1483 infer(r#"