diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 1b1bbb1564c46..9a4cfa87b92e1 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -330,15 +330,11 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_expr(&mut self, e: &'a ast::Expr) { match e.kind { ast::ExprKind::TryBlock(_, None) => { + // `try { ... }` is old and is only gated post-expansion here. gate!(&self, try_blocks, e.span, "`try` expression is experimental"); } ast::ExprKind::TryBlock(_, Some(_)) => { - gate!( - &self, - try_blocks_heterogeneous, - e.span, - "`try bikeshed` expression is experimental" - ); + // `try_blocks_heterogeneous` is new, and gated pre-expansion instead. } ast::ExprKind::Lit(token::Lit { kind: token::LitKind::Float | token::LitKind::Integer, @@ -499,6 +495,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) { half_open_range_patterns_in_slices, "half-open range patterns in slices are unstable" ); + gate_all!(try_blocks_heterogeneous, "`try bikeshed` expression is experimental"); gate_all!(yeet_expr, "`do yeet` expression is experimental"); gate_all!(const_closures, "const closures are experimental"); gate_all!(builtin_syntax, "`builtin #` syntax is unstable"); diff --git a/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.rs b/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.rs index 9f0073eac9adc..5a20370e2eeb8 100644 --- a/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.rs +++ b/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.rs @@ -6,4 +6,11 @@ pub fn main() { x }; assert_eq!(try_result, Some(5)); + + // The heterogenous form is new, so is gated even under a `cfg(false)`. + // See + + #[cfg(false)] + try bikeshed () {} + //~^ error `try bikeshed` expression is experimental } diff --git a/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.stderr b/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.stderr index 0d31dc507fdde..e448945b2ba81 100644 --- a/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.stderr +++ b/tests/ui/feature-gates/feature-gate-try_blocks_heterogeneous.stderr @@ -12,6 +12,16 @@ LL | | }; = help: add `#![feature(try_blocks_heterogeneous)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date -error: aborting due to 1 previous error +error[E0658]: `try bikeshed` expression is experimental + --> $DIR/feature-gate-try_blocks_heterogeneous.rs:14:5 + | +LL | try bikeshed () {} + | ^^^^^^^^^^^^^^^^^^ + | + = note: see issue #149488 for more information + = help: add `#![feature(try_blocks_heterogeneous)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/try-block/try-block-homogeneous-pre-expansion.rs b/tests/ui/try-block/try-block-homogeneous-pre-expansion.rs new file mode 100644 index 0000000000000..980f97ca0672e --- /dev/null +++ b/tests/ui/try-block/try-block-homogeneous-pre-expansion.rs @@ -0,0 +1,12 @@ +//@ check-pass +//@ edition: 2018 + +// For historical reasons this is only a warning, not an error. +// See + +fn main() { + #[cfg(false)] + try {} + //~^ warn `try` blocks are unstable + //~| warn unstable syntax can change at any point +} diff --git a/tests/ui/try-block/try-block-homogeneous-pre-expansion.stderr b/tests/ui/try-block/try-block-homogeneous-pre-expansion.stderr new file mode 100644 index 0000000000000..dc92d7e64aff3 --- /dev/null +++ b/tests/ui/try-block/try-block-homogeneous-pre-expansion.stderr @@ -0,0 +1,14 @@ +warning: `try` blocks are unstable + --> $DIR/try-block-homogeneous-pre-expansion.rs:9:5 + | +LL | try {} + | ^^^^^^ + | + = note: see issue #31436 for more information + = help: add `#![feature(try_blocks)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + = warning: unstable syntax can change at any point in the future, causing a hard error! + = note: for more information, see issue #65860 + +warning: 1 warning emitted +