diff options
author | Edwin Cheng <[email protected]> | 2019-12-24 11:45:28 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-12-24 11:45:28 +0000 |
commit | 0edb5b4a501a66baa7db8ececf46135e6246f4de (patch) | |
tree | a65f9899f109d4df2c05069e037fe5dec0545d73 /crates/ra_hir_def | |
parent | 60aa4d12f95477565d5b01f122d2c9dd845015b4 (diff) |
Implement infer await from async func
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 27 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 5 |
2 files changed, 30 insertions, 2 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 1aa9a9b7d..0a282f31b 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -6,12 +6,15 @@ use hir_expand::{ | |||
6 | name::{name, AsName, Name}, | 6 | name::{name, AsName, Name}, |
7 | AstId, InFile, | 7 | AstId, InFile, |
8 | }; | 8 | }; |
9 | use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner}; | 9 | use ra_syntax::ast::{ |
10 | self, AstNode, AsyncOwner, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, | ||
11 | }; | ||
10 | 12 | ||
11 | use crate::{ | 13 | use crate::{ |
12 | db::DefDatabase, | 14 | db::DefDatabase, |
15 | path::{path, GenericArgs, Path}, | ||
13 | src::HasSource, | 16 | src::HasSource, |
14 | type_ref::{Mutability, TypeRef}, | 17 | type_ref::{Mutability, TypeBound, TypeRef}, |
15 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, | 18 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, |
16 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 19 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
17 | }; | 20 | }; |
@@ -62,11 +65,31 @@ impl FunctionData { | |||
62 | TypeRef::unit() | 65 | TypeRef::unit() |
63 | }; | 66 | }; |
64 | 67 | ||
68 | let ret_type = if src.value.is_async() { | ||
69 | let future_impl = desugar_future_path(ret_type); | ||
70 | let ty_bound = TypeBound::Path(future_impl); | ||
71 | TypeRef::ImplTrait(vec![ty_bound]) | ||
72 | } else { | ||
73 | ret_type | ||
74 | }; | ||
75 | |||
65 | let sig = FunctionData { name, params, ret_type, has_self_param }; | 76 | let sig = FunctionData { name, params, ret_type, has_self_param }; |
66 | Arc::new(sig) | 77 | Arc::new(sig) |
67 | } | 78 | } |
68 | } | 79 | } |
69 | 80 | ||
81 | fn desugar_future_path(orig: TypeRef) -> Path { | ||
82 | let path = path![std::future::Future]; | ||
83 | |||
84 | let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); | ||
85 | |||
86 | let mut last = GenericArgs::empty(); | ||
87 | last.bindings.push((name![Output], orig)); | ||
88 | generic_args.push(Some(Arc::new(last))); | ||
89 | |||
90 | Path::from_known_path(path, generic_args) | ||
91 | } | ||
92 | |||
70 | #[derive(Debug, Clone, PartialEq, Eq)] | 93 | #[derive(Debug, Clone, PartialEq, Eq)] |
71 | pub struct TypeAliasData { | 94 | pub struct TypeAliasData { |
72 | pub name: Name, | 95 | pub name: Name, |
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 8e1294201..bf401df35 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -130,6 +130,11 @@ impl Path { | |||
130 | Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } | 130 | Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } |
131 | } | 131 | } |
132 | 132 | ||
133 | /// Converts a known mod path to `Path`. | ||
134 | pub(crate) fn from_known_path(path: ModPath, generic_args: Vec<Option<Arc<GenericArgs>>>) -> Path { | ||
135 | Path { type_anchor: None, mod_path: path, generic_args } | ||
136 | } | ||
137 | |||
133 | pub fn kind(&self) -> &PathKind { | 138 | pub fn kind(&self) -> &PathKind { |
134 | &self.mod_path.kind | 139 | &self.mod_path.kind |
135 | } | 140 | } |