Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 10 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 8 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 24 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 16 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 43 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.
dsciarra marked this conversation as resolved.
Outdated
Show resolved Hide resolved

// 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 _ : () = ();
dsciarra marked this conversation as resolved.
Outdated
Show resolved Hide resolved

const _ : i32 = 42;
const _ : Str = Str{};

check_impl!(Str, Trt);
check_impl!(Str, Trt);

fn main() {
check_impl!(Str, Trt);
check_impl!(Str, Trt);
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.