Emit pre-expansion feature gate warning for box'ed struct field pats

This commit is contained in:
León Orell Valerian Liehr
2026-03-27 17:45:04 +01:00
parent fda6d37bb8
commit a1d397e113
5 changed files with 63 additions and 2 deletions
+3
View File
@@ -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();
@@ -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<i32> }
let Packet { box x } = Packet { x: Box::new(0) }; //~ ERROR box pattern syntax is experimental
let _: i32 = x;
}
@@ -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 <https://github.com/rust-lang/rust/issues/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`.
@@ -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
}
@@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/65860>
warning: 2 warnings emitted