From 0edb5b4a501a66baa7db8ececf46135e6246f4de Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 24 Dec 2019 19:45:28 +0800 Subject: Implement infer await from async func --- crates/ra_hir_def/src/data.rs | 27 +++++++++++++++++++++++++-- crates/ra_hir_def/src/path.rs | 5 +++++ 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def') 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::{ name::{name, AsName, Name}, AstId, InFile, }; -use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner}; +use ra_syntax::ast::{ + self, AstNode, AsyncOwner, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, +}; use crate::{ db::DefDatabase, + path::{path, GenericArgs, Path}, src::HasSource, - type_ref::{Mutability, TypeRef}, + type_ref::{Mutability, TypeBound, TypeRef}, AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; @@ -62,11 +65,31 @@ impl FunctionData { TypeRef::unit() }; + let ret_type = if src.value.is_async() { + let future_impl = desugar_future_path(ret_type); + let ty_bound = TypeBound::Path(future_impl); + TypeRef::ImplTrait(vec![ty_bound]) + } else { + ret_type + }; + let sig = FunctionData { name, params, ret_type, has_self_param }; Arc::new(sig) } } +fn desugar_future_path(orig: TypeRef) -> Path { + let path = path![std::future::Future]; + + let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); + + let mut last = GenericArgs::empty(); + last.bindings.push((name![Output], orig)); + generic_args.push(Some(Arc::new(last))); + + Path::from_known_path(path, generic_args) +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct TypeAliasData { 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 { Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } } + /// Converts a known mod path to `Path`. + pub(crate) fn from_known_path(path: ModPath, generic_args: Vec>>) -> Path { + Path { type_anchor: None, mod_path: path, generic_args } + } + pub fn kind(&self) -> &PathKind { &self.mod_path.kind } -- cgit v1.2.3