diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-24 16:28:46 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-24 16:28:46 +0000 |
commit | 3f7e5cde0b80b19095bf7e4d40f88d418a3c5921 (patch) | |
tree | 03339444fa1f56b6549cca23822d119ee95f5d31 /crates/ra_hir_def | |
parent | aa49b79bda5b7cafbaa33c302a9974133d34c52b (diff) | |
parent | 208ad97fdc9427f1243ac170c1c25f9f7d6ae964 (diff) |
Merge #2661
2661: Implement infer await from async function r=flodiebold a=edwin0cheng
This PR is my attempt for trying to add support for infer `.await` expression from an `async` function, by desugaring its return type to `Impl Future<Output=RetType>`.
Note that I don't know it is supposed to desugaring it in that phase, if it is not suitable in current design, just feel free to reject it :)
r=@flodiebold
Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 8 |
2 files changed, 28 insertions, 1 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 1aa9a9b7d..c900a6a18 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -10,8 +10,9 @@ use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAs | |||
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | db::DefDatabase, | 12 | db::DefDatabase, |
13 | path::{path, GenericArgs, Path}, | ||
13 | src::HasSource, | 14 | src::HasSource, |
14 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeBound, TypeRef}, |
15 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, | 16 | AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, |
16 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, | 17 | ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, |
17 | }; | 18 | }; |
@@ -62,11 +63,29 @@ impl FunctionData { | |||
62 | TypeRef::unit() | 63 | TypeRef::unit() |
63 | }; | 64 | }; |
64 | 65 | ||
66 | let ret_type = if src.value.is_async() { | ||
67 | let future_impl = desugar_future_path(ret_type); | ||
68 | let ty_bound = TypeBound::Path(future_impl); | ||
69 | TypeRef::ImplTrait(vec![ty_bound]) | ||
70 | } else { | ||
71 | ret_type | ||
72 | }; | ||
73 | |||
65 | let sig = FunctionData { name, params, ret_type, has_self_param }; | 74 | let sig = FunctionData { name, params, ret_type, has_self_param }; |
66 | Arc::new(sig) | 75 | Arc::new(sig) |
67 | } | 76 | } |
68 | } | 77 | } |
69 | 78 | ||
79 | fn desugar_future_path(orig: TypeRef) -> Path { | ||
80 | let path = path![std::future::Future]; | ||
81 | let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); | ||
82 | let mut last = GenericArgs::empty(); | ||
83 | last.bindings.push((name![Output], orig)); | ||
84 | generic_args.push(Some(Arc::new(last))); | ||
85 | |||
86 | Path::from_known_path(path, generic_args) | ||
87 | } | ||
88 | |||
70 | #[derive(Debug, Clone, PartialEq, Eq)] | 89 | #[derive(Debug, Clone, PartialEq, Eq)] |
71 | pub struct TypeAliasData { | 90 | pub struct TypeAliasData { |
72 | pub name: Name, | 91 | pub name: Name, |
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 8e1294201..107d2d799 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -130,6 +130,14 @@ 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( | ||
135 | path: ModPath, | ||
136 | generic_args: Vec<Option<Arc<GenericArgs>>>, | ||
137 | ) -> Path { | ||
138 | Path { type_anchor: None, mod_path: path, generic_args } | ||
139 | } | ||
140 | |||
133 | pub fn kind(&self) -> &PathKind { | 141 | pub fn kind(&self) -> &PathKind { |
134 | &self.mod_path.kind | 142 | &self.mod_path.kind |
135 | } | 143 | } |