Skip to content

Commit a204217

Browse files
Make async closures directly lower to ClosureKind::CoroutineClosure
1 parent b2bb517 commit a204217

File tree

12 files changed

+93
-88
lines changed

12 files changed

+93
-88
lines changed

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ ast_lowering_never_pattern_with_guard =
117117
a guard on a never pattern will never be run
118118
.suggestion = remove this guard
119119
120-
ast_lowering_not_supported_for_lifetime_binder_async_closure =
121-
`for<...>` binders on `async` closures are not currently supported
122-
123120
ast_lowering_previously_used_here = previously used here
124121
125122
ast_lowering_register1 = register `{$reg1_name}`

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,6 @@ pub struct MisplacedRelaxTraitBound {
326326
pub span: Span,
327327
}
328328

329-
#[derive(Diagnostic, Clone, Copy)]
330-
#[diag(ast_lowering_not_supported_for_lifetime_binder_async_closure)]
331-
pub struct NotSupportedForLifetimeBinderAsyncClosure {
332-
#[primary_span]
333-
pub span: Span,
334-
}
335-
336329
#[derive(Diagnostic)]
337330
#[diag(ast_lowering_match_arm_with_no_body)]
338331
pub struct MatchArmWithNoBody {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use super::errors::{
22
AsyncCoroutinesNotSupported, AwaitOnlyInAsyncFnAndBlocks, BaseExpressionDoubleDot,
33
ClosureCannotBeStatic, CoroutineTooManyParameters,
44
FunctionalRecordUpdateDestructuringAssignment, InclusiveRangeWithNoEnd, MatchArmWithNoBody,
5-
NeverPatternWithBody, NeverPatternWithGuard, NotSupportedForLifetimeBinderAsyncClosure,
6-
UnderscoreExprLhsAssign,
5+
NeverPatternWithBody, NeverPatternWithGuard, UnderscoreExprLhsAssign,
76
};
87
use super::ResolverAstLoweringExt;
98
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
@@ -1026,30 +1025,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
10261025
fn_decl_span: Span,
10271026
fn_arg_span: Span,
10281027
) -> hir::ExprKind<'hir> {
1029-
if let &ClosureBinder::For { span, .. } = binder {
1030-
self.dcx().emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
1031-
}
1032-
10331028
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
10341029

10351030
let body = self.with_new_scopes(fn_decl_span, |this| {
1031+
let inner_decl =
1032+
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1033+
10361034
// Transform `async |x: u8| -> X { ... }` into
10371035
// `|x: u8| || -> X { ... }`.
10381036
let body_id = this.lower_body(|this| {
1039-
let async_ret_ty = if let FnRetTy::Ty(ty) = &decl.output {
1040-
let itctx = ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock);
1041-
Some(hir::FnRetTy::Return(this.lower_ty(ty, &itctx)))
1042-
} else {
1043-
None
1044-
};
1045-
10461037
let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
1047-
decl,
1038+
&inner_decl,
10481039
|this| this.with_new_scopes(fn_decl_span, |this| this.lower_expr_mut(body)),
10491040
body.span,
10501041
coroutine_kind,
10511042
hir::CoroutineSource::Closure,
1052-
async_ret_ty,
10531043
);
10541044

10551045
let hir_id = this.lower_node_id(coroutine_kind.closure_id());
@@ -1060,15 +1050,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
10601050
body_id
10611051
});
10621052

1063-
let outer_decl =
1064-
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
1065-
10661053
let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
10671054
// We need to lower the declaration outside the new scope, because we
10681055
// have to conserve the state of being inside a loop condition for the
10691056
// closure argument types.
10701057
let fn_decl =
1071-
self.lower_fn_decl(&outer_decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
1058+
self.lower_fn_decl(&decl, closure_id, fn_decl_span, FnDeclKind::Closure, None);
10721059

10731060
let c = self.arena.alloc(hir::Closure {
10741061
def_id: self.local_def_id(closure_id),
@@ -1079,7 +1066,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10791066
body,
10801067
fn_decl_span: self.lower_span(fn_decl_span),
10811068
fn_arg_span: Some(self.lower_span(fn_arg_span)),
1082-
kind: hir::ClosureKind::Closure,
1069+
kind: hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async),
10831070
constness: hir::Constness::NotConst,
10841071
});
10851072
hir::ExprKind::Closure(c)

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
10861086
body.span,
10871087
coroutine_kind,
10881088
hir::CoroutineSource::Fn,
1089-
None,
10901089
);
10911090

