]> git.lizzy.rs Git - rust.git/commitdiff
Feature gate raw identifiers.
authorLymia Aluysia <lymia@lymiahugs.com>
Wed, 14 Mar 2018 07:00:41 +0000 (02:00 -0500)
committerLymia Aluysia <lymia@lymiahugs.com>
Sun, 18 Mar 2018 15:07:19 +0000 (10:07 -0500)
14 files changed:
src/libsyntax/feature_gate.rs
src/libsyntax/parse/lexer/mod.rs
src/libsyntax/parse/mod.rs
src/libsyntax/parse/parser.rs
src/test/run-pass/rfc-2151-raw-identifiers/attr.rs
src/test/run-pass/rfc-2151-raw-identifiers/basic.rs
src/test/run-pass/rfc-2151-raw-identifiers/items.rs
src/test/run-pass/rfc-2151-raw-identifiers/macros.rs
src/test/ui/feature-gate-raw-identifiers.rs [new file with mode: 0644]
src/test/ui/feature-gate-raw-identifiers.stderr [new file with mode: 0644]
src/test/ui/raw-literal-keywords.rs
src/test/ui/raw-literal-keywords.stderr
src/test/ui/raw-literal-self.rs
src/test/ui/raw-literal-self.stderr

index fa600cd686064a796a392d43a6250597581a14bc..153e42c8214f89610b48d5ef7cc43c95831cae3d 100644 (file)
@@ -452,6 +452,9 @@ pub fn walk_feature_fields<F>(&self, mut f: F)
 
     // `use path as _;` and `extern crate c as _;`
     (active, underscore_imports, "1.26.0", Some(48216), None),
+
+    // Raw identifiers allowing keyword names to be used
+    (active, raw_identifiers, "1.26.0", Some(48589), None),
 );
 
 declare_features! (
@@ -1932,6 +1935,17 @@ pub fn check_crate(krate: &ast::Crate,
         parse_sess: sess,
         plugin_attributes,
     };
+
+    if !features.raw_identifiers {
+        for &span in sess.raw_identifier_spans.borrow().iter() {
+            if !span.allows_unstable() {
+                gate_feature!(&ctx, raw_identifiers, span,
+                    "raw identifiers are experimental and subject to change"
+                );
+            }
+        }
+    }
+
     let visitor = &mut PostExpansionVisitor { context: &ctx };
     visitor.whole_crate_feature_gates(krate);
     visit::walk_crate(visitor, krate);
index 0596fb44abeea6122dd9cf780a7182a529a8f5ea..8e746ea69e79fd3a6b972f9febae082c8d4a4f18 100644 (file)
@@ -1796,6 +1796,7 @@ fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
             included_mod_stack: RefCell::new(Vec::new()),
             code_map: cm,
             missing_fragment_specifiers: RefCell::new(HashSet::new()),
+            raw_identifier_spans: RefCell::new(Vec::new()),
             registered_diagnostics: Lock::new(ErrorMap::new()),
             non_modrs_mods: RefCell::new(vec![]),
         }
