Skip to content

Commit ae0a6e8

Browse files
Rollup merge of rust-lang#119198 - compiler-errors:desugaring, r=eholk
Split coroutine desugaring kind from source What a coroutine is desugared from (gen/async gen/async) should be separate from where it comes (fn/block/closure).
2 parents e0d7a72 + 0044505 commit ae0a6e8

File tree

30 files changed

+448
-239
lines changed

30 files changed

+448
-239
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
670670
let params = arena_vec![self; param];
671671

672672
let body = self.lower_body(move |this| {
673-
this.coroutine_kind = Some(hir::CoroutineKind::Async(async_coroutine_source));
673+
this.coroutine_kind = Some(hir::CoroutineKind::Desugared(
674+
hir::CoroutineDesugaring::Async,
675+
async_coroutine_source,
676+
));
674677

675678
let old_ctx = this.task_context;
676679
this.task_context = Some(task_context_hid);
@@ -724,7 +727,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
724727
});
725728

726729
let body = self.lower_body(move |this| {
727-
this.coroutine_kind = Some(hir::CoroutineKind::Gen(coroutine_source));
730+
this.coroutine_kind = Some(hir::CoroutineKind::Desugared(
731+
hir::CoroutineDesugaring::Gen,
732+
coroutine_source,
733+
));
728734

729735
let res = body(this);
730736
(&[], res)
@@ -802,7 +808,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
802808
let params = arena_vec![self; param];
803809

804810
let body = self.lower_body(move |this| {
805-
this.coroutine_kind = Some(hir::CoroutineKind::AsyncGen(async_coroutine_source));
811+
this.coroutine_kind = Some(hir::CoroutineKind::Desugared(
812+
hir::CoroutineDesugaring::AsyncGen,
813+
async_coroutine_source,
814+
));
806815

807816
let old_ctx = this.task_context;
808817
this.task_context = Some(task_context_hid);
@@ -888,9 +897,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
888897
let full_span = expr.span.to(await_kw_span);
889898

890899
let is_async_gen = match self.coroutine_kind {
891-
Some(hir::CoroutineKind::Async(_)) => false,
892-
Some(hir::CoroutineKind::AsyncGen(_)) => true,
893-
Some(hir::CoroutineKind::Coroutine) | Some(hir::CoroutineKind::Gen(_)) | None => {
900+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => false,
901+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
902+
Some(hir::CoroutineKind::Coroutine)
903+
| Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _))
904+
| None => {
894905
return hir::ExprKind::Err(self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
895906
await_kw_span,
896907
item_span: self.current_item,
@@ -1123,9 +1134,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
11231134
Some(movability)
11241135
}
11251136
Some(
1126-
hir::CoroutineKind::Gen(_)
1127-
| hir::CoroutineKind::Async(_)
1128-
| hir::CoroutineKind::AsyncGen(_),
1137+
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
1138+
| hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)
1139+
| hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _),
11291140
) => {
11301141
panic!("non-`async`/`gen` closure body turned `async`/`gen` during lowering");
11311142
}
@@ -1638,9 +1649,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
16381649

16391650
fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
16401651
let is_async_gen = match self.coroutine_kind {
1641-
Some(hir::CoroutineKind::Gen(_)) => false,
1642-
Some(hir::CoroutineKind::AsyncGen(_)) => true,
1643-
Some(hir::CoroutineKind::Async(_)) => {
1652+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => false,
1653+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
1654+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
16441655
return hir::ExprKind::Err(
16451656
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }),
16461657
);

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use either::Either;
2-
use hir::PatField;
32
use rustc_data_structures::captures::Captures;
43
use rustc_data_structures::fx::FxIndexSet;
54
use rustc_errors::{struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan};
65
use rustc_hir as hir;
76
use rustc_hir::def::{DefKind, Res};
87
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
8+
use rustc_hir::{CoroutineDesugaring, PatField};
99
use rustc_hir::{CoroutineKind, CoroutineSource, LangItem};
1010
use rustc_infer::traits::ObligationCause;
1111
use rustc_middle::hir::nested_filter::OnlyBodies;
@@ -2514,27 +2514,29 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25142514
};
25152515
let kind = match use_span.coroutine_kind() {
25162516
Some(coroutine_kind) => match coroutine_kind {
2517-
CoroutineKind::Gen(kind) => match kind {
2517+
CoroutineKind::Desugared(CoroutineDesugaring::Gen, kind) => match kind {
25182518
CoroutineSource::Block => "gen block",
25192519
CoroutineSource::Closure => "gen closure",
25202520
CoroutineSource::Fn => {
25212521
bug!("gen block/closure expected, but gen function found.")
25222522
}
25232523
},
2524-
CoroutineKind::AsyncGen(kind) => match kind {
2524+
CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, kind) => match kind {
25252525
CoroutineSource::Block => "async gen block",
25262526
CoroutineSource::Closure => "async gen closure",
25272527
CoroutineSource::Fn => {
25282528
bug!("gen block/closure expected, but gen function found.")
25292529
}
25302530
},
2531-
CoroutineKind::Async(async_kind) => match async_kind {
2532-
CoroutineSource::Block => "async block",
2533-
CoroutineSource::Closure => "async closure",
2534-
CoroutineSource::Fn => {
2535-
bug!("async block/closure expected, but async function found.")
2531+
CoroutineKind::Desugared(CoroutineDesugaring::Async, async_kind) => {
2532+
match async_kind {
2533+
CoroutineSource::Block => "async block",
2534+
CoroutineSource::Closure => "async closure",
2535+
CoroutineSource::Fn => {
2536+
bug!("async block/closure expected, but async function found.")
2537+
}
25362538
}
2537-
},
2539+
}
25382540
CoroutineKind::Coroutine => "coroutine",
25392541
},
25402542
None => "closure",
@@ -2564,7 +2566,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25642566
}
25652567
ConstraintCategory::CallArgument(_) => {
25662568
fr_name.highlight_region_name(&mut err);
2567-
if matches!(use_span.coroutine_kind(), Some(CoroutineKind::Async(_))) {
2569+
if matches!(
2570+
use_span.coroutine_kind(),
2571+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Async, _))
2572+
) {
25682573
err.note(
25692574
"async blocks are not executed immediately and must either take a \
25702575
reference or ownership of outside variables they use",

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10461046
..
10471047
}) => {
10481048
let body = map.body(*body);
1049-
if !matches!(body.coroutine_kind, Some(hir::CoroutineKind::Async(..))) {
1049+
if !matches!(
1050+
body.coroutine_kind,
1051+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _))
1052+
) {
10501053
closure_span = Some(expr.span.shrink_to_lo());
10511054
}
10521055
}

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+36-29
Original file line numberDiff line numberDiff line change
@@ -684,39 +684,46 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
684684
hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)),
685685
};
686686
let mir_description = match hir.body(body).coroutine_kind {
687-
Some(hir::CoroutineKind::Async(src)) => match src {
688-
hir::CoroutineSource::Block => " of async block",
689-
hir::CoroutineSource::Closure => " of async closure",
690-
hir::CoroutineSource::Fn => {
691-
let parent_item =
692-
tcx.hir_node_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
693-
let output = &parent_item
694-
.fn_decl()
695-
.expect("coroutine lowered from async fn should be in fn")
696-
.output;
697-
span = output.span();
698-
if let hir::FnRetTy::Return(ret) = output {
699-
hir_ty = Some(self.get_future_inner_return_ty(*ret));
687+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, src)) => {
688+
match src {
689+
hir::CoroutineSource::Block => " of async block",
690+
hir::CoroutineSource::Closure => " of async closure",
691+
hir::CoroutineSource::Fn => {
692+
let parent_item =
693+
tcx.hir_node_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
694+
let output = &parent_item
695+
.fn_decl()
696+
.expect("coroutine lowered from async fn should be in fn")
697+
.output;
698+
span = output.span();
699+
if let hir::FnRetTy::Return(ret) = output {
700+
hir_ty = Some(self.get_future_inner_return_ty(*ret));
701+
}
702+
" of async function"
700703
}
701-
" of async function"
702704
}
703-
},
704-
Some(hir::CoroutineKind::Gen(src)) => match src {
705-
hir::CoroutineSource::Block => " of gen block",
706-
hir::CoroutineSource::Closure => " of gen closure",
707-
hir::CoroutineSource::Fn => {
708-
let parent_item =
709-
tcx.hir_node_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
710-
let output = &parent_item
711-
.fn_decl()
712-
.expect("coroutine lowered from gen fn should be in fn")
713-
.output;
714-
span = output.span();
715-
" of gen function"
705+
}
706+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, src)) => {
707+
match src {
708+
hir::CoroutineSource::Block => " of gen block",
709+
hir::CoroutineSource::Closure => " of gen closure",
710+
hir::CoroutineSource::Fn => {
711+
let parent_item =
712+
tcx.hir_node_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
713+
let output = &parent_item
714+
.fn_decl()
715+
.expect("coroutine lowered from gen fn should be in fn")
716+
.output;
717+
span = output.span();
718+
" of gen function"
719+
}
716720
}
717-
},
721+
}
718722