10921091
// FIXME(async_fn_track_caller): Can this be moved above?
@@ -1108,7 +1107,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
11081107
body_span: Span,
11091108
coroutine_kind: CoroutineKind,
11101109
coroutine_source: hir::CoroutineSource,
1111-
return_type_hint: Option<hir::FnRetTy<'hir>>,
11121110
) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>) {
11131111
let mut parameters: Vec<hir::Param<'_>> = Vec::new();
11141112
let mut statements: Vec<hir::Stmt<'_>> = Vec::new();
@@ -1284,7 +1282,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12841282
// all async closures would default to `FnOnce` as their calling mode.
12851283
CaptureBy::Ref,
12861284
closure_id,
1287-
return_type_hint,
1285+
None,
12881286
body_span,
12891287
desugaring_kind,
12901288
coroutine_source,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![allow(internal_features)]
3434
#![feature(rustdoc_internals)]
3535
#![doc(rust_logo)]
36+
#![feature(assert_matches)]
3637
#![feature(box_patterns)]
3738
#![feature(let_chains)]
3839
#![deny(rustc::untranslatable_diagnostic)]
@@ -296,7 +297,6 @@ enum ImplTraitPosition {
296297
Path,
297298
Variable,
298299
Trait,
299-
AsyncBlock,
300300
Bound,
301301
Generic,
302302
ExternFnParam,
@@ -323,7 +323,6 @@ impl std::fmt::Display for ImplTraitPosition {
323323
ImplTraitPosition::Path => "paths",
324324
ImplTraitPosition::Variable => "the type of variable bindings",
325325
ImplTraitPosition::Trait => "traits",
326-
ImplTraitPosition::AsyncBlock => "async blocks",
327326
ImplTraitPosition::Bound => "bounds",
328327
ImplTraitPosition::Generic => "generics",
329328
ImplTraitPosition::ExternFnParam => "`extern fn` parameters",

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
858858
use crate::session_diagnostics::CaptureVarCause::*;
859859
match kind {
860860
hir::ClosureKind::Coroutine(_) => MoveUseInCoroutine { var_span },
861-
hir::ClosureKind::Closure => MoveUseInClosure { var_span },
861+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
862+
MoveUseInClosure { var_span }
863+
}
862864
}
863865
});
864866

@@ -905,7 +907,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
905907
hir::ClosureKind::Coroutine(_) => {
906908
BorrowUsePlaceCoroutine { place: desc_place, var_span, is_single_var: true }
907909
}
908-
hir::ClosureKind::Closure => {
910+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
909911
BorrowUsePlaceClosure { place: desc_place, var_span, is_single_var: true }
910912
}
911913
}
@@ -1056,7 +1058,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10561058
var_span,
10571059
is_single_var: true,
10581060
},
1059-
hir::ClosureKind::Closure => BorrowUsePlaceClosure {
1061+
hir::ClosureKind::Closure
1062+
| hir::ClosureKind::CoroutineClosure(_) => BorrowUsePlaceClosure {
10601063
place: desc_place,
10611064
var_span,
10621065
is_single_var: true,
@@ -1140,7 +1143,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11401143
var_span,
11411144
is_single_var: false,
11421145
},
1143-
hir::ClosureKind::Closure => {
1146+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
11441147
BorrowUsePlaceClosure { place: desc_place, var_span, is_single_var: false }
11451148
}
11461149
}
@@ -1158,7 +1161,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11581161
hir::ClosureKind::Coroutine(_) => {
11591162
FirstBorrowUsePlaceCoroutine { place: borrow_place_desc, var_span }
11601163
}
1161-
hir::ClosureKind::Closure => {
1164+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
11621165
FirstBorrowUsePlaceClosure { place: borrow_place_desc, var_span }
11631166
}
11641167
}
@@ -1175,7 +1178,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11751178
hir::ClosureKind::Coroutine(_) => {
11761179
SecondBorrowUsePlaceCoroutine { place: desc_place, var_span }
11771180
}
1178-
hir::ClosureKind::Closure => {
1181+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
11791182
SecondBorrowUsePlaceClosure { place: desc_place, var_span }
11801183
}
11811184
}
@@ -2942,7 +2945,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
29422945
use crate::session_diagnostics::CaptureVarCause::*;
29432946
match kind {
29442947
hir::ClosureKind::Coroutine(_) => BorrowUseInCoroutine { var_span },
2945-
hir::ClosureKind::Closure => BorrowUseInClosure { var_span },
2948+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
2949+
BorrowUseInClosure { var_span }
2950+
}
29462951
}
29472952
});
29482953

