Skip to content

Commit 4edff84

Browse files
committed
Auto merge of #61347 - Centril:stabilize-underscore_const_names, r=petrochenkov
Stabilize underscore_const_names in 1.37.0 You are now permitted to write: ```rust const _: $type_expression = $term_expression; ``` That is, we change the [grammar of items](https://github.com/rust-lang-nursery/wg-grammar/blob/9d1984d7ae8d6576f943566539a31a5800644c57/grammar/item.lyg#L3-L42), as written in [the *`.lyg`* notation](https://github.com/rust-lang/gll/tree/263bf161dad903e67aa65fc591ced3cab18afa2a#grammar), from: ```java Item = attrs:OuterAttr* vis:Vis? kind:ItemKind; ItemKind = | ... | Const:{ "const" name:IDENT ":" ty:Type "=" value:Expr ";" } | ... ; ``` into: ```java Item = attrs:OuterAttr* vis:Vis? kind:ItemKind; ItemKind = | ... | Const:{ "const" name:IdentOrUnderscore ":" ty:Type "=" value:Expr ";" } | ... ; IdentOrUnderscore = | Named:IDENT | NoName:"_" ; ``` r? @petrochenkov
2 parents 799cf3f + e62c9d7 commit 4edff84

13 files changed

+134
-80
lines changed

src/librustc_data_structures/macros.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// A simple static assertion macro.
22
#[macro_export]
3-
#[allow_internal_unstable(type_ascription, underscore_const_names)]
3+
#[cfg_attr(bootstrap, allow_internal_unstable(type_ascription, underscore_const_names))]
4+
#[cfg_attr(not(bootstrap), allow_internal_unstable(type_ascription))]
45
macro_rules! static_assert {
56
($test:expr) => {
67
// Use the bool to access an array such that if the bool is false, the access
@@ -12,7 +13,7 @@ macro_rules! static_assert {
1213

1314
/// Type size assertion. The first argument is a type and the second argument is its expected size.
1415
#[macro_export]
15-
#[allow_internal_unstable(underscore_const_names)]
16+
#[cfg_attr(bootstrap, allow_internal_unstable(underscore_const_names))]
1617
macro_rules! static_assert_size {
1718
($ty:ty, $size:expr) => {
1819
const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];

src/libsyntax/feature_gate.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::source_map::Spanned;
2525
use crate::edition::{ALL_EDITIONS, Edition};
2626
use crate::visit::{self, FnKind, Visitor};
2727
use crate::parse::{token, ParseSess};
28-
use crate::symbol::{Symbol, kw, sym};
28+
use crate::symbol::{Symbol, sym};
2929
use crate::tokenstream::TokenTree;
3030

3131
use errors::{Applicability, DiagnosticBuilder, Handler};
@@ -526,9 +526,6 @@ declare_features! (
526526
// Allows `impl Trait` in bindings (`let`, `const`, `static`).
527527
(active, impl_trait_in_bindings, "1.30.0", Some(34511), None),
528528

529-
// Allows `const _: TYPE = VALUE`.
530-
(active, underscore_const_names, "1.31.0", Some(54912), None),
531-
532529
// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.
533530
(active, lint_reasons, "1.31.0", Some(54503), None),
534531

@@ -851,6 +848,8 @@ declare_features! (
851848
// Allows using `#[repr(align(X))]` on enums with equivalent semantics
852849
// to wrapping an enum in a wrapper struct with `#[repr(align(X))]`.
853850
(accepted, repr_align_enum, "1.37.0", Some(57996), None),
851+
// Allows `const _: TYPE = VALUE`.
852+
(accepted, underscore_const_names, "1.37.0", Some(54912), None),
854853

855854
// -------------------------------------------------------------------------
856855
// feature-group-end: accepted features
@@ -2000,13 +1999,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
20001999

20012000
fn visit_item(&mut self, i: &'a ast::Item) {
20022001
match i.node {
2003-
ast::ItemKind::Const(_,_) => {
2004-
if i.ident.name == kw::Underscore {
2005-
gate_feature_post!(&self, underscore_const_names, i.span,
2006-
"naming constants with `_` is unstable");
2007-
}
2008-
}
2009-
20102002
ast::ItemKind::ForeignMod(ref foreign_module) => {
20112003
self.check_abi(foreign_module.abi, i.span);
20122004
}

src/test/ui/consts/const_short_circuit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(underscore_const_names)]
2-
31
const _: bool = false && false;
42
const _: bool = true && false;
53
const _: bool = {

src/test/ui/consts/const_short_circuit.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
error: new features like let bindings are not permitted in constants which also use short circuiting operators
2-
--> $DIR/const_short_circuit.rs:6:9
2+
--> $DIR/const_short_circuit.rs:4:9
33
|
44
LL | let mut x = true && false;
55
| ^^^^^
66
|
77
note: use of `&&` operator here does not actually short circuit due to the const evaluator presently not being able to do control flow. See https://github.com/rust-lang/rust/issues/49146 for more information.
8-
--> $DIR/const_short_circuit.rs:6:22
8+
--> $DIR/const_short_circuit.rs:4:22
99
|
1010
LL | let mut x = true && false;
1111
| ^^
1212

1313
error: new features like let bindings are not permitted in constants which also use short circuiting operators
14-
--> $DIR/const_short_circuit.rs:11:9
14+
--> $DIR/const_short_circuit.rs:9:9
1515
|
1616
LL | let x = true && false;
1717
| ^
1818
|
1919
note: use of `&&` operator here does not actually short circuit due to the const evaluator presently not being able to do control flow. See https://github.com/rust-lang/rust/issues/49146 for more information.
20-
--> $DIR/const_short_circuit.rs:11:18
20+
--> $DIR/const_short_circuit.rs:9:18
2121
|
2222
LL | let x = true && false;
2323
| ^^

src/test/ui/underscore_const_names.rs src/test/ui/consts/underscore_const_names.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// compile-pass
22

3-
#![feature(underscore_const_names)]
3+
#![deny(unused)]
44

55
trait Trt {}
6-
struct Str {}
6+
pub struct Str {}
77
impl Trt for Str {}
88

99
macro_rules! check_impl {
@@ -17,7 +17,6 @@ macro_rules! check_impl {
1717
}
1818
}
1919

20-
#[deny(unused)]
2120
const _ : () = ();
2221

2322
const _ : i32 = 42;

src/test/ui/feature-gates/feature-gate-underscore_const_names.rs

-14
This file was deleted.

src/test/ui/feature-gates/feature-gate-underscore_const_names.stderr

-18
This file was deleted.

src/test/ui/feature-gates/underscore_const_names_feature_gate.rs

-3
This file was deleted.

src/test/ui/feature-gates/underscore_const_names_feature_gate.stderr

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Test that various non-const items and associated consts do not permit `_` as a name.
2+
3+
// Associated `const`s:
4+
5+
pub trait A {
6+
const _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
7+
}
8+
impl A for () {
9+
const _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
10+
}
11+
impl dyn A {
12+
const _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
13+
}
14+
15+
// Other kinds of items:
16+
17+
static _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
18+
struct _(); //~ ERROR expected identifier, found reserved identifier `_`
19+
enum _ {} //~ ERROR expected identifier, found reserved identifier `_`
20+
fn _() {} //~ ERROR expected identifier, found reserved identifier `_`
21+
mod _ {} //~ ERROR expected identifier, found reserved identifier `_`
22+
type _ = (); //~ ERROR expected identifier, found reserved identifier `_`
23+
use _; //~ ERROR expected identifier, found reserved identifier `_`
24+
use _ as g; //~ ERROR expected identifier, found reserved identifier `_`
25+
trait _ {} //~ ERROR expected identifier, found reserved identifier `_`
26+
trait _ = Copy; //~ ERROR expected identifier, found reserved identifier `_`
27+
macro_rules! _ { () => {} } //~ ERROR expected identifier, found reserved identifier `_`
28+
union _ { f: u8 } //~ ERROR expected one of `!` or `::`, found `_`
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/underscore_item_not_const.rs:6:11
3+
|
4+
LL | const _: () = ();
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected identifier, found reserved identifier `_`
8+
--> $DIR/underscore_item_not_const.rs:9:11
9+
|
10+
LL | const _: () = ();
11+
| ^ expected identifier, found reserved identifier
12+
13+
error: expected identifier, found reserved identifier `_`
14+
--> $DIR/underscore_item_not_const.rs:12:11
15+
|
16+
LL | const _: () = ();
17+
| ^ expected identifier, found reserved identifier
18+
19+
error: expected identifier, found reserved identifier `_`
20+
--> $DIR/underscore_item_not_const.rs:17:8
21+
|
22+
LL | static _: () = ();
23+
| ^ expected identifier, found reserved identifier
24+
25+
error: expected identifier, found reserved identifier `_`
26+
--> $DIR/underscore_item_not_const.rs:18:8
27+
|
28+
LL | struct _();
29+
| ^ expected identifier, found reserved identifier
30+
31+
error: expected identifier, found reserved identifier `_`
32+
--> $DIR/underscore_item_not_const.rs:19:6
33+
|
34+
LL | enum _ {}
35+
| ^ expected identifier, found reserved identifier
36+
37+
error: expected identifier, found reserved identifier `_`
38+
--> $DIR/underscore_item_not_const.rs:20:4
39+
|
40+
LL | fn _() {}
41+
| ^ expected identifier, found reserved identifier
42+
43+
error: expected identifier, found reserved identifier `_`
44+
--> $DIR/underscore_item_not_const.rs:21:5
45+
|
46+
LL | mod _ {}
47+
| ^ expected identifier, found reserved identifier
48+
49+
error: expected identifier, found reserved identifier `_`
50+
--> $DIR/underscore_item_not_const.rs:22:6
51+
|
52+
LL | type _ = ();
53+
| ^ expected identifier, found reserved identifier
54+
55+
error: expected identifier, found reserved identifier `_`
56+
--> $DIR/underscore_item_not_const.rs:23:5
57+
|
58+
LL | use _;
59+
| ^ expected identifier, found reserved identifier
60+
61+
error: expected identifier, found reserved identifier `_`
62+
--> $DIR/underscore_item_not_const.rs:24:5
63+
|
64+
LL | use _ as g;
65+
| ^ expected identifier, found reserved identifier
66+
67+
error: expected identifier, found reserved identifier `_`
68+
--> $DIR/underscore_item_not_const.rs:25:7
69+
|
70+
LL | trait _ {}
71+
| ^ expected identifier, found reserved identifier
72+
73+
error: expected identifier, found reserved identifier `_`
74+
--> $DIR/underscore_item_not_const.rs:26:7
75+
|
76+
LL | trait _ = Copy;
77+
| ^ expected identifier, found reserved identifier
78+
79+
error: expected identifier, found reserved identifier `_`
80+
--> $DIR/underscore_item_not_const.rs:27:14
81+
|
82+
LL | macro_rules! _ { () => {} }
83+
| ^ expected identifier, found reserved identifier
84+
85+
error: expected one of `!` or `::`, found `_`
86+
--> $DIR/underscore_item_not_const.rs:28:7
87+
|
88+
LL | union _ { f: u8 }
89+
| ^ expected one of `!` or `::` here
90+
91+
error: aborting due to 15 previous errors
92+

src/test/ui/parser/underscore_static.rs

-3
This file was deleted.

src/test/ui/parser/underscore_static.stderr

-8
This file was deleted.

0 commit comments

Comments
 (0)