From f6eb44cd9e2a27d2004386c71fb6c72f79ba83b3 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Sat, 26 Jan 2019 19:25:57 +0100 Subject: Use type information from the turbofish --- crates/ra_hir/src/ty.rs | 34 ++++++++++++++++++---- .../ra_hir/src/ty/snapshots/tests__generic_fn.snap | 7 +++-- .../snapshots/tests__infer_function_generics.snap | 8 ++--- crates/ra_hir/src/ty/tests.rs | 3 ++ 4 files changed, 40 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 9332aca9a..00a71a42f 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -1157,6 +1157,32 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let typable = typable?; let ty = self.db.type_for_def(typable); let ty = self.insert_type_vars(ty); + + // try to get generic parameters from the path and add them to the + // function type substitutions + if let Ty::FnDef { ref def, .. } = ty { + let last_seg_bindings = path + .segments + .last() + .and_then(|segment| segment.args_and_bindings.as_ref()); + if let Some(generic_args) = last_seg_bindings { + let generic_params = def.generic_params(self.db); + if generic_args.args.len() == generic_params.params.len() { + let substs = Ty::substs_from_path( + self.db, + &self.module, + self.impl_block.as_ref(), + &generic_params, + path, + (*def).into(), + ); + return Some(ty.apply_substs(substs)); + } else { + // ERROR: incorrect number of type params + } + } + } + Some(ty) } @@ -1387,9 +1413,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let callee_ty = self.infer_expr(*callee, &Expectation::none()); let (param_tys, ret_ty) = match &callee_ty { Ty::FnPtr(sig) => (sig.input.clone(), sig.output.clone()), - Ty::FnDef { - def, substs, sig, .. - } => { + Ty::FnDef { substs, sig, .. } => { let ret_ty = sig.output.clone().subst(&substs); let param_tys = sig .input @@ -1437,9 +1461,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { (Ty::Unknown, Vec::new(), sig.output.clone()) } } - Ty::FnDef { - def, substs, sig, .. - } => { + Ty::FnDef { substs, sig, .. } => { let ret_ty = sig.output.clone().subst(&substs); if sig.input.len() > 0 { diff --git a/crates/ra_hir/src/ty/snapshots/tests__generic_fn.snap b/crates/ra_hir/src/ty/snapshots/tests__generic_fn.snap index 84fbe6e4c..85aeefa0c 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__generic_fn.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__generic_fn.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-26T17:46:03.963745056+00:00" +created: "2019-01-26T18:16:16.568375+00:00" creator: insta@0.5.2 expression: "&result" source: crates/ra_hir/src/ty/tests.rs @@ -10,7 +10,7 @@ source: crates/ra_hir/src/ty/tests.rs [44; 45) 'x': &[unknown] [56; 65) '{ x }': &[unknown] [62; 63) 'x': &[unknown] -[77; 138) '{ ...(z); }': () +[77; 197) '{ ...(1); }': () [87; 88) 'y': u32 [91; 96) '10u32': u32 [102; 104) 'id': fn id(T) -> T @@ -20,4 +20,7 @@ source: crates/ra_hir/src/ty/tests.rs [127; 132) 'clone': fn clone(&T) -> T [127; 135) 'clone(z)': bool [133; 134) 'z': &bool +[173; 191) 'id::': fn id(T) -> T +[173; 194) 'id::(1)': i32 +[192; 193) '1': i32 diff --git a/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap b/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap index 369705f84..8ff6e55a6 100644 --- a/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap +++ b/crates/ra_hir/src/ty/snapshots/tests__infer_function_generics.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-26T17:46:03.856278205+00:00" +created: "2019-01-26T18:16:16.530712344+00:00" creator: insta@0.5.2 expression: "&result" source: crates/ra_hir/src/ty/tests.rs @@ -11,9 +11,9 @@ source: crates/ra_hir/src/ty/tests.rs [44; 46) 'id': fn id(T) -> T [44; 52) 'id(1u32)': u32 [47; 51) '1u32': u32 -[58; 68) 'id::': fn id(T) -> T -[58; 71) 'id::(1)': i32 -[69; 70) '1': i32 +[58; 68) 'id::': fn id(T) -> T +[58; 71) 'id::(1)': i128 +[69; 70) '1': i128 [81; 82) 'x': u64 [90; 92) 'id': fn id(T) -> T [90; 95) 'id(1)': u64 diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 40913b164..fac566626 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -611,6 +611,9 @@ fn test() { let y = 10u32; id(y); let x: bool = clone(z); + + // bad turbofish - ignore! + id::(1); } "#, ); -- cgit v1.2.3