1
Fork 0

Rollup merge of #104612 - Swatinem:async-ret-y, r=estebank

Lower return type outside async block creation

This allows feeding a different output type to async blocks with a different `ImplTraitContext`. Spotted this while working on #104321
This commit is contained in:
Manish Goregaokar 2022-11-22 22:54:39 -05:00 committed by GitHub
commit 36815c6e3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -588,17 +588,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
&mut self, &mut self,
capture_clause: CaptureBy, capture_clause: CaptureBy,
closure_node_id: NodeId, closure_node_id: NodeId,
ret_ty: Option<AstP<Ty>>, ret_ty: Option<hir::FnRetTy<'hir>>,
span: Span, span: Span,
async_gen_kind: hir::AsyncGeneratorKind, async_gen_kind: hir::AsyncGeneratorKind,
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
) -> hir::ExprKind<'hir> { ) -> hir::ExprKind<'hir> {
let output = match ret_ty { let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
Some(ty) => hir::FnRetTy::Return(
self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)),
),
None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
};
// Resume argument type. We let the compiler infer this to simplify the lowering. It is // Resume argument type. We let the compiler infer this to simplify the lowering. It is
// fully constrained by `future::from_generator`. // fully constrained by `future::from_generator`.
@ -1003,8 +998,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Transform `async |x: u8| -> X { ... }` into // Transform `async |x: u8| -> X { ... }` into
// `|x: u8| future_from_generator(|| -> X { ... })`. // `|x: u8| future_from_generator(|| -> X { ... })`.
let body_id = this.lower_fn_body(&outer_decl, |this| { let body_id = this.lower_fn_body(&outer_decl, |this| {
let async_ret_ty = let async_ret_ty = if let FnRetTy::Ty(ty) = &decl.output {
if let FnRetTy::Ty(ty) = &decl.output { Some(ty.clone()) } else { None }; let itctx = ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock);
Some(hir::FnRetTy::Return(this.lower_ty(&ty, &itctx)))
} else {
None
};
let async_body = this.make_async_expr( let async_body = this.make_async_expr(
capture_clause, capture_clause,
inner_closure_id, inner_closure_id,