From cece90c65f280033790a228517dcb5a3c69805da Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 23 Oct 2023 14:29:00 +0000 Subject: [PATCH] Feature gate coroutine `yield` usage --- compiler/rustc_ast_lowering/src/expr.rs | 14 ++++++++-- tests/ui/coroutine/gen_block.e2024.stderr | 14 ++++++++-- tests/ui/coroutine/gen_block.none.stderr | 14 ++++++++-- tests/ui/coroutine/gen_block.rs | 2 +- .../feature-gate-coroutines.e2024.stderr | 23 +++++++++++++-- .../feature-gate-coroutines.none.stderr | 28 ++++++++++++++++--- .../feature-gates/feature-gate-coroutines.rs | 6 ++-- 7 files changed, 86 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index cce0b44fd98..5abb0ba722e 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1504,12 +1504,22 @@ fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> { fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> { match self.coroutine_kind { - Some(hir::CoroutineKind::Coroutine) => {} Some(hir::CoroutineKind::Gen(_)) => {} Some(hir::CoroutineKind::Async(_)) => { self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }); } - None => self.coroutine_kind = Some(hir::CoroutineKind::Coroutine), + Some(hir::CoroutineKind::Coroutine) | None => { + if !self.tcx.features().coroutines { + rustc_session::parse::feature_err( + &self.tcx.sess.parse_sess, + sym::coroutines, + span, + "yield syntax is experimental", + ) + .emit(); + } + self.coroutine_kind = Some(hir::CoroutineKind::Coroutine) + } } let expr = diff --git a/tests/ui/coroutine/gen_block.e2024.stderr b/tests/ui/coroutine/gen_block.e2024.stderr index 28cf036b7e8..f250e2f79c7 100644 --- a/tests/ui/coroutine/gen_block.e2024.stderr +++ b/tests/ui/coroutine/gen_block.e2024.stderr @@ -1,9 +1,19 @@ +error[E0658]: yield syntax is experimental + --> $DIR/gen_block.rs:15:16 + | +LL | let _ = || yield true; + | ^^^^^^^^^^ + | + = note: see issue #43122 for more information + = help: add `#![feature(coroutines)]` to the crate attributes to enable + error[E0282]: type annotations needed --> $DIR/gen_block.rs:6:17 | LL | let x = gen {}; | ^^ cannot infer type -error: aborting due to previous error +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0282`. +Some errors have detailed explanations: E0282, E0658. +For more information about an error, try `rustc --explain E0282`. diff --git a/tests/ui/coroutine/gen_block.none.stderr b/tests/ui/coroutine/gen_block.none.stderr index f1dd125efba..012a8308c7f 100644 --- a/tests/ui/coroutine/gen_block.none.stderr +++ b/tests/ui/coroutine/gen_block.none.stderr @@ -25,7 +25,7 @@ LL | gen {}; | ^^^ not found in this scope error[E0658]: yield syntax is experimental - --> $DIR/gen_block.rs:14:16 + --> $DIR/gen_block.rs:15:16 | LL | let _ = || yield true; | ^^^^^^^^^^ @@ -33,7 +33,17 @@ LL | let _ = || yield true; = note: see issue #43122 for more information = help: add `#![feature(coroutines)]` to the crate attributes to enable -error: aborting due to 5 previous errors +error[E0658]: yield syntax is experimental + --> $DIR/gen_block.rs:15:16 + | +LL | let _ = || yield true; + | ^^^^^^^^^^ + | + = note: see issue #43122 for more information + = help: add `#![feature(coroutines)]` to the crate attributes to enable + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error: aborting due to 6 previous errors Some errors have detailed explanations: E0422, E0658. For more information about an error, try `rustc --explain E0422`. diff --git a/tests/ui/coroutine/gen_block.rs b/tests/ui/coroutine/gen_block.rs index 041dcf0c02a..852c7c455a6 100644 --- a/tests/ui/coroutine/gen_block.rs +++ b/tests/ui/coroutine/gen_block.rs @@ -12,6 +12,6 @@ fn main() { gen {}; //[none]~^ ERROR: cannot find - // FIXME(gen_blocks): should also error in 2024 edition let _ = || yield true; //[none]~ ERROR yield syntax is experimental + //~^ ERROR yield syntax is experimental } diff --git a/tests/ui/feature-gates/feature-gate-coroutines.e2024.stderr b/tests/ui/feature-gates/feature-gate-coroutines.e2024.stderr index 595b42176b1..2e529236ad8 100644 --- a/tests/ui/feature-gates/feature-gate-coroutines.e2024.stderr +++ b/tests/ui/feature-gates/feature-gate-coroutines.e2024.stderr @@ -1,9 +1,28 @@ +error[E0658]: yield syntax is experimental + --> $DIR/feature-gate-coroutines.rs:5:5 + | +LL | yield true; + | ^^^^^^^^^^ + | + = note: see issue #43122 for more information + = help: add `#![feature(coroutines)]` to the crate attributes to enable + +error[E0658]: yield syntax is experimental + --> $DIR/feature-gate-coroutines.rs:9:16 + | +LL | let _ = || yield true; + | ^^^^^^^^^^ + | + = note: see issue #43122 for more information + = help: add `#![feature(coroutines)]` to the crate attributes to enable + error[E0627]: yield expression outside of coroutine literal --> $DIR/feature-gate-coroutines.rs:5:5 | LL | yield true; | ^^^^^^^^^^ -error: aborting due to previous error +error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0627`. +Some errors have detailed explanations: E0627, E0658. +For more information about an error, try `rustc --explain E0627`. diff --git a/tests/ui/feature-gates/feature-gate-coroutines.none.stderr b/tests/ui/feature-gates/feature-gate-coroutines.none.stderr index 680bd1f2878..ab24805e467 100644 --- a/tests/ui/feature-gates/feature-gate-coroutines.none.stderr +++ b/tests/ui/feature-gates/feature-gate-coroutines.none.stderr @@ -8,7 +8,7 @@ LL | yield true; = help: add `#![feature(coroutines)]` to the crate attributes to enable error[E0658]: yield syntax is experimental - --> $DIR/feature-gate-coroutines.rs:8:16 + --> $DIR/feature-gate-coroutines.rs:9:16 | LL | let _ = || yield true; | ^^^^^^^^^^ @@ -17,7 +17,7 @@ LL | let _ = || yield true; = help: add `#![feature(coroutines)]` to the crate attributes to enable error[E0658]: yield syntax is experimental - --> $DIR/feature-gate-coroutines.rs:14:5 + --> $DIR/feature-gate-coroutines.rs:16:5 | LL | yield; | ^^^^^ @@ -26,7 +26,7 @@ LL | yield; = help: add `#![feature(coroutines)]` to the crate attributes to enable error[E0658]: yield syntax is experimental - --> $DIR/feature-gate-coroutines.rs:15:5 + --> $DIR/feature-gate-coroutines.rs:17:5 | LL | yield 0; | ^^^^^^^ @@ -34,13 +34,33 @@ LL | yield 0; = note: see issue #43122 for more information = help: add `#![feature(coroutines)]` to the crate attributes to enable +error[E0658]: yield syntax is experimental + --> $DIR/feature-gate-coroutines.rs:5:5 + | +LL | yield true; + | ^^^^^^^^^^ + | + = note: see issue #43122 for more information + = help: add `#![feature(coroutines)]` to the crate attributes to enable + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error[E0658]: yield syntax is experimental + --> $DIR/feature-gate-coroutines.rs:9:16 + | +LL | let _ = || yield true; + | ^^^^^^^^^^ + | + = note: see issue #43122 for more information + = help: add `#![feature(coroutines)]` to the crate attributes to enable + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + error[E0627]: yield expression outside of coroutine literal --> $DIR/feature-gate-coroutines.rs:5:5 | LL | yield true; | ^^^^^^^^^^ -error: aborting due to 5 previous errors +error: aborting due to 7 previous errors Some errors have detailed explanations: E0627, E0658. For more information about an error, try `rustc --explain E0627`. diff --git a/tests/ui/feature-gates/feature-gate-coroutines.rs b/tests/ui/feature-gates/feature-gate-coroutines.rs index 55243549175..53b58d486a8 100644 --- a/tests/ui/feature-gates/feature-gate-coroutines.rs +++ b/tests/ui/feature-gates/feature-gate-coroutines.rs @@ -2,10 +2,12 @@ //[e2024] compile-flags: --edition 2024 -Zunstable-options fn main() { - yield true; //[none]~ ERROR yield syntax is experimental + yield true; //~ ERROR yield syntax is experimental //~^ ERROR yield expression outside of coroutine literal + //[none]~^^ ERROR yield syntax is experimental - let _ = || yield true; //[none]~ ERROR yield syntax is experimental + let _ = || yield true; //~ ERROR yield syntax is experimental + //[none]~^ ERROR yield syntax is experimental } #[cfg(FALSE)]