Skip to content

Commit bc45cdb

Browse files
Rollup merge of rust-lang#138081 - eholk:yield-feature, r=oli-obk
Move `yield` expressions behind their own feature gate In order to make progress with the `iter!` macro (e.g. in rust-lang#137725), we need `yield` expressions to be available without the `coroutines` feature. This PR moves `yield` to be guarded by the `yield_expr` feature so that we can stabilize that independently (or at least, concurrently with the `iter_macro` feature). Note that once `yield` is stable, it will still be an error to use `yield` expressions outside something like a generator or coroutine, and these features remain unstable. r? `@oli-obk`
2 parents 6963d74 + 432e1c3 commit bc45cdb

9 files changed

+74
-50
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
16901690
let yielded =
16911691
opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
16921692

1693+
if !self.tcx.features().yield_expr()
1694+
&& !self.tcx.features().coroutines()
1695+
&& !self.tcx.features().gen_blocks()
1696+
{
1697+
rustc_session::parse::feature_err(
1698+
&self.tcx.sess,
1699+
sym::yield_expr,
1700+
span,
1701+
fluent_generated::ast_lowering_yield,
1702+
)
1703+
.emit();
1704+
}
1705+
16931706
let is_async_gen = match self.coroutine_kind {
16941707
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => false,
16951708
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
@@ -1714,28 +1727,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
17141727
None,
17151728
);
17161729
}
1717-
Some(hir::CoroutineKind::Coroutine(_)) => {
1718-
if !self.tcx.features().coroutines() {
1719-
rustc_session::parse::feature_err(
1720-
&self.tcx.sess,
1721-
sym::coroutines,
1722-
span,
1723-
fluent_generated::ast_lowering_yield,
1724-
)
1725-
.emit();
1726-
}
1727-
false
1728-
}
1730+
Some(hir::CoroutineKind::Coroutine(_)) => false,
17291731
None => {
1730-
if !self.tcx.features().coroutines() {
1731-
rustc_session::parse::feature_err(
1732-
&self.tcx.sess,
1733-
sym::coroutines,
1734-
span,
1735-
fluent_generated::ast_lowering_yield,
1736-
)
1737-
.emit();
1738-
}
17391732
let suggestion = self.current_item.map(|s| s.shrink_to_lo());
17401733
self.dcx().emit_err(YieldInClosure { span, suggestion });
17411734
self.coroutine_kind = Some(hir::CoroutineKind::Coroutine(Movability::Movable));

compiler/rustc_feature/src/unstable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ declare_features! (
670670
(unstable, xop_target_feature, "1.81.0", Some(127208)),
671671
/// Allows `do yeet` expressions
672672
(unstable, yeet_expr, "1.62.0", Some(96373)),
673+
(unstable, yield_expr, "CURRENT_RUSTC_VERSION", Some(43122)),
673674
// !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!!
674675
// Features are listed in alphabetical order. Tidy will fail if you don't keep it this way.
675676
// !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!!

tests/ui/coroutine/gen_block.e2024.stderr

+1-21
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ LL | let _ = #[coroutine] || {};
1818
= help: add `#![feature(coroutines)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

21-
error[E0658]: yield syntax is experimental
22-
--> $DIR/gen_block.rs:16:16
23-
|
24-
LL | let _ = || yield true;
25-
| ^^^^^^^^^^
26-
|
27-
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
28-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
29-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30-
3121
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
3222
--> $DIR/gen_block.rs:16:16
3323
|
@@ -39,23 +29,13 @@ help: use `#[coroutine]` to make this closure a coroutine
3929
LL | let _ = #[coroutine] || yield true;
4030
| ++++++++++++
4131

42-
error[E0658]: yield syntax is experimental
43-
--> $DIR/gen_block.rs:20:29
44-
|
45-
LL | let _ = #[coroutine] || yield true;
46-
| ^^^^^^^^^^
47-
|
48-
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
49-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
50-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
51-
5232
error[E0282]: type annotations needed
5333
--> $DIR/gen_block.rs:7:13
5434
|
5535
LL | let x = gen {};
5636
| ^^^^^^ cannot infer type
5737

58-
error: aborting due to 6 previous errors
38+
error: aborting due to 4 previous errors
5939

6040
Some errors have detailed explanations: E0282, E0658.
6141
For more information about an error, try `rustc --explain E0282`.

tests/ui/coroutine/gen_block.none.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ LL | let _ = || yield true;
7171
| ^^^^^^^^^^
7272
|
7373
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
74-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
74+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
7575
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
7676

7777
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
@@ -92,7 +92,7 @@ LL | let _ = #[coroutine] || yield true;
9292
| ^^^^^^^^^^
9393
|
9494
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
95-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
95+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
9696
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9797

9898
error: aborting due to 11 previous errors

tests/ui/coroutine/gen_block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ fn main() {
1414
//[none]~^ ERROR: cannot find
1515

1616
let _ = || yield true; //[none]~ ERROR yield syntax is experimental
17-
//~^ ERROR yield syntax is experimental
17+
//[none]~^ ERROR yield syntax is experimental
1818
//~^^ ERROR `yield` can only be used in
1919

2020
let _ = #[coroutine] || yield true; //[none]~ ERROR yield syntax is experimental
2121
//~^ ERROR `#[coroutine]` attribute is an experimental feature
22-
//~^^ ERROR yield syntax is experimental
22+
//[none]~^^ ERROR yield syntax is experimental
2323

2424
let _ = #[coroutine] || {};
2525
//~^ ERROR `#[coroutine]` attribute is an experimental feature

tests/ui/feature-gates/feature-gate-coroutines.e2024.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LL | yield true;
4545
| ^^^^^^^^^^
4646
|
4747
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
48-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
48+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
4949
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5050

5151
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
@@ -66,7 +66,7 @@ LL | let _ = || yield true;
6666
| ^^^^^^^^^^
6767
|
6868
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
69-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
69+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
7070
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
7171

7272
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks

tests/ui/feature-gates/feature-gate-coroutines.none.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LL | yield true;
4545
| ^^^^^^^^^^
4646
|
4747
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
48-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
48+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
4949
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5050

5151
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
@@ -66,7 +66,7 @@ LL | let _ = || yield true;
6666
| ^^^^^^^^^^
6767
|
6868
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
69-
= help: add `#![feature(coroutines)]` to the crate attributes to enable
69+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
7070
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
7171

7272
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition: 2024
2+
#![feature(stmt_expr_attributes)]
3+
4+
fn main() {
5+
yield (); //~ ERROR yield syntax is experimental
6+
//~^ ERROR yield syntax is experimental
7+
//~^^ ERROR `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
8+
//~^^^ ERROR yield expression outside of coroutine literal
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0658]: yield syntax is experimental
2+
--> $DIR/feature-gate-yield-expr.rs:5:5
3+
|
4+
LL | yield ();
5+
| ^^^^^^^^
6+
|
7+
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
8+
= help: add `#![feature(coroutines)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: yield syntax is experimental
12+
--> $DIR/feature-gate-yield-expr.rs:5:5
13+
|
14+
LL | yield ();
15+
| ^^^^^^^^
16+
|
17+
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
18+
= help: add `#![feature(yield_expr)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: `yield` can only be used in `#[coroutine]` closures, or `gen` blocks
22+
--> $DIR/feature-gate-yield-expr.rs:5:5
23+
|
24+
LL | yield ();
25+
| ^^^^^^^^
26+
|
27+
help: use `#[coroutine]` to make this closure a coroutine
28+
|
29+
LL | #[coroutine] fn main() {
30+
| ++++++++++++
31+
32+
error[E0627]: yield expression outside of coroutine literal
33+
--> $DIR/feature-gate-yield-expr.rs:5:5
34+
|
35+
LL | yield ();
36+
| ^^^^^^^^
37+
38+
error: aborting due to 4 previous errors
39+
40+
Some errors have detailed explanations: E0627, E0658.
41+
For more information about an error, try `rustc --explain E0627`.

0 commit comments

Comments
 (0)