From a1d397e113cb147fddb769b9000bc59b41f7b518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Fri, 27 Mar 2026 17:45:04 +0100 Subject: [PATCH 1/2] Emit pre-expansion feature gate warning for `box`'ed struct field pats --- compiler/rustc_parse/src/parser/pat.rs | 3 +++ .../feature-gate-box_patterns.rs | 7 ++++- .../feature-gate-box_patterns.stderr | 12 ++++++++- .../soft-feature-gate-box_patterns.rs | 17 ++++++++++++ .../soft-feature-gate-box_patterns.stderr | 26 +++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/ui/feature-gates/soft-feature-gate-box_patterns.rs create mode 100644 tests/ui/feature-gates/soft-feature-gate-box_patterns.stderr diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index a21d19b6d3a2..bdcbb1eb42a8 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -1749,6 +1749,9 @@ fn parse_pat_field(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, PatField> } else { // Parsing a pattern of the form `(box) (ref) (mut) fieldname`. let is_box = self.eat_keyword(exp!(Box)); + if is_box { + self.psess.gated_spans.gate(sym::box_patterns, self.prev_token.span); + } let boxed_span = self.token.span; let mutability = self.parse_mutability(); let by_ref = self.parse_byref(); diff --git a/tests/ui/feature-gates/feature-gate-box_patterns.rs b/tests/ui/feature-gates/feature-gate-box_patterns.rs index 8bec16a974e8..9ff6604c9520 100644 --- a/tests/ui/feature-gates/feature-gate-box_patterns.rs +++ b/tests/ui/feature-gates/feature-gate-box_patterns.rs @@ -1,4 +1,9 @@ fn main() { let box x = Box::new('c'); //~ ERROR box pattern syntax is experimental - println!("x: {}", x); + let _: char = x; + + struct Packet { x: Box } + + let Packet { box x } = Packet { x: Box::new(0) }; //~ ERROR box pattern syntax is experimental + let _: i32 = x; } diff --git a/tests/ui/feature-gates/feature-gate-box_patterns.stderr b/tests/ui/feature-gates/feature-gate-box_patterns.stderr index fb61b2b1810a..6f5ee20925ee 100644 --- a/tests/ui/feature-gates/feature-gate-box_patterns.stderr +++ b/tests/ui/feature-gates/feature-gate-box_patterns.stderr @@ -8,6 +8,16 @@ LL | let box x = Box::new('c'); = help: add `#![feature(box_patterns)]` 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]: box pattern syntax is experimental + --> $DIR/feature-gate-box_patterns.rs:7:18 + | +LL | let Packet { box x } = Packet { x: Box::new(0) }; + | ^^^^^ + | + = note: see issue #29641 for more information + = help: add `#![feature(box_patterns)]` 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/feature-gates/soft-feature-gate-box_patterns.rs b/tests/ui/feature-gates/soft-feature-gate-box_patterns.rs new file mode 100644 index 000000000000..8ab0e80e6be3 --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-box_patterns.rs @@ -0,0 +1,17 @@ +// For historical reasons, box patterns don't have a proper pre-expansion feature gate. +// We're now at least issuing a *warning* for those that only exist before macro expansion. +// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one. +// As part of this, move these test cases into `feature-gate-box_patterns.rs`. +//@ check-pass + +fn main() { + #[cfg(false)] + let box x; + //~^ WARN box pattern syntax is experimental + //~| WARN unstable syntax can change at any point in the future + + #[cfg(false)] + let Packet { box x }; + //~^ WARN box pattern syntax is experimental + //~| WARN unstable syntax can change at any point in the future +} diff --git a/tests/ui/feature-gates/soft-feature-gate-box_patterns.stderr b/tests/ui/feature-gates/soft-feature-gate-box_patterns.stderr new file mode 100644 index 000000000000..a8399b5f01f8 --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-box_patterns.stderr @@ -0,0 +1,26 @@ +warning: box pattern syntax is experimental + --> $DIR/soft-feature-gate-box_patterns.rs:9:9 + | +LL | let box x; + | ^^^^^ + | + = note: see issue #29641 for more information + = help: add `#![feature(box_patterns)]` 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: box pattern syntax is experimental + --> $DIR/soft-feature-gate-box_patterns.rs:14:18 + | +LL | let Packet { box x }; + | ^^^ + | + = note: see issue #29641 for more information + = help: add `#![feature(box_patterns)]` 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: 2 warnings emitted + From a7ad9acc4ada5bef2489435a163eaab30c2db93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Fri, 27 Mar 2026 17:49:44 +0100 Subject: [PATCH 2/2] Clearly exercise all soft feature gates --- tests/ui/auto-traits/pre-cfg.rs | 8 ------ .../soft-feature-gate-auto_traits.rs | 12 +++++++++ .../soft-feature-gate-auto_traits.stderr} | 2 +- .../soft-feature-gate-decl_macro.rs | 17 ++++++++++++ .../soft-feature-gate-decl_macro.stderr | 26 +++++++++++++++++++ .../soft-feature-gate-trait_alias.rs | 17 ++++++++++++ .../soft-feature-gate-trait_alias.stderr | 26 +++++++++++++++++++ .../soft-feature-gate-try_blocks.rs | 13 ++++++++++ .../soft-feature-gate-try_blocks.stderr} | 2 +- .../try-block-homogeneous-pre-expansion.rs | 12 --------- 10 files changed, 113 insertions(+), 22 deletions(-) delete mode 100644 tests/ui/auto-traits/pre-cfg.rs create mode 100644 tests/ui/feature-gates/soft-feature-gate-auto_traits.rs rename tests/ui/{auto-traits/pre-cfg.stderr => feature-gates/soft-feature-gate-auto_traits.stderr} (92%) create mode 100644 tests/ui/feature-gates/soft-feature-gate-decl_macro.rs create mode 100644 tests/ui/feature-gates/soft-feature-gate-decl_macro.stderr create mode 100644 tests/ui/feature-gates/soft-feature-gate-trait_alias.rs create mode 100644 tests/ui/feature-gates/soft-feature-gate-trait_alias.stderr create mode 100644 tests/ui/feature-gates/soft-feature-gate-try_blocks.rs rename tests/ui/{try-block/try-block-homogeneous-pre-expansion.stderr => feature-gates/soft-feature-gate-try_blocks.stderr} (91%) delete mode 100644 tests/ui/try-block/try-block-homogeneous-pre-expansion.rs diff --git a/tests/ui/auto-traits/pre-cfg.rs b/tests/ui/auto-traits/pre-cfg.rs deleted file mode 100644 index 4820a5353580..000000000000 --- a/tests/ui/auto-traits/pre-cfg.rs +++ /dev/null @@ -1,8 +0,0 @@ -//@ check-pass - -#[cfg(false)] -auto trait Foo {} -//~^ WARN `auto` traits are unstable -//~| WARN unstable syntax can change at any point in the future, causing a hard error! - -fn main() {} diff --git a/tests/ui/feature-gates/soft-feature-gate-auto_traits.rs b/tests/ui/feature-gates/soft-feature-gate-auto_traits.rs new file mode 100644 index 000000000000..0ccbaf1e476a --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-auto_traits.rs @@ -0,0 +1,12 @@ +// For historical reasons, auto traits don't have a proper pre-expansion feature gate. +// We're now at least issuing a *warning* for those that only exist before macro expansion. +// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one. +// As part of this, move these test cases into `feature-gate-auto-traits.rs`. +//@ check-pass + +#[cfg(false)] +auto trait Foo {} +//~^ WARN `auto` traits are unstable +//~| WARN unstable syntax can change at any point in the future + +fn main() {} diff --git a/tests/ui/auto-traits/pre-cfg.stderr b/tests/ui/feature-gates/soft-feature-gate-auto_traits.stderr similarity index 92% rename from tests/ui/auto-traits/pre-cfg.stderr rename to tests/ui/feature-gates/soft-feature-gate-auto_traits.stderr index 648f9464d61d..20811aadda92 100644 --- a/tests/ui/auto-traits/pre-cfg.stderr +++ b/tests/ui/feature-gates/soft-feature-gate-auto_traits.stderr @@ -1,5 +1,5 @@ warning: `auto` traits are unstable - --> $DIR/pre-cfg.rs:4:1 + --> $DIR/soft-feature-gate-auto_traits.rs:8:1 | LL | auto trait Foo {} | ^^^^ diff --git a/tests/ui/feature-gates/soft-feature-gate-decl_macro.rs b/tests/ui/feature-gates/soft-feature-gate-decl_macro.rs new file mode 100644 index 000000000000..7d2d48503797 --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-decl_macro.rs @@ -0,0 +1,17 @@ +// For historical reasons, decl macros 2.0 don't have a proper pre-expansion feature gate. +// We're now at least issuing a *warning* for those that only exist before macro expansion. +// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one. +// As part of this, move these test cases into `feature-gate-decl_macro.rs`. +//@ check-pass + +#[cfg(false)] +macro make() {} +//~^ WARN `macro` is experimental +//~| WARN unstable syntax can change at any point in the future + +#[cfg(false)] +macro create { () => {} } +//~^ WARN `macro` is experimental +//~| WARN unstable syntax can change at any point in the future + +fn main() {} diff --git a/tests/ui/feature-gates/soft-feature-gate-decl_macro.stderr b/tests/ui/feature-gates/soft-feature-gate-decl_macro.stderr new file mode 100644 index 000000000000..71521626f69b --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-decl_macro.stderr @@ -0,0 +1,26 @@ +warning: `macro` is experimental + --> $DIR/soft-feature-gate-decl_macro.rs:8:1 + | +LL | macro make() {} + | ^^^^^^^^^^^^^^^ + | + = note: see issue #39412 for more information + = help: add `#![feature(decl_macro)]` 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: `macro` is experimental + --> $DIR/soft-feature-gate-decl_macro.rs:13:1 + | +LL | macro create { () => {} } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #39412 for more information + = help: add `#![feature(decl_macro)]` 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: 2 warnings emitted + diff --git a/tests/ui/feature-gates/soft-feature-gate-trait_alias.rs b/tests/ui/feature-gates/soft-feature-gate-trait_alias.rs new file mode 100644 index 000000000000..553e88375a2a --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-trait_alias.rs @@ -0,0 +1,17 @@ +// For historical reasons, trait aliases don't have a proper pre-expansion feature gate. +// We're now at least issuing a *warning* for those that only exist before macro expansion. +// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one. +// As part of this, move these test cases into `feature-gate-trait-alias.rs`. +//@ check-pass + +#[cfg(false)] +trait Trait =; +//~^ WARN trait aliases are experimental +//~| WARN unstable syntax can change at any point in the future + +#[cfg(false)] +trait Trait = Bound where T: Bound; +//~^ WARN trait aliases are experimental +//~| WARN unstable syntax can change at any point in the future + +fn main() {} diff --git a/tests/ui/feature-gates/soft-feature-gate-trait_alias.stderr b/tests/ui/feature-gates/soft-feature-gate-trait_alias.stderr new file mode 100644 index 000000000000..e50d4c36d122 --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-trait_alias.stderr @@ -0,0 +1,26 @@ +warning: trait aliases are experimental + --> $DIR/soft-feature-gate-trait_alias.rs:8:1 + | +LL | trait Trait =; + | ^^^^^^^^^^^^^^ + | + = note: see issue #41517 for more information + = help: add `#![feature(trait_alias)]` 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: trait aliases are experimental + --> $DIR/soft-feature-gate-trait_alias.rs:13:1 + | +LL | trait Trait = Bound where T: Bound; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #41517 for more information + = help: add `#![feature(trait_alias)]` 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: 2 warnings emitted + diff --git a/tests/ui/feature-gates/soft-feature-gate-try_blocks.rs b/tests/ui/feature-gates/soft-feature-gate-try_blocks.rs new file mode 100644 index 000000000000..aa51e60d56b4 --- /dev/null +++ b/tests/ui/feature-gates/soft-feature-gate-try_blocks.rs @@ -0,0 +1,13 @@ +// For historical reasons, try blocks don't have a proper pre-expansion feature gate. +// We're now at least issuing a *warning* for those that only exist before macro expansion. +// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one. +// As part of this, move these test cases into `feature-gate-try_blocks.rs`. +//@ edition: 2018 +//@ check-pass + +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/feature-gates/soft-feature-gate-try_blocks.stderr similarity index 91% rename from tests/ui/try-block/try-block-homogeneous-pre-expansion.stderr rename to tests/ui/feature-gates/soft-feature-gate-try_blocks.stderr index 67cbcb42fb47..2b20f40e09d0 100644 --- a/tests/ui/try-block/try-block-homogeneous-pre-expansion.stderr +++ b/tests/ui/feature-gates/soft-feature-gate-try_blocks.stderr @@ -1,5 +1,5 @@ warning: `try` blocks are unstable - --> $DIR/try-block-homogeneous-pre-expansion.rs:9:5 + --> $DIR/soft-feature-gate-try_blocks.rs:10:5 | LL | try {} | ^^^^^^ diff --git a/tests/ui/try-block/try-block-homogeneous-pre-expansion.rs b/tests/ui/try-block/try-block-homogeneous-pre-expansion.rs deleted file mode 100644 index 980f97ca0672..000000000000 --- a/tests/ui/try-block/try-block-homogeneous-pre-expansion.rs +++ /dev/null @@ -1,12 +0,0 @@ -//@ 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 -}