From 56d71c2910a4e07512ca6bf80fe257bbfcc3265e Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 30 May 2019 12:30:03 +0200 Subject: [PATCH 1/3] Stabilize underscore_const_names. --- src/librustc_data_structures/macros.rs | 5 +++-- src/libsyntax/feature_gate.rs | 14 +++----------- src/test/ui/consts/const_short_circuit.rs | 2 -- src/test/ui/consts/const_short_circuit.stderr | 8 ++++---- .../ui/{ => consts}/underscore_const_names.rs | 5 ++--- .../feature-gate-underscore_const_names.rs | 14 -------------- .../feature-gate-underscore_const_names.stderr | 18 ------------------ .../underscore_const_names_feature_gate.rs | 3 --- .../underscore_const_names_feature_gate.stderr | 12 ------------ 9 files changed, 12 insertions(+), 69 deletions(-) rename src/test/ui/{ => consts}/underscore_const_names.rs (88%) delete mode 100644 src/test/ui/feature-gates/feature-gate-underscore_const_names.rs delete mode 100644 src/test/ui/feature-gates/feature-gate-underscore_const_names.stderr delete mode 100644 src/test/ui/feature-gates/underscore_const_names_feature_gate.rs delete mode 100644 src/test/ui/feature-gates/underscore_const_names_feature_gate.stderr diff --git a/src/librustc_data_structures/macros.rs b/src/librustc_data_structures/macros.rs index b851263aaf9c4..a813b5230d245 100644 --- a/src/librustc_data_structures/macros.rs +++ b/src/librustc_data_structures/macros.rs @@ -1,6 +1,7 @@ /// A simple static assertion macro. #[macro_export] -#[allow_internal_unstable(type_ascription, underscore_const_names)] +#[cfg_attr(stage0, allow_internal_unstable(type_ascription, underscore_const_names))] +#[cfg_attr(not(stage0), 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 @@ -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(stage0, allow_internal_unstable(underscore_const_names))] macro_rules! static_assert_size { ($ty:ty, $size:expr) => { const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()]; diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index bbb297e3c4fc7..e759d245646af 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -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}; @@ -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), @@ -843,6 +840,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 @@ -1992,13 +1991,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); } diff --git a/src/test/ui/consts/const_short_circuit.rs b/src/test/ui/consts/const_short_circuit.rs index 1e7b7ed319355..87b14a1117805 100644 --- a/src/test/ui/consts/const_short_circuit.rs +++ b/src/test/ui/consts/const_short_circuit.rs @@ -1,5 +1,3 @@ -#![feature(underscore_const_names)] - const _: bool = false && false; const _: bool = true && false; const _: bool = { diff --git a/src/test/ui/consts/const_short_circuit.stderr b/src/test/ui/consts/const_short_circuit.stderr index a67bb0b1b6d98..0a6536c88372a 100644 --- a/src/test/ui/consts/const_short_circuit.stderr +++ b/src/test/ui/consts/const_short_circuit.stderr @@ -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; | ^^ diff --git a/src/test/ui/underscore_const_names.rs b/src/test/ui/consts/underscore_const_names.rs similarity index 88% rename from src/test/ui/underscore_const_names.rs rename to src/test/ui/consts/underscore_const_names.rs index 1db022e886208..8d57e5074f1b1 100644 --- a/src/test/ui/underscore_const_names.rs +++ b/src/test/ui/consts/underscore_const_names.rs @@ -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 { @@ -17,7 +17,6 @@ macro_rules! check_impl { } } -#[deny(unused)] const _ : () = (); const _ : i32 = 42; diff --git a/src/test/ui/feature-gates/feature-gate-underscore_const_names.rs b/src/test/ui/feature-gates/feature-gate-underscore_const_names.rs deleted file mode 100644 index 6b97c24a47ed2..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-underscore_const_names.rs +++ /dev/null @@ -1,14 +0,0 @@ -trait Trt {} -struct Str {} - -impl Trt for Str {} - -const _ : () = { -//~^ ERROR is unstable - use std::marker::PhantomData; - struct ImplementsTrait(PhantomData); - let _ = ImplementsTrait::(PhantomData); - () -}; - -fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-underscore_const_names.stderr b/src/test/ui/feature-gates/feature-gate-underscore_const_names.stderr deleted file mode 100644 index 8d925424d8ce5..0000000000000 --- a/src/test/ui/feature-gates/feature-gate-underscore_const_names.stderr +++ /dev/null @@ -1,18 +0,0 @@ -error[E0658]: naming constants with `_` is unstable - --> $DIR/feature-gate-underscore_const_names.rs:6:1 - | -LL | / const _ : () = { -LL | | -LL | | use std::marker::PhantomData; -LL | | struct ImplementsTrait(PhantomData); -LL | | let _ = ImplementsTrait::(PhantomData); -LL | | () -LL | | }; - | |__^ - | - = note: for more information, see https://github.com/rust-lang/rust/issues/54912 - = help: add #![feature(underscore_const_names)] to the crate attributes to enable - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/feature-gates/underscore_const_names_feature_gate.rs b/src/test/ui/feature-gates/underscore_const_names_feature_gate.rs deleted file mode 100644 index e50bbf5b64909..0000000000000 --- a/src/test/ui/feature-gates/underscore_const_names_feature_gate.rs +++ /dev/null @@ -1,3 +0,0 @@ -const _: () = (); //~ ERROR is unstable - -fn main() {} diff --git a/src/test/ui/feature-gates/underscore_const_names_feature_gate.stderr b/src/test/ui/feature-gates/underscore_const_names_feature_gate.stderr deleted file mode 100644 index 0931145a6e26b..0000000000000 --- a/src/test/ui/feature-gates/underscore_const_names_feature_gate.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0658]: naming constants with `_` is unstable - --> $DIR/underscore_const_names_feature_gate.rs:1:1 - | -LL | const _: () = (); - | ^^^^^^^^^^^^^^^^^ - | - = note: for more information, see https://github.com/rust-lang/rust/issues/54912 - = help: add #![feature(underscore_const_names)] to the crate attributes to enable - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0658`. From 48e863ed69cb29a0b4ff5e20519d30227a1f8df5 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 30 May 2019 15:37:10 +0200 Subject: [PATCH 2/3] Harden non-const items to not accept underscore names. --- .../ui/parser/underscore_item_not_const.rs | 30 ++++++ .../parser/underscore_item_not_const.stderr | 92 +++++++++++++++++++ src/test/ui/parser/underscore_static.rs | 3 - src/test/ui/parser/underscore_static.stderr | 8 -- 4 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/parser/underscore_item_not_const.rs create mode 100644 src/test/ui/parser/underscore_item_not_const.stderr delete mode 100644 src/test/ui/parser/underscore_static.rs delete mode 100644 src/test/ui/parser/underscore_static.stderr diff --git a/src/test/ui/parser/underscore_item_not_const.rs b/src/test/ui/parser/underscore_item_not_const.rs new file mode 100644 index 0000000000000..375bdc3a46369 --- /dev/null +++ b/src/test/ui/parser/underscore_item_not_const.rs @@ -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() {} diff --git a/src/test/ui/parser/underscore_item_not_const.stderr b/src/test/ui/parser/underscore_item_not_const.stderr new file mode 100644 index 0000000000000..deb4a012e32ab --- /dev/null +++ b/src/test/ui/parser/underscore_item_not_const.stderr @@ -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 + diff --git a/src/test/ui/parser/underscore_static.rs b/src/test/ui/parser/underscore_static.rs deleted file mode 100644 index 21d6a1bc1b360..0000000000000 --- a/src/test/ui/parser/underscore_static.rs +++ /dev/null @@ -1,3 +0,0 @@ -static _: () = (); //~ ERROR expected identifier, found reserved identifier `_` - -fn main() {} diff --git a/src/test/ui/parser/underscore_static.stderr b/src/test/ui/parser/underscore_static.stderr deleted file mode 100644 index 4c41afdc3f09e..0000000000000 --- a/src/test/ui/parser/underscore_static.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: expected identifier, found reserved identifier `_` - --> $DIR/underscore_static.rs:1:8 - | -LL | static _: () = (); - | ^ expected identifier, found reserved identifier - -error: aborting due to previous error - From e62c9d7917052db098c6f27314f4daa5b9513387 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 8 Jun 2019 01:00:40 +0200 Subject: [PATCH 3/3] Stabilize underscore_const_names: stage0 -> bootstrap. --- src/librustc_data_structures/macros.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc_data_structures/macros.rs b/src/librustc_data_structures/macros.rs index a813b5230d245..6e7a8e98853c0 100644 --- a/src/librustc_data_structures/macros.rs +++ b/src/librustc_data_structures/macros.rs @@ -1,7 +1,7 @@ /// A simple static assertion macro. #[macro_export] -#[cfg_attr(stage0, allow_internal_unstable(type_ascription, underscore_const_names))] -#[cfg_attr(not(stage0), allow_internal_unstable(type_ascription))] +#[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 @@ -13,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] -#[cfg_attr(stage0, 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>()];