]> git.lizzy.rs Git - rust.git/commitdiff
address comments
authorAgustin Chiappe Berrini <jnieve@gmail.com>
Thu, 7 Dec 2017 08:52:25 +0000 (03:52 -0500)
committerAgustin Chiappe Berrini <jnieve@gmail.com>
Thu, 7 Dec 2017 08:52:25 +0000 (03:52 -0500)
src/librustc_passes/ast_validation.rs
src/libsyntax/parse/lexer/mod.rs
src/libsyntax/parse/token.rs
src/libsyntax_pos/symbol.rs
src/test/compile-fail/issue-10412.rs
src/test/compile-fail/lifetime-no-keyword.rs

index 8f21028c4bdbb6cff16cf05b0ca993f948f8073a..205b8bf0d20ea348aeed6a16a0188cb631200eba 100644 (file)
 use syntax::ast::*;
 use syntax::attr;
 use syntax::codemap::Spanned;
+use syntax::parse::token;
 use syntax::visit::{self, Visitor};
 use syntax_pos::Span;
+use syntax_pos::symbol::keywords;
 use errors;
 
 struct AstValidator<'a> {
@@ -35,15 +37,15 @@ fn err_handler(&self) -> &errors::Handler {
     }
 
     fn check_lifetime(&self, lifetime: &Lifetime) {
-        if !lifetime.ident.without_first_quote().is_valid() &&
-            !lifetime.ident.name.is_static_keyword() {
+        let valid_names = [keywords::StaticLifetime.name(), keywords::Invalid.name()];
+        if !valid_names.contains(&lifetime.ident.name) &&
+            token::Ident(lifetime.ident.without_first_quote()).is_reserved_ident() {
             self.err_handler().span_err(lifetime.span, "lifetimes cannot use keyword names");
         }
     }
 
     fn check_label(&self, label: Ident, span: Span) {
-        if label.name.is_static_keyword() || !label.without_first_quote().is_valid()
-            || label.name == "'_" {
+        if token::Ident(label.without_first_quote()).is_reserved_ident() || label.name == "'_" {
             self.err_handler().span_err(span, &format!("invalid label name `{}`", label.name));
         }
     }
@@ -207,9 +209,14 @@ fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
         visit::walk_use_tree(self, use_tree, id);
     }
 
+    fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
+        self.check_lifetime(lifetime);
+        visit::walk_lifetime(self, lifetime);
+    }
+
     fn visit_item(&mut self, item: &'a Item) {
         match item.node {
-            ItemKind::Impl(.., ref generics, Some(..), _, ref impl_items) => {
+            ItemKind::Impl(.., Some(..), _, ref impl_items) => {
                 self.invalid_visibility(&item.vis, item.span, None);
                 for impl_item in impl_items {
                     self.invalid_visibility(&impl_item.vis, impl_item.span, None);
@@ -217,13 +224,11 @@ fn visit_item(&mut self, item: &'a Item) {
                         self.check_trait_fn_not_const(sig.constness);
                     }
                 }
-                generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
             }
-            ItemKind::Impl(.., ref generics, None, _, _) => {
+            ItemKind::Impl(.., None, _, _) => {
                 self.invalid_visibility(&item.vis,
                                         item.span,
                                         Some("place qualifiers on individual impl items instead"));
-                generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
             }
             ItemKind::AutoImpl(..) => {
                 self.invalid_visibility(&item.vis, item.span, None);
@@ -234,14 +239,13 @@ fn visit_item(&mut self, item: &'a Item) {
                                         Some("place qualifiers on individual foreign items \
                                               instead"));
             }
-            ItemKind::Enum(ref def, ref generics) => {
+            ItemKind::Enum(ref def, _) => {
                 for variant in &def.variants {
                     self.invalid_non_exhaustive_attribute(variant);
                     for field in variant.node.data.fields() {
                         self.invalid_visibility(&field.vis, field.span, None);
                     }
                 }
-                generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
             }
             ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
                 if is_auto == IsAuto::Yes {
@@ -278,7 +282,6 @@ fn visit_item(&mut self, item: &'a Item) {
                         }
                     }
                 }
-                generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
             }
             ItemKind::Mod(_) => {
                 // Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
@@ -289,7 +292,7 @@ fn visit_item(&mut self, item: &'a Item) {
                     self.session.buffer_lint(lint, item.id, item.span, msg);
                 }
             }
-            ItemKind::Union(ref vdata, ref generics) => {
+            ItemKind::Union(ref vdata, _) => {
                 if !vdata.is_struct() {
                     self.err_handler().span_err(item.span,
                                                 "tuple and unit unions are not permitted");
@@ -298,12 +301,6 @@ fn visit_item(&mut self, item: &'a Item) {
                     self.err_handler().span_err(item.span,
                                                 "unions cannot have zero fields");
                 }
-                generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
-            }
-            ItemKind::Fn(.., ref generics, _) |
-            ItemKind::Ty(_, ref generics) |
-            ItemKind::Struct(_, ref generics) => {
-                generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime))
             }
             _ => {}
         }
index 5de5e1fd5deb703de900ef213a352191d36154a1..d9c33fa50bd89ab8f0180b6884ec26399f0c48dd 100644 (file)
@@ -14,7 +14,7 @@
 use errors::{FatalError, DiagnosticBuilder};
 use parse::{token, ParseSess};
 use str::char_at;
-use symbol::{Symbol};
+use symbol::Symbol;
 use std_unicode::property::Pattern_White_Space;
 
 use std::borrow::Cow;
index 94b279d2bdc3565c1d2da17d79ba54cb37b98a95..ff87f146c0a71616dd2b818f3b03556838aef2b8 100644 (file)
@@ -364,12 +364,18 @@ pub fn is_special_ident(&self) -> bool {
 
     /// Returns `true` if the token is a keyword used in the language.
     pub fn is_used_keyword(&self) -> bool {
-        self.ident().map(|id| id.name.is_used_keyword()).unwrap_or(false)
+        match self.ident() {
+            Some(id) => id.name >= keywords::As.name() && id.name <= keywords::While.name(),
+            _ => false,
+        }
     }
 
     /// Returns `true` if the token is a keyword reserved for possible future use.
     pub fn is_unused_keyword(&self) -> bool {
-        self.ident().map(|id| id.name.is_unused_keyword()).unwrap_or(false)
+        match self.ident() {
+            Some(id) => id.name >= keywords::Abstract.name() && id.name <= keywords::Yield.name(),
+            _ => false,
+        }
     }
 
     pub fn glue(self, joint: Token) -> Option<Token> {
index c904e93e63ad7c68e6c45ec0e87402bc7d16f9bc..0e90e0922b977a8cebb68450d2a5b579ae442cd4 100644 (file)
@@ -42,10 +42,6 @@ pub fn without_first_quote(&self) -> Ident {
     pub fn modern(self) -> Ident {
         Ident { name: self.name, ctxt: self.ctxt.modern() }
     }
-
-    pub fn is_valid(&self) -> bool {
-        !self.name.is_used_keyword() && !self.name.is_unused_keyword()
-    }
 }
 
 impl fmt::Debug for Ident {
@@ -122,20 +118,6 @@ pub fn as_u32(self) -> u32 {
         self.0
     }
 
-    /// Returns `true` if the token is a keyword used in the language.
-    pub fn is_used_keyword(&self) -> bool {
-        self >= &keywords::As.name() && self <= &keywords::While.name()
-    }
-
-    /// Returns `true` if the token is a keyword reserved for possible future use.
-    pub fn is_unused_keyword(&self) -> bool {
-        self >= &keywords::Abstract.name() && self <= &keywords::Yield.name()
-    }
-
-    pub fn is_static_keyword(&self) -> bool {
-        self == &keywords::StaticLifetime.name()
-    }
-
     pub fn without_first_quote(&self) -> Symbol {
         Symbol::from(self.as_str().trim_left_matches('\''))
     }
index 20421bfc7c49150620ab2e420aa28f4fd788f00f..03f4fd1b3c09b97e9ab0a48450ae98314ed25ebf 100644 (file)
@@ -9,17 +9,21 @@
 // except according to those terms.
 
 trait Serializable<'self, T> { //~ ERROR lifetimes cannot use keyword names
-    fn serialize(val : &'self T) -> Vec<u8> ;
+    fn serialize(val : &'self T) -> Vec<u8>;
+    //~^ ERROR lifetimes cannot use keyword names
     fn deserialize(repr : &[u8]) -> &'self T;
+    //~^ ERROR lifetimes cannot use keyword names
 }
 
-impl<'self> Serializable<str> for &'self str {
+impl<'self> Serializable<str> for &'self str { //~ ERROR lifetimes cannot use keyword names
     //~^ ERROR lifetimes cannot use keyword names
     //~| ERROR missing lifetime specifier
     fn serialize(val : &'self str) -> Vec<u8> {
+    //~^ ERROR lifetimes cannot use keyword names
         vec![1]
     }
     fn deserialize(repr: &[u8]) -> &'self str {
+    //~^ ERROR lifetimes cannot use keyword names
         "hi"
     }
 }
index e2465663dd0d00e22eedac0ea9d6c241cc17c77a..d583c4fc6c6b8461c5ef4d3ce89d4f7a3c5ab359 100644 (file)
@@ -11,6 +11,7 @@
 fn foo<'a>(a: &'a isize) { }
 fn bar(a: &'static isize) { }
 fn baz<'let>(a: &'let isize) { } //~ ERROR lifetimes cannot use keyword names
+//~^ ERROR lifetimes cannot use keyword names
 fn zab<'self>(a: &'self isize) { } //~ ERROR lifetimes cannot use keyword names
-
+//~^ ERROR lifetimes cannot use keyword names
 fn main() { }