index 4acfdab53c094f5515741e95a492cc245a855a48..03ac1e8b5a9bbc3470a32c5bd2291aaa608c16d1 100644 (file)
@@ -48,6 +48,9 @@ pub struct ParseSess {
     pub unstable_features: UnstableFeatures,
     pub config: CrateConfig,
     pub missing_fragment_specifiers: RefCell<HashSet<Span>>,
+    /// Places where raw identifiers were used. This is used for feature gating
+    /// raw identifiers
+    pub raw_identifier_spans: RefCell<Vec<Span>>,
     /// The registered diagnostics codes
     pub registered_diagnostics: Lock<ErrorMap>,
     // Spans where a `mod foo;` statement was included in a non-mod.rs file.
@@ -74,6 +77,7 @@ pub fn with_span_handler(handler: Handler, code_map: Lrc<CodeMap>) -> ParseSess
             unstable_features: UnstableFeatures::from_environment(),
             config: HashSet::new(),
             missing_fragment_specifiers: RefCell::new(HashSet::new()),
+            raw_identifier_spans: RefCell::new(Vec::new()),
             registered_diagnostics: Lock::new(ErrorMap::new()),
             included_mod_stack: RefCell::new(vec![]),
             code_map,
index 4c1575cf58939d1b8851c43e73c6b43fe79b1971..c2ee78e9e9d1b4edae2325e1b7186f5a2d4a2878 100644 (file)
@@ -784,7 +784,7 @@ pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
 
     fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
         match self.token {
-            token::Ident(i, _) => {
+            token::Ident(i, is_raw) => {
                 if self.token.is_reserved_ident() {
                     let mut err = self.expected_ident_found();
                     if recover {
@@ -793,6 +793,9 @@ fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
                         return Err(err);
                     }
                 }
+                if is_raw {
+                    self.sess.raw_identifier_spans.borrow_mut().push(self.span);
+                }
                 self.bump();
                 Ok(i)
             }
index 2ef9fba2076add70307e2be38398a0809f88b1c8..6cea75cf1d11e9a83daa59481c62a90d8f60d9cf 100644 (file)
@@ -8,6 +8,8 @@
 // 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)]
index eefce3981bec1f64c1a9a25b739c0d825355465f..5d495c4e9e557eca33006318ca3ffd6af05b9bd7 100644 (file)
@@ -8,6 +8,8 @@
 // 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
 }
index 4306ffe2042af6d326cfe748f858c967d59a8872..256bd263d38d42c1d5c182310ccbdf50c48e4518 100644 (file)
@@ -8,6 +8,8 @@
 // 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);
 
index 9e89b79266cface4c335c39e1a6db9e29108411b..4bd16ded52fbdf6b39ba4e5d86aaa8deadd7207f 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #![feature(decl_macro)]
+#![feature(raw_identifiers)]
 
 r#macro_rules! r#struct {
     ($r#struct:expr) => { $r#struct }
diff --git a/src/test/ui/feature-gate-raw-identifiers.rs b/src/test/ui/feature-gate-raw-identifiers.rs
new file mode 100644 (file)
index 0000000..38024fe
--- /dev/null
@@ -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);
+}
diff --git a/src/test/ui/feature-gate-raw-identifiers.stderr b/src/test/ui/feature-gate-raw-identifiers.stderr
new file mode 100644 (file)
index 0000000..02eff72
--- /dev/null
@@ -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`.
index 8b3747dbe15e4793222fe223a6d3dd2178dd0c32..9b28aa0b15116d9549b56ac0bf908f52b01a34bf 100644 (file)
@@ -11,6 +11,7 @@
 // compile-flags: -Z parse-only
 
 #![feature(dyn_trait)]
+#![feature(raw_identifiers)]
 
 fn test_if() {
     r#if true { } //~ ERROR found `true`
index 022f80ae8a4ecb31ba854774a2e9a7b961dd8b8d..3758568323cc0ac0a3e10a348bee33266e6a1d2d 100644 (file)
@@ -1,17 +1,17 @@
 error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `true`
-  --> $DIR/raw-literal-keywords.rs:16:10
+  --> $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:20:14
+  --> $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:24:13
+  --> $DIR/raw-literal-keywords.rs:25:13
    |
 LL |     r#union Test; //~ ERROR found `Test`
    |             ^^^^ expected one of 8 possible tokens here
index 17496d767b6226b95b56c0590d0f7f2e8bc82cf7..f88d6cf9a67bd635df468f0697e9f965dba7cce5 100644 (file)
@@ -10,6 +10,8 @@
 
 // compile-flags: -Z parse-only
 
+#![feature(raw_identifiers)]
+
 fn self_test(r#self: u32) {
     //~^ ERROR `r#self` is not currently supported.
 }
index f4b759372471cdb79dfd34fe990d3e4f3bc22298..e3345847aa895367624496deb0981c21de063e1f 100644 (file)
@@ -1,5 +1,5 @@
 error: `r#self` is not currently supported.
-  --> $DIR/raw-literal-self.rs:13:14
+  --> $DIR/raw-literal-self.rs:15:14
    |
 LL | fn self_test(r#self: u32) {
    |              ^^^^^^