-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Implementation of RFC 2151, Raw Identifiers #48942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fad1648
7d5c29b
d2e7953
5c3d632
ce84a41
bfb94ac
57f9c4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,7 +200,7 @@ pub fn parse( | |
let span = match trees.next() { | ||
Some(tokenstream::TokenTree::Token(span, token::Colon)) => match trees.next() { | ||
Some(tokenstream::TokenTree::Token(end_sp, ref tok)) => match tok.ident() { | ||
Some(kind) => { | ||
Some((kind, _)) => { | ||
let span = end_sp.with_lo(start_sp.lo()); | ||
result.push(TokenTree::MetaVarDecl(span, ident, kind)); | ||
continue; | ||
|
@@ -289,14 +289,14 @@ where | |
// `tree` is followed by an `ident`. This could be `$meta_var` or the `$crate` special | ||
// metavariable that names the crate of the invokation. | ||
Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => { | ||
let ident = token.ident().unwrap(); | ||
let (ident, _) = token.ident().unwrap(); | ||
let span = ident_span.with_lo(span.lo()); | ||
if ident.name == keywords::Crate.name() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think this will accept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test case for this as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently |
||
let ident = ast::Ident { | ||
name: keywords::DollarCrate.name(), | ||
..ident | ||
}; | ||
TokenTree::Token(span, token::Ident(ident)) | ||
TokenTree::Token(span, token::Ident(ident, false)) | ||
} else { | ||
TokenTree::MetaVar(span, ident) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 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(raw_identifiers)] | ||
|
||
use std::mem; | ||
|
||
#[r#repr(r#C, r#packed)] | ||
struct Test { | ||
a: bool, b: u64 | ||
} | ||
|
||
#[r#derive(r#Debug)] | ||
struct Test2(u32); | ||
|
||
pub fn main() { | ||
assert_eq!(mem::size_of::<Test>(), 9); | ||
assert_eq!("Test2(123)", format!("{:?}", Test2(123))); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 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(raw_identifiers)] | ||
|
||
fn r#fn(r#match: u32) -> u32 { | ||
r#match | ||
} | ||
|
||
pub fn main() { | ||
let r#struct = 1; | ||
assert_eq!(1, r#struct); | ||
|
||
let foo = 2; | ||
assert_eq!(2, r#foo); | ||
|
||
let r#bar = 3; | ||
assert_eq!(3, bar); | ||
|
||
assert_eq!(4, r#fn(4)); | ||
|
||
let r#true = false; | ||
assert_eq!(r#true, false); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 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(raw_identifiers)] | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
struct IntWrapper(u32); | ||
|
||
#[derive(Debug, Ord, PartialOrd, PartialEq, Eq, Hash, Copy, Clone, Default)] | ||
struct HasKeywordField { | ||
r#struct: u32, | ||
} | ||
|
||
struct Generic<r#T>(T); | ||
|
||
trait Trait { | ||
fn r#trait(&self) -> u32; | ||
} | ||
impl Trait for Generic<u32> { | ||
fn r#trait(&self) -> u32 { | ||
self.0 | ||
} | ||
} | ||
|
||
pub fn main() { | ||
assert_eq!(IntWrapper(1), r#IntWrapper(1)); | ||
|
||
match IntWrapper(2) { | ||
r#IntWrapper(r#struct) => assert_eq!(2, r#struct), | ||
} | ||
|
||
assert_eq!("HasKeywordField { struct: 3 }", format!("{:?}", HasKeywordField { r#struct: 3 })); | ||
|
||
assert_eq!(4, Generic(4).0); | ||
assert_eq!(5, Generic(5).r#trait()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 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(decl_macro)] | ||
#![feature(raw_identifiers)] | ||
|
||
r#macro_rules! r#struct { | ||
($r#struct:expr) => { $r#struct } | ||
} | ||
|
||
macro_rules! old_macro { | ||
($a:expr) => {$a} | ||
} | ||
|
||
macro r#decl_macro($r#fn:expr) { | ||
$r#fn | ||
} | ||
|
||
macro passthrough($id:ident) { | ||
$id | ||
} | ||
|
||
macro_rules! test_pat_match { | ||
(a) => { 6 }; | ||
(r#a) => { 7 }; | ||
} | ||
|
||
pub fn main() { | ||
r#println!("{struct}", r#struct = 1); | ||
assert_eq!(2, r#struct!(2)); | ||
assert_eq!(3, r#old_macro!(3)); | ||
assert_eq!(4, decl_macro!(4)); | ||
|
||
let r#match = 5; | ||
assert_eq!(5, passthrough!(r#match)); | ||
|
||
assert_eq!("r#struct", stringify!(r#struct)); | ||
|
||
assert_eq!(6, test_pat_match!(a)); | ||
assert_eq!(7, test_pat_match!(r#a)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 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. | ||
|
||
fn main() { | ||
let r#foo = 3; //~ ERROR raw identifiers are experimental and subject to change | ||
println!("{}", foo); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0658]: raw identifiers are experimental and subject to change (see issue #48589) | ||
--> $DIR/feature-gate-raw-identifiers.rs:12:9 | ||
| | ||
LL | let r#foo = 3; //~ ERROR raw identifiers are experimental and subject to change | ||
| ^^^^^ | ||
| | ||
= help: add #![feature(raw_identifiers)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 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-flags: -Z parse-only | ||
|
||
#![feature(dyn_trait)] | ||
#![feature(raw_identifiers)] | ||
|
||
fn test_if() { | ||
r#if true { } //~ ERROR found `true` | ||
} | ||
|
||
fn test_struct() { | ||
r#struct Test; //~ ERROR found `Test` | ||
} | ||
|
||
fn test_union() { | ||
r#union Test; //~ ERROR found `Test` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `true` | ||
--> $DIR/raw-literal-keywords.rs:17:10 | ||
| | ||
LL | r#if true { } //~ ERROR found `true` | ||
| ^^^^ expected one of 8 possible tokens here | ||
|
||
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test` | ||
--> $DIR/raw-literal-keywords.rs:21:14 | ||
| | ||
LL | r#struct Test; //~ ERROR found `Test` | ||
| ^^^^ expected one of 8 possible tokens here | ||
|
||
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test` | ||
--> $DIR/raw-literal-keywords.rs:25:13 | ||
| | ||
LL | r#union Test; //~ ERROR found `Test` | ||
| ^^^^ expected one of 8 possible tokens here | ||
|
||
error: aborting due to 3 previous errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 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-flags: -Z parse-only | ||
|
||
#![feature(raw_identifiers)] | ||
|
||
fn self_test(r#self: u32) { | ||
//~^ ERROR `r#self` is not currently supported. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: `r#self` is not currently supported. | ||
--> $DIR/raw-literal-self.rs:15:14 | ||
| | ||
LL | fn self_test(r#self: u32) { | ||
| ^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 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-flags: -Z parse-only | ||
|
||
fn underscore_test(r#_: u32) { | ||
//~^ ERROR `r#_` is not currently supported. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error: `r#_` is not currently supported. | ||
--> $DIR/raw-literal-underscore.rs:13:20 | ||
| | ||
LL | fn underscore_test(r#_: u32) { | ||
| ^^^ | ||
|
||
error: aborting due to previous error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as https://github.com/rust-lang/rust/pull/48942/files#r175613878
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this in
from_ast_ident
, since it seems this is the best behavior in all these cases.