Skip to content

Commit 251ef93

Browse files
committed
Implement async blocks
1 parent 0275b08 commit 251ef93

File tree

11 files changed

+248
-55
lines changed

11 files changed

+248
-55
lines changed

crates/hir/src/code_model.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,8 @@ impl Type {
12831283
/// Checks that particular type `ty` implements `std::future::Future`.
12841284
/// This function is used in `.await` syntax completion.
12851285
pub fn impls_future(&self, db: &dyn HirDatabase) -> bool {
1286+
// No special case for the type of async block, since Chalk can figure it out.
1287+
12861288
let krate = self.krate;
12871289

12881290
let std_future_trait =

crates/hir_def/src/body/lower.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ impl ExprCollector<'_> {
239239
None => self.missing_expr(),
240240
},
241241
// FIXME: we need to record these effects somewhere...
242-
ast::Effect::Async(_) => self.collect_block_opt(e.block_expr()),
242+
ast::Effect::Async(_) => {
243+
let body = self.collect_block_opt(e.block_expr());
244+
self.alloc_expr(Expr::Async { body }, syntax_ptr)
245+
}
243246
},
244247
ast::Expr::BlockExpr(e) => self.collect_block(e),
245248
ast::Expr::LoopExpr(e) => {

crates/hir_def/src/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ pub enum Expr {
111111
TryBlock {
112112
body: ExprId,
113113
},
114+
Async {
115+
body: ExprId,
116+
},
114117
Cast {
115118
expr: ExprId,
116119
type_ref: TypeRef,
@@ -250,7 +253,7 @@ impl Expr {
250253
f(*expr);
251254
}
252255
}
253-
Expr::TryBlock { body } | Expr::Unsafe { body } => f(*body),
256+
Expr::TryBlock { body } | Expr::Unsafe { body } | Expr::Async { body } => f(*body),
254257
Expr::Loop { body, .. } => f(*body),
255258
Expr::While { condition, body, .. } => {
256259
f(*condition);

crates/hir_ty/src/display.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,24 @@ impl HirDisplay for ApplicationTy {
381381
}
382382
}
383383
TypeCtor::OpaqueType(opaque_ty_id) => {
384-
let bounds = match opaque_ty_id {
384+
match opaque_ty_id {
385385
OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
386386
let datas =
387387
f.db.return_type_impl_traits(func).expect("impl trait id without data");
388388
let data = (*datas)
389389
.as_ref()
390390
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
391-
data.subst(&self.parameters)
391+
let bounds = data.subst(&self.parameters);
392+
write!(f, "impl ")?;
393+
write_bounds_like_dyn_trait(&bounds.value, f)?;
394+
// FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
392395
}
393-
};
394-
write!(f, "impl ")?;
395-
write_bounds_like_dyn_trait(&bounds.value, f)?;
396-
// FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
396+
OpaqueTyId::AsyncBlockTypeImplTrait(..) => {
397+
write!(f, "impl Future<Output = ")?;
398+
self.parameters[0].hir_fmt(f)?;
399+
write!(f, ">")?;
400+
}
401+
}
397402
}
398403
TypeCtor::Closure { .. } => {
399404
let sig = self.parameters[0].callable_sig(f.db);
@@ -474,18 +479,21 @@ impl HirDisplay for Ty {
474479
write_bounds_like_dyn_trait(predicates, f)?;
475480
}
476481
Ty::Opaque(opaque_ty) => {
477-
let bounds = match opaque_ty.opaque_ty_id {
482+
match opaque_ty.opaque_ty_id {
478483
OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
479484
let datas =
480485
f.db.return_type_impl_traits(func).expect("impl trait id without data");
481486
let data = (*datas)
482487
.as_ref()
483488
.map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
484-
data.subst(&opaque_ty.parameters)
489+
let bounds = data.subst(&opaque_ty.parameters);
490+
write!(f, "impl ")?;
491+
write_bounds_like_dyn_trait(&bounds.value, f)?;
492+
}
493+
OpaqueTyId::AsyncBlockTypeImplTrait(..) => {
494+
write!(f, "{{async block}}")?;
485495
}
486496
};
487-
write!(f, "impl ")?;
488-
write_bounds_like_dyn_trait(&bounds.value, f)?;
489497
}
490498
Ty::Unknown => write!(f, "{{unknown}}")?,
491499
Ty::Infer(..) => write!(f, "_")?,

crates/hir_ty/src/infer/expr.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::{
1717
autoderef, method_resolution, op,
1818
traits::{FnTrait, InEnvironment},
1919
utils::{generics, variant_data, Generics},
20-
ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, Rawness, Substs,
21-
TraitRef, Ty, TypeCtor,
20+
ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, OpaqueTyId,
21+
Rawness, Substs, TraitRef, Ty, TypeCtor,
2222
};
2323

2424
use super::{
@@ -146,6 +146,13 @@ impl<'a> InferenceContext<'a> {
146146
// FIXME should be std::result::Result<{inner}, _>
147147
Ty::Unknown
148148
}
149+
Expr::Async { body } => {
150+
// Use the first type parameter as the output type of future.
151+
// existenail type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
152+
let inner_ty = self.infer_expr(*body, &Expectation::none());
153+
let opaque_ty_id = OpaqueTyId::AsyncBlockTypeImplTrait(self.owner, *body);
154+
Ty::apply_one(TypeCtor::OpaqueType(opaque_ty_id), inner_ty)
155+
}
149156
Expr::Loop { body, label } => {
150157
self.breakables.push(BreakableContext {
151158
may_break: false,

crates/hir_ty/src/lib.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use hir_def::{
3333
AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId,
3434
TypeParamId,
3535
};
36+
use hir_expand::name::name;
3637
use itertools::Itertools;
3738

3839
use crate::{
@@ -129,8 +130,9 @@ pub enum TypeCtor {
129130

130131
/// This represents a placeholder for an opaque type in situations where we
131132
/// don't know the hidden type (i.e. currently almost always). This is
132-
/// analogous to the `AssociatedType` type constructor. As with that one,
133-
/// these are only produced by Chalk.
133+
/// analogous to the `AssociatedType` type constructor.
134+
/// It is also used as the type of async block, with one type parameter
135+
/// representing the Future::Output type.
134136
OpaqueType(OpaqueTyId),
135137

136138
/// The type of a specific closure.
@@ -173,6 +175,8 @@ impl TypeCtor {
173175
let generic_params = generics(db.upcast(), func.into());
174176
generic_params.len()
175177
}
178+
// 1 param representing Future::Output type.
179+
OpaqueTyId::AsyncBlockTypeImplTrait(..) => 1,
176180
}
177181
}
178182
TypeCtor::FnPtr { num_args, is_varargs: _ } => num_args as usize + 1,
@@ -205,6 +209,7 @@ impl TypeCtor {
205209
OpaqueTyId::ReturnTypeImplTrait(func, _) => {
206210
Some(func.lookup(db.upcast()).module(db.upcast()).krate)
207211
}
212+
OpaqueTyId::AsyncBlockTypeImplTrait(def, _) => Some(def.module(db.upcast()).krate),
208213
},
209214
}
210215
}
@@ -843,6 +848,33 @@ impl Ty {
843848

844849
pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> {
845850
match self {
851+
Ty::Apply(ApplicationTy { ctor: TypeCtor::OpaqueType(opaque_ty_id), parameters }) => {
852+
match opaque_ty_id {
853+
OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => {
854+
let krate = def.module(db.upcast()).krate;
855+
if let Some(future_output) = db
856+
.lang_item(krate, "future_trait".into())
857+
.and_then(|item| item.as_trait())
858+
.and_then(|trait_| {
859+
db.trait_data(trait_).associated_type_by_name(&name![Output])
860+
})
861+
{
862+
let proj = GenericPredicate::Projection(ProjectionPredicate {
863+
projection_ty: ProjectionTy {
864+
associated_ty: future_output,
865+
// Self type.
866+
parameters: Substs::single(self.clone()),
867+
},
868+
ty: parameters[0].clone(),
869+
});
870+
Some(vec![proj])
871+
} else {
872+
None
873+
}
874+
}
875+
OpaqueTyId::ReturnTypeImplTrait(..) => None,
876+
}
877+
}
846878
Ty::Opaque(opaque_ty) => {
847879
let predicates = match opaque_ty.opaque_ty_id {
848880
OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
@@ -853,6 +885,8 @@ impl Ty {
853885
data.subst(&opaque_ty.parameters)
854886
})
855887
}
888+
// It always has an parameter for Future::Output type.
889+
OpaqueTyId::AsyncBlockTypeImplTrait(..) => unreachable!(),
856890
};
857891

858892
predicates.map(|it| it.value)
@@ -1065,6 +1099,7 @@ impl<T: TypeWalk> TypeWalk for Vec<T> {
10651099
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
10661100
pub enum OpaqueTyId {
10671101
ReturnTypeImplTrait(hir_def::FunctionId, u16),
1102+
AsyncBlockTypeImplTrait(hir_def::DefWithBodyId, ExprId),
10681103
}
10691104

10701105
#[derive(Clone, PartialEq, Eq, Debug, Hash)]

crates/hir_ty/src/tests/simple.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,31 +1889,40 @@ fn fn_pointer_return() {
18891889
fn effects_smoke_test() {
18901890
check_infer(
18911891
r#"
1892-
fn main() {
1892+
async fn main() {
18931893
let x = unsafe { 92 };
18941894
let y = async { async { () }.await };
18951895
let z = try { () };
18961896
let t = 'a: { 92 };
18971897
}
1898+
1899+
#[prelude_import] use future::*;
1900+
1901+
mod future {
1902+
#[lang = "future_trait"]
1903+
pub trait Future { type Output; }
1904+
}
18981905
"#,
18991906
expect![[r#"
1900-
10..130 '{ ...2 }; }': ()
1901-
20..21 'x': i32
1902-
24..37 'unsafe { 92 }': i32
1903-
31..37 '{ 92 }': i32
1904-
33..35 '92': i32
1905-
47..48 'y': {unknown}
1906-
57..79 '{ asyn...wait }': {unknown}
1907-
59..77 'async ....await': {unknown}
1908-
65..71 '{ () }': ()
1909-
67..69 '()': ()
1910-
89..90 'z': {unknown}
1911-
93..103 'try { () }': {unknown}
1912-
97..103 '{ () }': ()
1913-
99..101 '()': ()
1914-
113..114 't': i32
1915-
121..127 '{ 92 }': i32
1916-
123..125 '92': i32
1907+
16..136 '{ ...2 }; }': ()
1908+
26..27 'x': i32
1909+
30..43 'unsafe { 92 }': i32
1910+
37..43 '{ 92 }': i32
1911+
39..41 '92': i32
1912+
53..54 'y': impl Future<Output = ()>
1913+
57..85 'async ...wait }': impl Future<Output = ()>
1914+
63..85 '{ asyn...wait }': ()
1915+
65..77 'async { () }': impl Future<Output = ()>
1916+
65..83 'async ....await': ()
1917+
71..77 '{ () }': ()
1918+
73..75 '()': ()
1919+
95..96 'z': {unknown}
1920+
99..109 'try { () }': {unknown}
1921+
103..109 '{ () }': ()
1922+
105..107 '()': ()
1923+
119..120 't': i32
1924+
127..133 '{ 92 }': i32
1925+
129..131 '92': i32
19171926
"#]],
19181927
)
19191928
}

crates/hir_ty/src/tests/traits.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,46 @@ mod future {
8585
);
8686
}
8787

88+
#[test]
89+
fn infer_async_block() {
90+
check_types(
91+
r#"
92+
//- /main.rs crate:main deps:core
93+
async fn test() {
94+
let a = async { 42 };
95+
a;
96+
// ^ impl Future<Output = i32>
97+
let x = a.await;
98+
x;
99+
// ^ i32
100+
let b = async {}.await;
101+
b;
102+
// ^ ()
103+
let c = async {
104+
let y = Option::None;
105+
y
106+
// ^ Option<u64>
107+
};
108+
let _: Option<u64> = c.await;
109+
c;
110+
// ^ impl Future<Output = Option<u64>>
111+
}
112+
113+
enum Option<T> { None, Some(T) }
114+
115+
//- /core.rs crate:core
116+
#[prelude_import] use future::*;
117+
mod future {
118+
#[lang = "future_trait"]
119+
trait Future {
120+
type Output;
121+
}
122+
}
123+
124+
"#,
125+
);
126+
}
127+
88128
#[test]
89129
fn infer_try() {
90130
check_types(

0 commit comments

Comments
 (0)