Skip to content

Commit

Permalink
Auto merge of #61347 - Centril:stabilize-underscore_const_names, r=pe…
Browse files Browse the repository at this point in the history
…trochenkov

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
  • Loading branch information
bors committed Jun 16, 2019
2 parents 799cf3f + e62c9d7 commit 4edff84
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 80 deletions.
5 changes: 3 additions & 2 deletions src/librustc_data_structures/macros.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// A simple static assertion macro.
#[macro_export]
#[allow_internal_unstable(type_ascription, underscore_const_names)]
#[cfg_attr(bootstrap, allow_internal_unstable(type_ascription, underscore_const_names))]
#[cfg_attr(not(bootstrap), allow_internal_unstable(type_ascription))]
macro_rules! static_assert {
($test:expr) => {
// Use the bool to access an array such that if the bool is false, the access
Expand All @@ -12,7 +13,7 @@ macro_rules! static_assert {

/// Type size assertion. The first argument is a type and the second argument is its expected size.
#[macro_export]
#[allow_internal_unstable(underscore_const_names)]
#[cfg_attr(bootstrap, allow_internal_unstable(underscore_const_names))]
macro_rules! static_assert_size {
($ty:ty, $size:expr) => {
const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()];
Expand Down
14 changes: 3 additions & 11 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::source_map::Spanned;
use crate::edition::{ALL_EDITIONS, Edition};
use crate::visit::{self, FnKind, Visitor};
use crate::parse::{token, ParseSess};
use crate::symbol::{Symbol, kw, sym};
use crate::symbol::{Symbol, sym};
use crate::tokenstream::TokenTree;

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

// Allows `const _: TYPE = VALUE`.
(active, underscore_const_names, "1.31.0", Some(54912), None),

// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.
(active, lint_reasons, "1.31.0", Some(54503), None),

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

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

fn visit_item(&mut self, i: &'a ast::Item) {
match i.node {
ast::ItemKind::Const(_,_) => {
if i.ident.name == kw::Underscore {
gate_feature_post!(&self, underscore_const_names, i.span,
"naming constants with `_` is unstable");
}
}

ast::ItemKind::ForeignMod(ref foreign_module) => {
self.check_abi(foreign_module.abi, i.span);
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/consts/const_short_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(underscore_const_names)]

const _: bool = false && false;
const _: bool = true && false;
const _: bool = {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/const_short_circuit.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: new features like let bindings are not permitted in constants which also use short circuiting operators
--> $DIR/const_short_circuit.rs:6:9
--> $DIR/const_short_circuit.rs:4:9
|
LL | let mut x = true && false;
| ^^^^^
|
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.
--> $DIR/const_short_circuit.rs:6:22
--> $DIR/const_short_circuit.rs:4:22
|
LL | let mut x = true && false;
| ^^

error: new features like let bindings are not permitted in constants which also use short circuiting operators
--> $DIR/const_short_circuit.rs:11:9
--> $DIR/const_short_circuit.rs:9:9
|
LL | let x = true && false;
| ^
|
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.
--> $DIR/const_short_circuit.rs:11:18
--> $DIR/const_short_circuit.rs:9:18
|
LL | let x = true && false;
| ^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// compile-pass

#![feature(underscore_const_names)]
#![deny(unused)]

trait Trt {}
struct Str {}
pub struct Str {}
impl Trt for Str {}

macro_rules! check_impl {
Expand All @@ -17,7 +17,6 @@ macro_rules! check_impl {
}
}

#[deny(unused)]
const _ : () = ();

const _ : i32 = 42;
Expand Down
14 changes: 0 additions & 14 deletions src/test/ui/feature-gates/feature-gate-underscore_const_names.rs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

30 changes: 30 additions & 0 deletions src/test/ui/parser/underscore_item_not_const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Test that various non-const items and associated consts do not permit `_` as a name.

// Associated `const`s:

pub trait A {
const _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
}
impl A for () {
const _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
}
impl dyn A {
const _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
}

// Other kinds of items:

static _: () = (); //~ ERROR expected identifier, found reserved identifier `_`
struct _(); //~ ERROR expected identifier, found reserved identifier `_`
enum _ {} //~ ERROR expected identifier, found reserved identifier `_`
fn _() {} //~ ERROR expected identifier, found reserved identifier `_`
mod _ {} //~ ERROR expected identifier, found reserved identifier `_`
type _ = (); //~ ERROR expected identifier, found reserved identifier `_`
use _; //~ ERROR expected identifier, found reserved identifier `_`
use _ as g; //~ ERROR expected identifier, found reserved identifier `_`
trait _ {} //~ ERROR expected identifier, found reserved identifier `_`
trait _ = Copy; //~ ERROR expected identifier, found reserved identifier `_`
macro_rules! _ { () => {} } //~ ERROR expected identifier, found reserved identifier `_`
union _ { f: u8 } //~ ERROR expected one of `!` or `::`, found `_`

fn main() {}
92 changes: 92 additions & 0 deletions src/test/ui/parser/underscore_item_not_const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:6:11
|
LL | const _: () = ();
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:9:11
|
LL | const _: () = ();
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:12:11
|
LL | const _: () = ();
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:17:8
|
LL | static _: () = ();
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:18:8
|
LL | struct _();
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:19:6
|
LL | enum _ {}
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:20:4
|
LL | fn _() {}
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:21:5
|
LL | mod _ {}
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:22:6
|
LL | type _ = ();
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:23:5
|
LL | use _;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:24:5
|
LL | use _ as g;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:25:7
|
LL | trait _ {}
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:26:7
|
LL | trait _ = Copy;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/underscore_item_not_const.rs:27:14
|
LL | macro_rules! _ { () => {} }
| ^ expected identifier, found reserved identifier

error: expected one of `!` or `::`, found `_`
--> $DIR/underscore_item_not_const.rs:28:7
|
LL | union _ { f: u8 }
| ^ expected one of `!` or `::` here

error: aborting due to 15 previous errors

3 changes: 0 additions & 3 deletions src/test/ui/parser/underscore_static.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/parser/underscore_static.stderr

This file was deleted.

0 comments on commit 4edff84

Please sign in to comment.