Skip to content

Commit 2462a2d

Browse files
committedOct 14, 2018
Auto merge of #55015 - dsciarra:underscores-constant-names, r=petrochenkov
Support underscore as constant name Issue: #54912
2 parents 1ebcb21 + 406cbf1 commit 2462a2d

5 files changed

+100
-1
lines changed
 

‎src/libsyntax/feature_gate.rs

+10
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ declare_features! (
499499

500500
// #[cfg_attr(predicate, multiple, attributes, here)]
501501
(active, cfg_attr_multi, "1.31.0", Some(54881), None),
502+
503+
// Allows `const _: TYPE = VALUE`
504+
(active, underscore_const_names, "1.31.0", Some(54912), None),
502505
);
503506

504507
declare_features! (
@@ -1583,6 +1586,13 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
15831586
}
15841587
}
15851588

1589+
ast::ItemKind::Const(_,_) => {
1590+
if i.ident.name == "_" {
1591+
gate_feature_post!(&self, underscore_const_names, i.span,
1592+
"naming constants with `_` is unstable");
1593+
}
1594+
}
1595+
15861596
ast::ItemKind::ForeignMod(ref foreign_module) => {
15871597
self.check_abi(foreign_module.abi, i.span);
15881598
}

‎src/libsyntax/parse/parser.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -6346,7 +6346,13 @@ impl<'a> Parser<'a> {
63466346
}
63476347

63486348
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
6349-
let id = self.parse_ident()?;
6349+
let id = match self.token {
6350+
token::Ident(ident, false) if ident.name == keywords::Underscore.name() => {
6351+
self.bump(); // `_`
6352+
ident.gensym()
6353+
},
6354+
_ => self.parse_ident()?,
6355+
};
63506356
self.expect(&token::Colon)?;
63516357
let ty = self.parse_ty()?;
63526358
self.expect(&token::Eq)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
#![feature(const_let)]
11+
12+
trait Trt {}
13+
struct Str {}
14+
15+
impl Trt for Str {}
16+
17+
const _ : () = {
18+
use std::marker::PhantomData;
19+
struct ImplementsTrait<T: Trt>(PhantomData<T>);
20+
let _ = ImplementsTrait::<Str>(PhantomData);
21+
()
22+
};
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0658]: naming constants with `_` is unstable (see issue #54912)
2+
--> $DIR/feature-gate-underscore_const_names.rs:17:1
3+
|
4+
LL | / const _ : () = {
5+
LL | | use std::marker::PhantomData;
6+
LL | | struct ImplementsTrait<T: Trt>(PhantomData<T>);
7+
LL | | let _ = ImplementsTrait::<Str>(PhantomData);
8+
LL | | ()
9+
LL | | };
10+
| |__^
11+
|
12+
= help: add #![feature(underscore_const_names)] to the crate attributes to enable
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0658`.

‎src/test/ui/underscore_const_names.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
13+
#![feature(const_let)]
14+
#![feature(underscore_const_names)]
15+
16+
trait Trt {}
17+
struct Str {}
18+
impl Trt for Str {}
19+
20+
macro_rules! check_impl {
21+
($struct:ident,$trait:ident) => {
22+
const _ : () = {
23+
use std::marker::PhantomData;
24+
struct ImplementsTrait<T: $trait>(PhantomData<T>);
25+
let _ = ImplementsTrait::<$struct>(PhantomData);
26+
()
27+
};
28+
}
29+
}
30+
31+
#[deny(unused)]
32+
const _ : () = ();
33+
34+
const _ : i32 = 42;
35+
const _ : Str = Str{};
36+
37+
check_impl!(Str, Trt);
38+
check_impl!(Str, Trt);
39+
40+
fn main() {
41+
check_impl!(Str, Trt);
42+
check_impl!(Str, Trt);
43+
}

0 commit comments

Comments
 (0)