719-
Some(hir::CoroutineKind::AsyncGen(src)) => match src {
723+
Some(hir::CoroutineKind::Desugared(
724+
hir::CoroutineDesugaring::AsyncGen,
725+
src,
726+
)) => match src {
720727
hir::CoroutineSource::Block => " of async gen block",
721728
hir::CoroutineSource::Closure => " of async gen closure",
722729
hir::CoroutineSource::Fn => {

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_data_structures::fx::FxHashSet;
1515
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
1616
use rustc_hir::def_id::DefId;
1717
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
18-
use rustc_hir::{CoroutineKind, CoroutineSource, Mutability};
18+
use rustc_hir::{CoroutineDesugaring, CoroutineKind, CoroutineSource, Mutability};
1919
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
2020
use rustc_middle::ty::{self, ExistentialProjection, ParamEnv, Ty, TyCtxt};
2121
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
@@ -560,15 +560,31 @@ pub fn push_item_name(tcx: TyCtxt<'_>, def_id: DefId, qualified: bool, output: &
560560

561561
fn coroutine_kind_label(coroutine_kind: Option<CoroutineKind>) -> &'static str {
562562
match coroutine_kind {
563-
Some(CoroutineKind::Gen(CoroutineSource::Block)) => "gen_block",
564-
Some(CoroutineKind::Gen(CoroutineSource::Closure)) => "gen_closure",
565-
Some(CoroutineKind::Gen(CoroutineSource::Fn)) => "gen_fn",
566-
Some(CoroutineKind::Async(CoroutineSource::Block)) => "async_block",
567-
Some(CoroutineKind::Async(CoroutineSource::Closure)) => "async_closure",
568-
Some(CoroutineKind::Async(CoroutineSource::Fn)) => "async_fn",
569-
Some(CoroutineKind::AsyncGen(CoroutineSource::Block)) => "async_gen_block",
570-
Some(CoroutineKind::AsyncGen(CoroutineSource::Closure)) => "async_gen_closure",
571-
Some(CoroutineKind::AsyncGen(CoroutineSource::Fn)) => "async_gen_fn",
563+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Block)) => {
564+
"gen_block"
565+
}
566+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Closure)) => {
567+
"gen_closure"
568+
}
569+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Gen, CoroutineSource::Fn)) => "gen_fn",
570+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Block)) => {
571+
"async_block"
572+
}
573+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Closure)) => {
574+
"async_closure"
575+
}
576+
Some(CoroutineKind::Desugared(CoroutineDesugaring::Async, CoroutineSource::Fn)) => {
577+
"async_fn"
578+
}
579+
Some(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, CoroutineSource::Block)) => {
580+
"async_gen_block"
581+
}
582+
Some(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, CoroutineSource::Closure)) => {
583+
"async_gen_closure"
584+
}
585+
Some(CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, CoroutineSource::Fn)) => {
586+
"async_gen_fn"
587+
}
572588
Some(CoroutineKind::Coroutine) => "coroutine",
573589
None => "closure",
574590
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
464464

