1
Fork 0

Support underscore as constant name

Issue: 54912
This commit is contained in:
Donato Sciarra 2018-10-12 00:34:26 +02:00
parent 2bab4bf486
commit 406cbf1a39
5 changed files with 100 additions and 1 deletions

View file

@ -499,6 +499,9 @@ declare_features! (
// #[cfg_attr(predicate, multiple, attributes, here)] // #[cfg_attr(predicate, multiple, attributes, here)]
(active, cfg_attr_multi, "1.31.0", Some(54881), None), (active, cfg_attr_multi, "1.31.0", Some(54881), None),
// Allows `const _: TYPE = VALUE`
(active, underscore_const_names, "1.31.0", Some(54912), None),
); );
declare_features! ( declare_features! (
@ -1583,6 +1586,13 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
} }
} }
ast::ItemKind::Const(_,_) => {
if i.ident.name == "_" {
gate_feature_post!(&self, underscore_const_names, i.span,
"naming constants with `_` is unstable");
}
}
ast::ItemKind::ForeignMod(ref foreign_module) => { ast::ItemKind::ForeignMod(ref foreign_module) => {
self.check_abi(foreign_module.abi, i.span); self.check_abi(foreign_module.abi, i.span);
} }

View file

@ -6346,7 +6346,13 @@ impl<'a> Parser<'a> {
} }
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> { fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
let id = self.parse_ident()?; let id = match self.token {
token::Ident(ident, false) if ident.name == keywords::Underscore.name() => {
self.bump(); // `_`
ident.gensym()
},
_ => self.parse_ident()?,
};
self.expect(&token::Colon)?; self.expect(&token::Colon)?;
let ty = self.parse_ty()?; let ty = self.parse_ty()?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;

View file

@ -0,0 +1,24 @@
// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(const_let)]
trait Trt {}
struct Str {}
impl Trt for Str {}
const _ : () = {
use std::marker::PhantomData;
struct ImplementsTrait<T: Trt>(PhantomData<T>);
let _ = ImplementsTrait::<Str>(PhantomData);
()
};
fn main() {}

View file

@ -0,0 +1,16 @@
error[E0658]: naming constants with `_` is unstable (see issue #54912)
--> $DIR/feature-gate-underscore_const_names.rs:17:1
|
LL | / const _ : () = {
LL | | use std::marker::PhantomData;
LL | | struct ImplementsTrait<T: Trt>(PhantomData<T>);
LL | | let _ = ImplementsTrait::<Str>(PhantomData);
LL | | ()
LL | | };
| |__^
|
= 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`.

View file

@ -0,0 +1,43 @@
// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-pass
#![feature(const_let)]
#![feature(underscore_const_names)]
trait Trt {}
struct Str {}
impl Trt for Str {}
macro_rules! check_impl {
($struct:ident,$trait:ident) => {
const _ : () = {
use std::marker::PhantomData;
struct ImplementsTrait<T: $trait>(PhantomData<T>);
let _ = ImplementsTrait::<$struct>(PhantomData);
()
};
}
}
#[deny(unused)]
const _ : () = ();
const _ : i32 = 42;
const _ : Str = Str{};
check_impl!(Str, Trt);
check_impl!(Str, Trt);
fn main() {
check_impl!(Str, Trt);
check_impl!(Str, Trt);
}