@@ -2958,7 +2963,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
29582963
use crate::session_diagnostics::CaptureVarCause::*;
29592964
match kind {
29602965
hir::ClosureKind::Coroutine(_) => BorrowUseInCoroutine { var_span },
2961-
hir::ClosureKind::Closure => BorrowUseInClosure { var_span },
2966+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
2967+
BorrowUseInClosure { var_span }
2968+
}
29622969
}
29632970
});
29642971

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl UseSpans<'_> {
614614
PartialAssignment => AssignPartInCoroutine { path_span },
615615
});
616616
}
617-
hir::ClosureKind::Closure => {
617+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
618618
err.subdiagnostic(match action {
619619
Borrow => BorrowInClosure { path_span },
620620
MatchOn | Use => UseInClosure { path_span },
@@ -1253,7 +1253,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12531253
hir::ClosureKind::Coroutine(_) => {
12541254
CaptureVarCause::PartialMoveUseInCoroutine { var_span, is_partial }
12551255
}
1256-
hir::ClosureKind::Closure => {
1256+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
12571257
CaptureVarCause::PartialMoveUseInClosure { var_span, is_partial }
12581258
}
12591259
})

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
692692
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
693693
hir::CoroutineDesugaring::Async,
694694
hir::CoroutineSource::Closure,
695-
)) => " of async closure",
695+
))
696+
| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => {
697+
" of async closure"
698+
}
696699

697700
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
698701
hir::CoroutineDesugaring::Async,
@@ -719,7 +722,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
719722
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
720723
hir::CoroutineDesugaring::Gen,
721724
hir::CoroutineSource::Closure,
722-
)) => " of gen closure",
725+
))
726+
| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
727+
" of gen closure"
728+
}
723729

724730
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
725731
hir::CoroutineDesugaring::Gen,
@@ -743,7 +749,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
743749
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
744750
hir::CoroutineDesugaring::AsyncGen,
745751
hir::CoroutineSource::Closure,
746-
)) => " of async gen closure",
752+
))
753+
| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen) => {
754+
" of async gen closure"
755+
}
747756

748757
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
749758
hir::CoroutineDesugaring::AsyncGen,

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,11 @@ pub enum ClosureKind {
952952
/// usage (e.g. `let x = || { yield (); }`) or from a desugared expression
953953
/// (e.g. `async` and `gen` blocks).
954954
Coroutine(CoroutineKind),
955+
/// This is a coroutine-closure, which is a special sugared closure that
956+
/// returns one of the sugared coroutine (`async`/`gen`/`async gen`). It
957+
/// additionally allows capturing the coroutine's upvars by ref, and therefore
958+
/// needs to be specially treated during analysis and borrowck.
959+
CoroutineClosure(CoroutineDesugaring),
955960
}
956961

957962
/// A block of statements `{ .. }`, which may have a label (in this case the

compiler/rustc_hir_analysis/src/collect/generics_of.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
349349
ClosureKind::Coroutine(_) => {
350350
&["<resume_ty>", "<yield_ty>", "<return_ty>", "<witness>", "<upvars>"][..]
351351
}
352+
ClosureKind::CoroutineClosure(_) => todo!(),
352353
};
353354

354355
params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
192192
Some(CoroutineTypes { resume_ty, yield_ty }),
193193
)
194194
}
195+
hir::ClosureKind::CoroutineClosure(_) => todo!(),
195196
};
196197

197198
check_fn(
@@ -690,7 +691,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
690691
_,
691692
))
692693
| hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_))
693-
| hir::ClosureKind::Closure => astconv.ty_infer(None, decl.output.span()),
694+
| hir::ClosureKind::Closure
695+
| hir::ClosureKind::CoroutineClosure(_) => {
696+
astconv.ty_infer(None, decl.output.span())
697+
}
694698
},
695699
};
696700

0 commit comments

Comments
 (0)