From 54d60fdee9a49e2081837cc6445c61dec0b303af Mon Sep 17 00:00:00 2001 From: cynecx Date: Sat, 29 May 2021 18:16:20 +0200 Subject: hir_ty: use async ret type for inference inside async bodies --- crates/hir_ty/src/infer.rs | 8 ++++++- crates/hir_ty/src/tests/traits.rs | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 8cefd80f3..7a4268819 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -558,7 +558,13 @@ impl<'a> InferenceContext<'a> { self.infer_pat(*pat, &ty, BindingMode::default()); } - let return_ty = self.make_ty_with_mode(&data.ret_type, ImplTraitLoweringMode::Disallowed); // FIXME implement RPIT + let error_ty = &TypeRef::Error; + let return_ty = if data.is_async() { + data.async_ret_type.as_deref().unwrap_or(error_ty) + } else { + &*data.ret_type + }; + let return_ty = self.make_ty_with_mode(return_ty, ImplTraitLoweringMode::Disallowed); // FIXME implement RPIT self.return_ty = return_ty; } diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 6ad96bfe3..7c0ff2170 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -3660,3 +3660,52 @@ impl foo::Foo for u32 { "#]], ); } + +#[test] +fn infer_async_ret_type() { + check_types( + r#" +//- /main.rs crate:main deps:core + +enum Result { + Ok(T), + Err(E), +} + +use Result::*; + + +struct Fooey; + +impl Fooey { + fn collect(self) -> B { + B::new() + } +} + +trait Convert { + fn new() -> Self; +} +impl Convert for u32 { + fn new() -> Self { + 0 + } +} + +async fn get_accounts() -> Result { + let ret = Fooey.collect(); + // ^ u32 + Ok(ret) +} + +//- /core.rs crate:core +#[prelude_import] use future::*; +mod future { + #[lang = "future_trait"] + trait Future { + type Output; + } +} +"#, + ); +} -- cgit v1.2.3