Skip to content

Commit b5aa943

Browse files
committed
Add hir::GeneratorKind::Iter
This is a placeholder name until we change `Gen` to `Coroutine`.
1 parent 944d1ab commit b5aa943

File tree

12 files changed

+76
-12
lines changed

12 files changed

+76
-12
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
712712
let full_span = expr.span.to(await_kw_span);
713713
match self.coroutine_kind {
714714
Some(hir::CoroutineKind::Async(_)) => {}
715-
Some(hir::CoroutineKind::Coroutine) | None => {
715+
Some(hir::CoroutineKind::Coroutine) | Some(hir::CoroutineKind::Gen(_)) | None => {
716716
self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
717717
await_kw_span,
718718
item_span: self.current_item,
@@ -930,7 +930,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
930930
movability: Movability,
931931
) -> Option<hir::Movability> {
932932
match coroutine_kind {
933-
Some(hir::CoroutineKind::Coroutine) => {
933+
Some(hir::CoroutineKind::Gen(_)) | Some(hir::CoroutineKind::Coroutine) => {
934934
if decl.inputs.len() > 1 {
935935
self.tcx.sess.emit_err(CoroutineTooManyParameters { fn_decl_span });
936936
}
@@ -1446,6 +1446,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14461446
fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
14471447
match self.coroutine_kind {
14481448
Some(hir::CoroutineKind::Coroutine) => {}
1449+
Some(hir::CoroutineKind::Gen(_)) => {}
14491450
Some(hir::CoroutineKind::Async(_)) => {
14501451
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span });
14511452
}

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
25052505
};
25062506
let kind = match use_span.coroutine_kind() {
25072507
Some(coroutine_kind) => match coroutine_kind {
2508+
CoroutineKind::Gen(kind) => match kind {
2509+
AsyncCoroutineKind::Block => "gen block",
2510+
AsyncCoroutineKind::Closure => "gen closure",
2511+
_ => bug!("gen block/closure expected, but gen function found."),
2512+
},
25082513
CoroutineKind::Async(async_kind) => match async_kind {
25092514
AsyncCoroutineKind::Block => "async block",
25102515
AsyncCoroutineKind::Closure => "async closure",

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+14
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,20 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
698698
" of async function"
699699
}
700700
},
701+
Some(hir::CoroutineKind::Gen(gen)) => match gen {
702+
hir::AsyncCoroutineKind::Block => " of gen block",
703+
hir::AsyncCoroutineKind::Closure => " of gen closure",
704+
hir::AsyncCoroutineKind::Fn => {
705+
let parent_item =
706+
hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
707+
let output = &parent_item
708+
.fn_decl()
709+
.expect("generator lowered from gen fn should be in fn")
710+
.output;
711+
span = output.span();
712+
" of gen function"
713+
}
714+
},
701715
Some(hir::CoroutineKind::Coroutine) => " of coroutine",
702716
None => " of closure",
703717
};

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+3
Original file line numberDiff line numberDiff line change
@@ -560,6 +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::Gen(AsyncCoroutineKind::Block)) => "gen_block",
564+
Some(CoroutineKind::Gen(AsyncCoroutineKind::Closure)) => "gen_closure",
565+
Some(CoroutineKind::Gen(AsyncCoroutineKind::Fn)) => "gen_fn",
563566
Some(CoroutineKind::Async(AsyncCoroutineKind::Block)) => "async_block",
564567
Some(CoroutineKind::Async(AsyncCoroutineKind::Closure)) => "async_closure",
565568
Some(CoroutineKind::Async(AsyncCoroutineKind::Fn)) => "async_fn",

compiler/rustc_hir/src/hir.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,9 @@ pub enum CoroutineKind {
15131513
/// An explicit `async` block or the body of an async function.
15141514
Async(AsyncCoroutineKind),
15151515

1516+
/// An explicit `gen` block or the body of a `gen` function.
1517+
Gen(AsyncCoroutineKind),
1518+
15161519
/// A coroutine literal created via a `yield` inside a closure.
15171520
Coroutine,
15181521
}
@@ -1522,6 +1525,8 @@ impl fmt::Display for CoroutineKind {
15221525
match self {
15231526
CoroutineKind::Async(k) => fmt::Display::fmt(k, f),
15241527
CoroutineKind::Coroutine => f.write_str("coroutine"),
1528+
// FIXME(oli-obk): this should print `gen fn`, not `async fn`
1529+
CoroutineKind::Gen(k) => fmt::Display::fmt(k, f),
15251530
}
15261531
}
15271532
}
@@ -1531,6 +1536,8 @@ impl CoroutineKind {
15311536
match self {
15321537
CoroutineKind::Async(ask) => ask.descr(),
15331538
CoroutineKind::Coroutine => "coroutine",
1539+
// FIXME(oli-obk): this should print `gen fn`, not `async fn`
1540+
CoroutineKind::Gen(ask) => ask.descr(),
15341541
}
15351542
}
15361543
}
@@ -2253,6 +2260,7 @@ impl From<CoroutineKind> for YieldSource {
22532260
// Guess based on the kind of the current coroutine.
22542261
CoroutineKind::Coroutine => Self::Yield,
22552262
CoroutineKind::Async(_) => Self::Await { expr: None },
2263+
CoroutineKind::Gen(_) => Self::Yield,
22562264
}
22572265
}
22582266
}

