aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-05-19 14:08:16 +0100
committerEdwin Cheng <[email protected]>2019-05-19 14:08:16 +0100
commitd4dc8794159fbee10fcfe78d5da3a7bf41d7f4f6 (patch)
tree3d12bfa68f784e6ac2fa73bcbee516cdb22f4f1e /crates
parente74d8f148b805f9f6cb1331d1a96f680948da399 (diff)
Add infer for generic default type
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/db.rs5
-rw-r--r--crates/ra_hir/src/ty.rs2
-rw-r--r--crates/ra_hir/src/ty/lower.rs59
-rw-r--r--crates/ra_hir/src/ty/tests.rs29
4 files changed, 80 insertions, 15 deletions
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 8e827d4f5..bda02d3cc 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -11,7 +11,7 @@ use crate::{
11 DefWithBody, Trait, 11 DefWithBody, Trait,
12 ids, 12 ids,
13 nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap}, 13 nameres::{Namespace, ImportSourceMap, RawItems, CrateDefMap},
14 ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig, TypeCtor, GenericPredicate}, 14 ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig, TypeCtor, GenericPredicate, Substs},
15 adt::{StructData, EnumData}, 15 adt::{StructData, EnumData},
16 impl_block::{ModuleImplBlocks, ImplSourceMap, ImplBlock}, 16 impl_block::{ModuleImplBlocks, ImplSourceMap, ImplBlock},
17 generics::{GenericParams, GenericDef}, 17 generics::{GenericParams, GenericDef},
@@ -141,6 +141,9 @@ pub trait HirDatabase: DefDatabase {
141 #[salsa::invoke(crate::ty::generic_predicates)] 141 #[salsa::invoke(crate::ty::generic_predicates)]
142 fn generic_predicates(&self, def: GenericDef) -> Arc<[GenericPredicate]>; 142 fn generic_predicates(&self, def: GenericDef) -> Arc<[GenericPredicate]>;
143 143
144 #[salsa::invoke(crate::ty::generic_defaults)]
145 fn generic_defaults(&self, def: GenericDef) -> Substs;
146
144 #[salsa::invoke(crate::expr::body_with_source_map_query)] 147 #[salsa::invoke(crate::expr::body_with_source_map_query)]
145 fn body_with_source_map( 148 fn body_with_source_map(
146 &self, 149 &self,
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index cfe07156b..9a65bf567 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -19,7 +19,7 @@ use std::{fmt, mem};
19use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase, Trait, GenericParams}; 19use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase, Trait, GenericParams};
20use display::{HirDisplay, HirFormatter}; 20use display::{HirDisplay, HirFormatter};
21 21
22pub(crate) use lower::{TypableDef, type_for_def, type_for_field, callable_item_sig, generic_predicates}; 22pub(crate) use lower::{TypableDef, type_for_def, type_for_field, callable_item_sig, generic_predicates, generic_defaults};
23pub(crate) use infer::{infer, InferenceResult, InferTy}; 23pub(crate) use infer::{infer, InferenceResult, InferTy};
24pub use lower::CallableDef; 24pub use lower::CallableDef;
25 25
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#"