Skip to content

Commit

Permalink
Auto merge of #55015 - dsciarra:underscores-constant-names, r=petroch…
Browse files Browse the repository at this point in the history
…enkov

Support underscore as constant name

Issue: #54912
  • Loading branch information
bors committed Oct 14, 2018
2 parents 1ebcb21 + 406cbf1 commit 2462a2d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ declare_features! (

// #[cfg_attr(predicate, multiple, attributes, here)]
(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! (
Expand Down Expand Up @@ -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) => {
self.check_abi(foreign_module.abi, i.span);
}
Expand Down
8 changes: 7 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6346,7 +6346,13 @@ impl<'a> Parser<'a> {
}

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)?;
let ty = self.parse_ty()?;
self.expect(&token::Eq)?;
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/feature-gate-underscore_const_names.rs
Original file line number Diff line number Diff line change
@@ -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() {}
16 changes: 16 additions & 0 deletions src/test/ui/feature-gate-underscore_const_names.stderr
Original file line number Diff line number Diff line change
@@ -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`.
43 changes: 43 additions & 0 deletions src/test/ui/underscore_const_names.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 2462a2d

Please sign in to comment.