compiler/rustc_hir_typeck/src/check.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ pub(super) fn check_fn<'a, 'tcx>(
5858
if let Some(kind) = body.coroutine_kind
5959
&& can_be_coroutine.is_some()
6060
{
61-
let yield_ty = if kind == hir::CoroutineKind::Coroutine {
62-
let yield_ty = fcx.next_ty_var(TypeVariableOrigin {
63-
kind: TypeVariableOriginKind::TypeInference,
64-
span,
65-
});
66-
fcx.require_type_is_sized(yield_ty, span, traits::SizedYieldType);
67-
yield_ty
68-
} else {
69-
Ty::new_unit(tcx)
61+
let yield_ty = match kind {
62+
hir::GeneratorKind::Gen(..) | hir::CoroutineKind::Coroutine => {
63+
let yield_ty = fcx.next_ty_var(TypeVariableOrigin {
64+
kind: TypeVariableOriginKind::TypeInference,
65+
span,
66+
});
67+
fcx.require_type_is_sized(yield_ty, span, traits::SizedYieldType);
68+
yield_ty
69+
}
70+
hir::GeneratorKind::Async(..) => Ty::new_unit(tcx),
7071
};
7172

7273
// Resume type defaults to `()` if the coroutine has no argument.

compiler/rustc_middle/src/mir/terminator.rs

+10
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,14 @@ impl<O> AssertKind<O> {
141141
RemainderByZero(_) => "attempt to calculate the remainder with a divisor of zero",
142142
ResumedAfterReturn(CoroutineKind::Coroutine) => "coroutine resumed after completion",
143143
ResumedAfterReturn(CoroutineKind::Async(_)) => "`async fn` resumed after completion",
144+
ResumedAfterReturn(CoroutineKind::Gen(_)) => {
145+
bug!("`gen fn` should just keep returning `None` after the first time")
146+
}
144147
ResumedAfterPanic(CoroutineKind::Coroutine) => "coroutine resumed after panicking",
145148
ResumedAfterPanic(CoroutineKind::Async(_)) => "`async fn` resumed after panicking",
149+
ResumedAfterPanic(CoroutineKind::Gen(_)) => {
150+
bug!("`gen fn` should just keep returning `None` after panicking")
151+
}
146152
BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
147153
bug!("Unexpected AssertKind")
148154
}
@@ -229,10 +235,14 @@ impl<O> AssertKind<O> {
229235
DivisionByZero(_) => middle_assert_divide_by_zero,
230236
RemainderByZero(_) => middle_assert_remainder_by_zero,
231237
ResumedAfterReturn(CoroutineKind::Async(_)) => middle_assert_async_resume_after_return,
238+
// FIXME(oli-obk): custom error message for `gen` blocks
239+
ResumedAfterReturn(CoroutineKind::Gen(_)) => middle_assert_async_resume_after_return,
232240
ResumedAfterReturn(CoroutineKind::Coroutine) => {
233241
middle_assert_coroutine_resume_after_return
234242
}
235243
ResumedAfterPanic(CoroutineKind::Async(_)) => middle_assert_async_resume_after_panic,
244+
// FIXME(oli-obk): custom error message for `gen` blocks
245+
ResumedAfterPanic(CoroutineKind::Gen(_)) => middle_assert_async_resume_after_panic,
236246
ResumedAfterPanic(CoroutineKind::Coroutine) => {
237247
middle_assert_coroutine_resume_after_panic
238248
}

compiler/rustc_middle/src/ty/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ impl<'tcx> TyCtxt<'tcx> {
749749
DefKind::Coroutine => match self.coroutine_kind(def_id).unwrap() {
750750
rustc_hir::CoroutineKind::Async(..) => "async closure",
751751
rustc_hir::CoroutineKind::Coroutine => "coroutine",
752+
rustc_hir::CoroutineKind::Gen(..) => "gen closure",
752753
},
753754
_ => def_kind.descr(def_id),
754755
}
@@ -766,6 +767,7 @@ impl<'tcx> TyCtxt<'tcx> {
766767
DefKind::Coroutine => match self.coroutine_kind(def_id).unwrap() {
767768
rustc_hir::CoroutineKind::Async(..) => "an",
768769
rustc_hir::CoroutineKind::Coroutine => "a",
770+
rustc_hir::CoroutineKind::Gen(..) => "a",
769771
},
770772
_ => def_kind.article(),
771773
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineKind {
887887
};
888888
stable_mir::mir::CoroutineKind::Async(async_gen)
889889
}
890+
CoroutineKind::Gen(..) => stable_mir::mir::CoroutineKind::Coroutine,
890891
CoroutineKind::Coroutine => stable_mir::mir::CoroutineKind::Coroutine,
891892
}
892893
}

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,21 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
24252425
CoroutineKind::Async(AsyncCoroutineKind::Closure) => {
24262426
format!("future created by async closure is not {trait_name}")
24272427
}
2428+
CoroutineKind::Gen(AsyncCoroutineKind::Fn) => self
2429+
.tcx
2430+
.parent(coroutine_did)
2431+
.as_local()
2432+
.map(|parent_did| hir.local_def_id_to_hir_id(parent_did))
2433+
.and_then(|parent_hir_id| hir.opt_name(parent_hir_id))
2434+
.map(|name| {
2435+
format!("iterator returned by `{name}` is not {trait_name}")
2436+
})?,
2437+
CoroutineKind::Gen(AsyncCoroutineKind::Block) => {
2438+
format!("iterator created by async block is not {trait_name}")
2439+
}
2440+
CoroutineKind::Gen(AsyncCoroutineKind::Closure) => {
2441+
format!("iterator created by async closure is not {trait_name}")
2442+
}
24282443
})
24292444
})
24302445
.unwrap_or_else(|| format!("{future_or_coroutine} is not {trait_name}"));
@@ -2905,7 +2920,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
29052920
}
29062921
ObligationCauseCode::SizedCoroutineInterior(coroutine_def_id) => {
29072922
let what = match self.tcx.coroutine_kind(coroutine_def_id) {
2908-
None | Some(hir::CoroutineKind::Coroutine) => "yield",
2923+
None | Some(hir::CoroutineKind::Coroutine) | Some(hir::CoroutineKind::Gen(_)) => "yield",
29092924
Some(hir::CoroutineKind::Async(..)) => "await",
29102925
};
29112926
err.note(format!(

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

+3
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16191619
hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Block) => "an async block",
16201620
hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Fn) => "an async function",
16211621
hir::CoroutineKind::Async(hir::AsyncCoroutineKind::Closure) => "an async closure",
1622+
hir::CoroutineKind::Gen(hir::AsyncCoroutineKind::Block) => "a gen block",
1623+
hir::CoroutineKind::Gen(hir::AsyncCoroutineKind::Fn) => "a gen function",
1624+
hir::CoroutineKind::Gen(hir::AsyncCoroutineKind::Closure) => "a gen closure",
16221625
})
16231626
}
16241627

compiler/stable_mir/src/mir/body.rs

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub enum UnOp {
137137
pub enum CoroutineKind {
138138
Async(AsyncCoroutineKind),
139139
Coroutine,
140+
Gen(AsyncCoroutineKind),
140141
}
141142

142143
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)