465465
Rvalue::Aggregate(kind, ..) => {
466466
if let AggregateKind::Coroutine(def_id, ..) = kind.as_ref()
467-
&& let Some(coroutine_kind @ hir::CoroutineKind::Async(..)) =
468-
self.tcx.coroutine_kind(def_id)
467+
&& let Some(
468+
coroutine_kind @ hir::CoroutineKind::Desugared(
469+
hir::CoroutineDesugaring::Async,
470+
_,
471+
),
472+
) = self.tcx.coroutine_kind(def_id)
469473
{
470474
self.check_op(ops::Coroutine(coroutine_kind));
471475
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
339339
pub struct Coroutine(pub hir::CoroutineKind);
340340
impl<'tcx> NonConstOp<'tcx> for Coroutine {
341341
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
342-
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
342+
if let hir::CoroutineKind::Desugared(
343+
hir::CoroutineDesugaring::Async,
344+
hir::CoroutineSource::Block,
345+
) = self.0
346+
{
343347
Status::Unstable(sym::const_async_blocks)
344348
} else {
345349
Status::Forbidden
@@ -348,7 +352,11 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
348352

349353
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
350354
let msg = format!("{:#}s are not allowed in {}s", self.0, ccx.const_kind());
351-
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
355+
if let hir::CoroutineKind::Desugared(
356+
hir::CoroutineDesugaring::Async,
357+
hir::CoroutineSource::Block,
358+
) = self.0
359+
{
352360
ccx.tcx.sess.create_feature_err(
353361
errors::UnallowedOpInConstContext { span, msg },
354362
sym::const_async_blocks,

0 commit comments

Comments
 (0)