Skip to content

Commit 7d5c29b

Browse files
committed
Feature gate raw identifiers.
1 parent fad1648 commit 7d5c29b

14 files changed

+62
-5
lines changed

src/libsyntax/feature_gate.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ declare_features! (
452452

453453
// `use path as _;` and `extern crate c as _;`
454454
(active, underscore_imports, "1.26.0", Some(48216), None),
455+
456+
// Raw identifiers allowing keyword names to be used
457+
(active, raw_identifiers, "1.26.0", Some(48589), None),
455458
);
456459

457460
declare_features! (
@@ -1932,6 +1935,17 @@ pub fn check_crate(krate: &ast::Crate,
19321935
parse_sess: sess,
19331936
plugin_attributes,
19341937
};
1938+
1939+
if !features.raw_identifiers {
1940+
for &span in sess.raw_identifier_spans.borrow().iter() {
1941+
if !span.allows_unstable() {
1942+
gate_feature!(&ctx, raw_identifiers, span,
1943+
"raw identifiers are experimental and subject to change"
1944+
);
1945+
}
1946+
}
1947+
}
1948+
19351949
let visitor = &mut PostExpansionVisitor { context: &ctx };
19361950
visitor.whole_crate_feature_gates(krate);
19371951
visit::walk_crate(visitor, krate);

src/libsyntax/parse/lexer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,7 @@ mod tests {
17961796
included_mod_stack: RefCell::new(Vec::new()),
17971797
code_map: cm,
17981798
missing_fragment_specifiers: RefCell::new(HashSet::new()),
1799+
raw_identifier_spans: RefCell::new(Vec::new()),
17991800
registered_diagnostics: Lock::new(ErrorMap::new()),
18001801
non_modrs_mods: RefCell::new(vec![]),
18011802
}

src/libsyntax/parse/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub struct ParseSess {
4848
pub unstable_features: UnstableFeatures,
4949
pub config: CrateConfig,
5050
pub missing_fragment_specifiers: RefCell<HashSet<Span>>,
51+
/// Places where raw identifiers were used. This is used for feature gating
52+
/// raw identifiers
53+
pub raw_identifier_spans: RefCell<Vec<Span>>,
5154
/// The registered diagnostics codes
5255
pub registered_diagnostics: Lock<ErrorMap>,
5356
// Spans where a `mod foo;` statement was included in a non-mod.rs file.
@@ -74,6 +77,7 @@ impl ParseSess {
7477
unstable_features: UnstableFeatures::from_environment(),
7578
config: HashSet::new(),
7679
missing_fragment_specifiers: RefCell::new(HashSet::new()),
80+
raw_identifier_spans: RefCell::new(Vec::new()),
7781
registered_diagnostics: Lock::new(ErrorMap::new()),
7882
included_mod_stack: RefCell::new(vec![]),
7983
code_map,

src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ impl<'a> Parser<'a> {
784784

785785
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
786786
match self.token {
787-
token::Ident(i, _) => {
787+
token::Ident(i, is_raw) => {
788788
if self.token.is_reserved_ident() {
789789
let mut err = self.expected_ident_found();
790790
if recover {
@@ -793,6 +793,9 @@ impl<'a> Parser<'a> {
793793
return Err(err);
794794
}
795795
}
796+
if is_raw {
797+
self.sess.raw_identifier_spans.borrow_mut().push(self.span);
798+
}
796799
self.bump();
797800
Ok(i)
798801
}

src/test/run-pass/rfc-2151-raw-identifiers/attr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(raw_identifiers)]
12+
1113
use std::mem;
1214

1315
#[r#repr(r#C, r#packed)]

src/test/run-pass/rfc-2151-raw-identifiers/basic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(raw_identifiers)]
12+
1113
fn r#fn(r#match: u32) -> u32 {
1214
r#match
1315
}

src/test/run-pass/rfc-2151-raw-identifiers/items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(raw_identifiers)]
12+
1113
#[derive(Debug, PartialEq, Eq)]
1214
struct IntWrapper(u32);
1315

src/test/run-pass/rfc-2151-raw-identifiers/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![feature(decl_macro)]
12+
#![feature(raw_identifiers)]
1213

1314
r#macro_rules! r#struct {
1415
($r#struct:expr) => { $r#struct }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 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+
fn main() {
12+
let r#foo = 3; //~ ERROR raw identifiers are experimental and subject to change
13+
println!("{}", foo);
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0658]: raw identifiers are experimental and subject to change (see issue #48589)
2+
--> $DIR/feature-gate-raw-identifiers.rs:12:9
3+
|
4+
LL | let r#foo = 3; //~ ERROR raw identifiers are experimental and subject to change
5+
| ^^^^^
6+
|
7+
= help: add #![feature(raw_identifiers)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/raw-literal-keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// compile-flags: -Z parse-only
1212

1313
#![feature(dyn_trait)]
14+
#![feature(raw_identifiers)]
1415

1516
fn test_if() {
1617
r#if true { } //~ ERROR found `true`

src/test/ui/raw-literal-keywords.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `true`
2-
--> $DIR/raw-literal-keywords.rs:16:10
2+
--> $DIR/raw-literal-keywords.rs:17:10
33
|
44
LL | r#if true { } //~ ERROR found `true`
55
| ^^^^ expected one of 8 possible tokens here
66

77
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test`
8-
--> $DIR/raw-literal-keywords.rs:20:14
8+
--> $DIR/raw-literal-keywords.rs:21:14
99
|
1010
LL | r#struct Test; //~ ERROR found `Test`
1111
| ^^^^ expected one of 8 possible tokens here
1212

1313
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test`
14-
--> $DIR/raw-literal-keywords.rs:24:13
14+
--> $DIR/raw-literal-keywords.rs:25:13
1515
|
1616
LL | r#union Test; //~ ERROR found `Test`
1717
| ^^^^ expected one of 8 possible tokens here

src/test/ui/raw-literal-self.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// compile-flags: -Z parse-only
1212

13+
#![feature(raw_identifiers)]
14+
1315
fn self_test(r#self: u32) {
1416
//~^ ERROR `r#self` is not currently supported.
1517
}

src/test/ui/raw-literal-self.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `r#self` is not currently supported.
2-
--> $DIR/raw-literal-self.rs:13:14
2+
--> $DIR/raw-literal-self.rs:15:14
33
|
44
LL | fn self_test(r#self: u32) {
55
| ^^^^^^

0 commit comments

Comments
 (0)