Skip to content

Commit 4e4e561

Browse files
authored
Rollup merge of rust-lang#117175 - oli-obk:gen_fn_split, r=compiler-errors
Rename AsyncCoroutineKind to CoroutineSource pulled out of rust-lang#116447 Also refactors the printing infra of `CoroutineSource` to be ready for easily extending it with a `Gen` variant for `gen` blocks
2 parents 2a027fa + c601ade commit 4e4e561

File tree

27 files changed

+91
-97
lines changed

27 files changed

+91
-97
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
188188
e.id,
189189
None,
190190
e.span,
191-
hir::AsyncCoroutineKind::Block,
191+
hir::CoroutineSource::Block,
192192
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
193193
),
194194
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
@@ -598,7 +598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
598598
closure_node_id: NodeId,
599599
ret_ty: Option<hir::FnRetTy<'hir>>,
600600
span: Span,
601-
async_gen_kind: hir::AsyncCoroutineKind,
601+
async_gen_kind: hir::CoroutineSource,
602602
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
603603
) -> hir::ExprKind<'hir> {
604604
let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span)));
@@ -1005,7 +1005,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10051005
inner_closure_id,
10061006
async_ret_ty,
10071007
body.span,
1008-
hir::AsyncCoroutineKind::Closure,
1008+
hir::CoroutineSource::Closure,
10091009
|this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
10101010
);
10111011
let hir_id = this.lower_node_id(inner_closure_id);

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12061206
closure_id,
12071207
None,
12081208
body.span,
1209-
hir::AsyncCoroutineKind::Fn,
1209+
hir::CoroutineSource::Fn,
12101210
|this| {
12111211
// Create a block from the user's function body:
12121212
let user_body = this.lower_block_expr(body);

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{
88
use rustc_hir as hir;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
11-
use rustc_hir::{AsyncCoroutineKind, CoroutineKind, LangItem};
11+
use rustc_hir::{CoroutineKind, CoroutineSource, LangItem};
1212
use rustc_infer::traits::ObligationCause;
1313
use rustc_middle::hir::nested_filter::OnlyBodies;
1414
use rustc_middle::mir::tcx::PlaceTy;
@@ -2506,8 +2506,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25062506
let kind = match use_span.coroutine_kind() {
25072507
Some(coroutine_kind) => match coroutine_kind {
25082508
CoroutineKind::Async(async_kind) => match async_kind {
2509-
AsyncCoroutineKind::Block => "async block",
2510-
AsyncCoroutineKind::Closure => "async closure",
2509+
CoroutineSource::Block => "async block",
2510+
CoroutineSource::Closure => "async closure",
25112511
_ => bug!("async block/closure expected, but async function found."),
25122512
},
25132513
CoroutineKind::Coroutine => "coroutine",

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
682682
};
683683
let mir_description = match hir.body(body).coroutine_kind {
684684
Some(hir::CoroutineKind::Async(gen)) => match gen {
685-
hir::AsyncCoroutineKind::Block => " of async block",
686-
hir::AsyncCoroutineKind::Closure => " of async closure",
687-
hir::AsyncCoroutineKind::Fn => {
685+
hir::CoroutineSource::Block => " of async block",
686+
hir::CoroutineSource::Closure => " of async closure",
687+
hir::CoroutineSource::Fn => {
688688
let parent_item =
689689
hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
690690
let output = &parent_item

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+4-4
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::{AsyncCoroutineKind, CoroutineKind, Mutability};
18+
use rustc_hir::{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,9 +560,9 @@ 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::Async(AsyncCoroutineKind::Block)) => "async_block",
564-
Some(CoroutineKind::Async(AsyncCoroutineKind::Closure)) => "async_closure",
565-
Some(CoroutineKind::Async(AsyncCoroutineKind::Fn)) => "async_fn",
563+
Some(CoroutineKind::Async(CoroutineSource::Block)) => "async_block",
564+
Some(CoroutineKind::Async(CoroutineSource::Closure)) => "async_closure",
565+
Some(CoroutineKind::Async(CoroutineSource::Fn)) => "async_fn",
566566
Some(CoroutineKind::Coroutine) => "coroutine",
567567
None => "closure",
568568
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
360360
pub struct Coroutine(pub hir::CoroutineKind);
361361
impl<'tcx> NonConstOp<'tcx> for Coroutine {
362362
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
363-
if let hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Block) = self.0 {
363+
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
364364
Status::Unstable(sym::const_async_blocks)
365365
} else {
366366
Status::Forbidden
@@ -372,8 +372,8 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
372372
ccx: &ConstCx<'_, 'tcx>,
373373
span: Span,
374374
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
375-
let msg = format!("{}s are not allowed in {}s", self.0.descr(), ccx.const_kind());
376-
if let hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Block) = self.0 {
375+
let msg = format!("{:#}s are not allowed in {}s", self.0, ccx.const_kind());
376+
if let hir::CoroutineKind::Async(hir::CoroutineSource::Block) = self.0 {
377377
ccx.tcx.sess.create_feature_err(
378378
errors::UnallowedOpInConstContext { span, msg },
379379
sym::const_async_blocks,

compiler/rustc_hir/src/hir.rs

+20-31
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ impl<'hir> Body<'hir> {
15111511
#[derive(HashStable_Generic, Encodable, Decodable)]
15121512
pub enum CoroutineKind {
15131513
/// An explicit `async` block or the body of an async function.
1514-
Async(AsyncCoroutineKind),
1514+
Async(CoroutineSource),
15151515

15161516
/// A coroutine literal created via a `yield` inside a closure.
15171517
Coroutine,
@@ -1520,56 +1520,45 @@ pub enum CoroutineKind {
15201520
impl fmt::Display for CoroutineKind {
15211521
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15221522
match self {
1523-
CoroutineKind::Async(k) => fmt::Display::fmt(k, f),
1523+
CoroutineKind::Async(k) => {
1524+
if f.alternate() {
1525+
f.write_str("`async` ")?;
1526+
} else {
1527+
f.write_str("async ")?
1528+
}
1529+
k.fmt(f)
1530+
}
15241531
CoroutineKind::Coroutine => f.write_str("coroutine"),
15251532
}
15261533
}
15271534
}
15281535

1529-
impl CoroutineKind {
1530-
pub fn descr(&self) -> &'static str {
1531-
match self {
1532-
CoroutineKind::Async(ask) => ask.descr(),
1533-
CoroutineKind::Coroutine => "coroutine",
1534-
}
1535-
}
1536-
}
1537-
1538-
/// In the case of a coroutine created as part of an async construct,
1539-
/// which kind of async construct caused it to be created?
1536+
/// In the case of a coroutine created as part of an async/gen construct,
1537+
/// which kind of async/gen construct caused it to be created?
15401538
///
15411539
/// This helps error messages but is also used to drive coercions in
15421540
/// type-checking (see #60424).
15431541
#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
15441542
#[derive(HashStable_Generic, Encodable, Decodable)]
1545-
pub enum AsyncCoroutineKind {
1546-
/// An explicit `async` block written by the user.
1543+
pub enum CoroutineSource {
1544+
/// An explicit `async`/`gen` block written by the user.
15471545
Block,
15481546

1549-
/// An explicit `async` closure written by the user.
1547+
/// An explicit `async`/`gen` closure written by the user.
15501548
Closure,
15511549

1552-
/// The `async` block generated as the body of an async function.
1550+
/// The `async`/`gen` block generated as the body of an async/gen function.
15531551
Fn,
15541552
}
15551553

1556-
impl fmt::Display for AsyncCoroutineKind {
1554+
impl fmt::Display for CoroutineSource {
15571555
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1558-
f.write_str(match self {
1559-
AsyncCoroutineKind::Block => "async block",
1560-
AsyncCoroutineKind::Closure => "async closure body",
1561-
AsyncCoroutineKind::Fn => "async fn body",
1562-
})
1563-
}
1564-
}
1565-
1566-
impl AsyncCoroutineKind {
1567-
pub fn descr(&self) -> &'static str {
15681556
match self {
1569-
AsyncCoroutineKind::Block => "`async` block",
1570-
AsyncCoroutineKind::Closure => "`async` closure body",
1571-
AsyncCoroutineKind::Fn => "`async fn` body",
1557+
CoroutineSource::Block => "block",
1558+
CoroutineSource::Closure => "closure body",
1559+
CoroutineSource::Fn => "fn body",
15721560
}
1561+
.fmt(f)
15731562
}
15741563
}
15751564

compiler/rustc_hir_typeck/src/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
305305
) = (parent_node, callee_node)
306306
{
307307
let fn_decl_span = if hir.body(body).coroutine_kind
308-
== Some(hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Closure))
308+
== Some(hir::CoroutineKind::Async(hir::CoroutineSource::Closure))
309309
{
310310
// Actually need to unwrap one more layer of HIR to get to
311311
// the _real_ closure...

compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
636636
// In the case of the async block that we create for a function body,
637637
// we expect the return type of the block to match that of the enclosing
638638
// function.
639-
Some(hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Fn)) => {
639+
Some(hir::CoroutineKind::Async(hir::CoroutineSource::Fn)) => {
640640
debug!("closure is async fn body");
641641
let def_id = self.tcx.hir().body_owner_def_id(body.id());
642642
self.deduce_future_output_from_obligations(expr_def_id, def_id).unwrap_or_else(

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_hir::def::Res;
1010
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
1111
use rustc_hir::lang_items::LangItem;
1212
use rustc_hir::{
13-
AsyncCoroutineKind, CoroutineKind, Expr, ExprKind, GenericBound, HirId, Node, Path, QPath,
14-
Stmt, StmtKind, TyKind, WherePredicate,
13+
CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node, Path, QPath, Stmt,
14+
StmtKind, TyKind, WherePredicate,
1515
};
1616
use rustc_hir_analysis::astconv::AstConv;
1717
use rustc_infer::traits::{self, StatementAsExpression};
@@ -536,7 +536,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
536536
ty::Coroutine(def_id, ..)
537537
if matches!(
538538
self.tcx.coroutine_kind(def_id),
539-
Some(CoroutineKind::Async(AsyncCoroutineKind::Closure))
539+
Some(CoroutineKind::Async(CoroutineSource::Closure))
540540
) =>
541541
{
542542
errors::SuggestBoxing::AsyncBody

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1584,14 +1584,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15841584
target: &str,
15851585
types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
15861586
) {
1587-
for (key, values) in types.iter() {
1587+
for (kind, values) in types.iter() {
15881588
let count = values.len();
1589-
let kind = key.descr();
15901589
for &sp in values {
15911590
err.span_label(
15921591
sp,
15931592
format!(
1594-
"{}{} {}{}",
1593+
"{}{} {:#}{}",
15951594
if count == 1 { "the " } else { "one of the " },
15961595
target,
15971596
kind,
@@ -2952,17 +2951,19 @@ pub enum TyCategory {
29522951
Foreign,
29532952
}
29542953

2955-
impl TyCategory {
2956-
fn descr(&self) -> &'static str {
2954+
impl fmt::Display for TyCategory {
2955+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29572956
match self {
2958-
Self::Closure => "closure",
2959-
Self::Opaque => "opaque type",
2960-
Self::OpaqueFuture => "future",
2961-
Self::Coroutine(gk) => gk.descr(),
2962-
Self::Foreign => "foreign type",
2957+
Self::Closure => "closure".fmt(f),
2958+
Self::Opaque => "opaque type".fmt(f),
2959+
Self::OpaqueFuture => "future".fmt(f),
2960+
Self::Coroutine(gk) => gk.fmt(f),
2961+
Self::Foreign => "foreign type".fmt(f),
29632962
}
29642963
}
2964+
}
29652965

2966+
impl TyCategory {
29662967
pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
29672968
match *ty.kind() {
29682969
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),

compiler/rustc_middle/src/ty/error.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ impl<'tcx> Ty<'tcx> {
241241
}
242242
ty::Dynamic(..) => "trait object".into(),
243243
ty::Closure(..) => "closure".into(),
244-
ty::Coroutine(def_id, ..) => tcx.coroutine_kind(def_id).unwrap().descr().into(),
244+
ty::Coroutine(def_id, ..) => {
245+
format!("{:#}", tcx.coroutine_kind(def_id).unwrap()).into()
246+
}
245247
ty::CoroutineWitness(..) => "coroutine witness".into(),
246248
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
247249
ty::Infer(ty::IntVar(_)) => "integer".into(),
@@ -299,7 +301,9 @@ impl<'tcx> Ty<'tcx> {
299301
ty::FnPtr(_) => "fn pointer".into(),
300302
ty::Dynamic(..) => "trait object".into(),
301303
ty::Closure(..) => "closure".into(),
302-
ty::Coroutine(def_id, ..) => tcx.coroutine_kind(def_id).unwrap().descr().into(),
304+
ty::Coroutine(def_id, ..) => {
305+
format!("{:#}", tcx.coroutine_kind(def_id).unwrap()).into()
306+
}
303307
ty::CoroutineWitness(..) => "coroutine witness".into(),
304308
ty::Tuple(..) => "tuple".into(),
305309
ty::Placeholder(..) => "higher-ranked type".into(),

compiler/rustc_smir/src/rustc_smir/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,13 @@ impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> {
883883
impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineKind {
884884
type T = stable_mir::mir::CoroutineKind;
885885
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
886-
use rustc_hir::{AsyncCoroutineKind, CoroutineKind};
886+
use rustc_hir::{CoroutineKind, CoroutineSource};
887887
match self {
888888
CoroutineKind::Async(async_gen) => {
889889
let async_gen = match async_gen {
890-
AsyncCoroutineKind::Block => stable_mir::mir::AsyncCoroutineKind::Block,
891-
AsyncCoroutineKind::Closure => stable_mir::mir::AsyncCoroutineKind::Closure,
892-
AsyncCoroutineKind::Fn => stable_mir::mir::AsyncCoroutineKind::Fn,
890+
CoroutineSource::Block => stable_mir::mir::CoroutineSource::Block,
891+
CoroutineSource::Closure => stable_mir::mir::CoroutineSource::Closure,
892+
CoroutineSource::Fn => stable_mir::mir::CoroutineSource::Fn,
893893
};
894894
stable_mir::mir::CoroutineKind::Async(async_gen)
895895
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_hir::def_id::DefId;
2222
use rustc_hir::intravisit::Visitor;
2323
use rustc_hir::is_range_literal;
2424
use rustc_hir::lang_items::LangItem;
25-
use rustc_hir::{AsyncCoroutineKind, CoroutineKind, Node};
25+
use rustc_hir::{CoroutineKind, CoroutineSource, Node};
2626
use rustc_hir::{Expr, HirId};
2727
use rustc_infer::infer::error_reporting::TypeErrCtxt;
2828
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@@ -2410,7 +2410,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
24102410
.and_then(|coroutine_did| {
24112411
Some(match self.tcx.coroutine_kind(coroutine_did).unwrap() {
24122412
CoroutineKind::Coroutine => format!("coroutine is not {trait_name}"),
2413-
CoroutineKind::Async(AsyncCoroutineKind::Fn) => self
2413+
CoroutineKind::Async(CoroutineSource::Fn) => self
24142414
.tcx
24152415
.parent(coroutine_did)
24162416
.as_local()
@@ -2419,10 +2419,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
24192419
.map(|name| {
24202420
format!("future returned by `{name}` is not {trait_name}")
24212421
})?,
2422-
CoroutineKind::Async(AsyncCoroutineKind::Block) => {
2422+
CoroutineKind::Async(CoroutineSource::Block) => {
24232423
format!("future created by async block is not {trait_name}")
24242424
}
2425-
CoroutineKind::Async(AsyncCoroutineKind::Closure) => {
2425+
CoroutineKind::Async(CoroutineSource::Closure) => {
24262426
format!("future created by async closure is not {trait_name}")
24272427
}
24282428
})
@@ -2995,11 +2995,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
29952995
let sp = self.tcx.def_span(def_id);
29962996

29972997
// Special-case this to say "async block" instead of `[static coroutine]`.
2998-
let kind = tcx.coroutine_kind(def_id).unwrap().descr();
2998+
let kind = tcx.coroutine_kind(def_id).unwrap();
29992999
err.span_note(
30003000
sp,
30013001
with_forced_trimmed_paths!(format!(
3002-
"required because it's used within this {kind}",
3002+
"required because it's used within this {kind:#}",
30033003
)),
30043004
)
30053005
}

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1611,9 +1611,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16111611
fn describe_coroutine(&self, body_id: hir::BodyId) -> Option<&'static str> {
16121612
self.tcx.hir().body(body_id).coroutine_kind.map(|gen_kind| match gen_kind {
16131613
hir::CoroutineKind::Coroutine => "a coroutine",
1614-
hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Block) => "an async block",
1615-
hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Fn) => "an async function",
1616-
hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Closure) => "an async closure",
1614+
hir::CoroutineKind::Async(hir::CoroutineSource::Block) => "an async block",
1615+
hir::CoroutineKind::Async(hir::CoroutineSource::Fn) => "an async function",
1616+
hir::CoroutineKind::Async(hir::CoroutineSource::Closure) => "an async closure",
16171617
})
16181618
}
16191619

compiler/stable_mir/src/mir/body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ pub enum UnOp {
135135

136136
#[derive(Clone, Debug)]
137137
pub enum CoroutineKind {
138-
Async(AsyncCoroutineKind),
138+
Async(CoroutineSource),
139139
Coroutine,
140140
}
141141

142142
#[derive(Clone, Debug)]
143-
pub enum AsyncCoroutineKind {
143+
pub enum CoroutineSource {
144144
Block,
145145
Closure,
146146
Fn,

0 commit comments

Comments
 (0)