Skip to content

Commit 7ee19eb

Browse files
authored
Rollup merge of #154475 - fmease:soft-gate-box-struct-field-pat, r=Kivooeo
Emit a pre-expansion feature gate warning for `box`'ed struct field patterns While the following code triggers a feature gate *warning*: ```rs fn f() { #[cfg(false)] let box x; //~ WARN box pattern syntax is experimental } ``` the code below does not (on stable & main): ```rs fn f() { #[cfg(false)] let Struct { box x }; } ``` This is an oversight as both are part of the unstable feature `box_patterns` (that isn't properly gated pre expansion for historical reasons). Of course, both forms lead to a feature gate error *post expansion*. This is a bug fix and doesn't need any input from T-compiler or T-lang. For context, emitting warnings in these cases is legitimized by [MCP 535](https://github.com/rust-lang/compiler-team/issues/535)[^1]. Part of #154045. [^1]: In case you're wondering why the MCP talks about a *lint* even though the feature gate warnings as seen today don't reference any lint by name, read #154045 (comment).
2 parents effcc70 + a7ad9ac commit 7ee19eb

15 files changed

Lines changed: 176 additions & 24 deletions

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,9 @@ impl<'a> Parser<'a> {
17491749
} else {
17501750
// Parsing a pattern of the form `(box) (ref) (mut) fieldname`.
17511751
let is_box = self.eat_keyword(exp!(Box));
1752+
if is_box {
1753+
self.psess.gated_spans.gate(sym::box_patterns, self.prev_token.span);
1754+
}
17521755
let boxed_span = self.token.span;
17531756
let mutability = self.parse_mutability();
17541757
let by_ref = self.parse_byref();

tests/ui/auto-traits/pre-cfg.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
fn main() {
22
let box x = Box::new('c'); //~ ERROR box pattern syntax is experimental
3-
println!("x: {}", x);
3+
let _: char = x;
4+
5+
struct Packet { x: Box<i32> }
6+
7+
let Packet { box x } = Packet { x: Box::new(0) }; //~ ERROR box pattern syntax is experimental
8+
let _: i32 = x;
49
}

tests/ui/feature-gates/feature-gate-box_patterns.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ LL | let box x = Box::new('c');
88
= help: add `#![feature(box_patterns)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

11-
error: aborting due to 1 previous error
11+
error[E0658]: box pattern syntax is experimental
12+
--> $DIR/feature-gate-box_patterns.rs:7:18
13+
|
14+
LL | let Packet { box x } = Packet { x: Box::new(0) };
15+
| ^^^^^
16+
|
17+
= note: see issue #29641 <https://github.com/rust-lang/rust/issues/29641> for more information
18+
= help: add `#![feature(box_patterns)]` 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: aborting due to 2 previous errors
1222

1323
For more information about this error, try `rustc --explain E0658`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// For historical reasons, auto traits don't have a proper pre-expansion feature gate.
2+
// We're now at least issuing a *warning* for those that only exist before macro expansion.
3+
// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one.
4+
// As part of this, move these test cases into `feature-gate-auto-traits.rs`.
5+
//@ check-pass
6+
7+
#[cfg(false)]
8+
auto trait Foo {}
9+
//~^ WARN `auto` traits are unstable
10+
//~| WARN unstable syntax can change at any point in the future
11+
12+
fn main() {}

tests/ui/auto-traits/pre-cfg.stderr renamed to tests/ui/feature-gates/soft-feature-gate-auto_traits.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: `auto` traits are unstable
2-
--> $DIR/pre-cfg.rs:4:1
2+
--> $DIR/soft-feature-gate-auto_traits.rs:8:1
33
|
44
LL | auto trait Foo {}
55
| ^^^^
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// For historical reasons, box patterns don't have a proper pre-expansion feature gate.
2+
// We're now at least issuing a *warning* for those that only exist before macro expansion.
3+
// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one.
4+
// As part of this, move these test cases into `feature-gate-box_patterns.rs`.
5+
//@ check-pass
6+
7+
fn main() {
8+
#[cfg(false)]
9+
let box x;
10+
//~^ WARN box pattern syntax is experimental
11+
//~| WARN unstable syntax can change at any point in the future
12+
13+
#[cfg(false)]
14+
let Packet { box x };
15+
//~^ WARN box pattern syntax is experimental
16+
//~| WARN unstable syntax can change at any point in the future
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: box pattern syntax is experimental
2+
--> $DIR/soft-feature-gate-box_patterns.rs:9:9
3+
|
4+
LL | let box x;
5+
| ^^^^^
6+
|
7+
= note: see issue #29641 <https://github.com/rust-lang/rust/issues/29641> for more information
8+
= help: add `#![feature(box_patterns)]` 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+
= warning: unstable syntax can change at any point in the future, causing a hard error!
11+
= note: for more information, see issue #65860 <https://github.com/rust-lang/rust/issues/65860>
12+
13+
warning: box pattern syntax is experimental
14+
--> $DIR/soft-feature-gate-box_patterns.rs:14:18
15+
|
16+
LL | let Packet { box x };
17+
| ^^^
18+
|
19+
= note: see issue #29641 <https://github.com/rust-lang/rust/issues/29641> for more information
20+
= help: add `#![feature(box_patterns)]` to the crate attributes to enable
21+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
22+
= warning: unstable syntax can change at any point in the future, causing a hard error!
23+
= note: for more information, see issue #65860 <https://github.com/rust-lang/rust/issues/65860>
24+
25+
warning: 2 warnings emitted
26+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// For historical reasons, decl macros 2.0 don't have a proper pre-expansion feature gate.
2+
// We're now at least issuing a *warning* for those that only exist before macro expansion.
3+
// FIXME(#154045): Turn their post-expansion feature gate into a proper pre-expansion one.
4+
// As part of this, move these test cases into `feature-gate-decl_macro.rs`.
5+
//@ check-pass
6+
7+
#[cfg(false)]
8+
macro make() {}
9+
//~^ WARN `macro` is experimental
10+
//~| WARN unstable syntax can change at any point in the future
11+
12+
#[cfg(false)]
13+
macro create { () => {} }
14+
//~^ WARN `macro` is experimental
15+
//~| WARN unstable syntax can change at any point in the future
16+
17+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: `macro` is experimental
2+
--> $DIR/soft-feature-gate-decl_macro.rs:8:1
3+
|
4+
LL | macro make() {}
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #39412 <https://github.com/rust-lang/rust/issues/39412> for more information
8+
= help: add `#![feature(decl_macro)]` 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+
= warning: unstable syntax can change at any point in the future, causing a hard error!
11+
= note: for more information, see issue #65860 <https://github.com/rust-lang/rust/issues/65860>
12+
13+
warning: `macro` is experimental
14+
--> $DIR/soft-feature-gate-decl_macro.rs:13:1
15+
|
16+
LL | macro create { () => {} }
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
= note: see issue #39412 <https://github.com/rust-lang/rust/issues/39412> for more information
20+
= help: add `#![feature(decl_macro)]` to the crate attributes to enable
21+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
22+
= warning: unstable syntax can change at any point in the future, causing a hard error!
23+
= note: for more information, see issue #65860 <https://github.com/rust-lang/rust/issues/65860>
24+
25+
warning: 2 warnings emitted
26+

0 commit comments

Comments
 (0)