-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Modify message for keyword as identifier name #46497
Changes from 1 commit
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 |
---|---|---|
|
@@ -21,7 +21,6 @@ use rustc::session::Session; | |
use syntax::ast::*; | ||
use syntax::attr; | ||
use syntax::codemap::Spanned; | ||
use syntax::symbol::keywords; | ||
use syntax::visit::{self, Visitor}; | ||
use syntax_pos::Span; | ||
use errors; | ||
|
@@ -35,8 +34,16 @@ impl<'a> AstValidator<'a> { | |
&self.session.parse_sess.span_diagnostic | ||
} | ||
|
||
fn check_lifetime(&self, lifetime: &Lifetime) { | ||
if !lifetime.ident.without_first_quote().is_valid() && | ||
!lifetime.ident.name.is_static_keyword() { | ||
self.err_handler().span_err(lifetime.span, "lifetimes cannot use keyword names"); | ||
} | ||
} | ||
|
||
fn check_label(&self, label: Ident, span: Span) { | ||
if label.name == keywords::StaticLifetime.name() || label.name == "'_" { | ||
if label.name.is_static_keyword() || !label.without_first_quote().is_valid() | ||
|| label.name == "'_" { | ||
self.err_handler().span_err(span, &format!("invalid label name `{}`", label.name)); | ||
} | ||
} | ||
|
@@ -202,19 +209,21 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
|
||
fn visit_item(&mut self, item: &'a Item) { | ||
match item.node { | ||
ItemKind::Impl(.., Some(..), _, ref impl_items) => { | ||
ItemKind::Impl(.., ref generics, 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); | ||
if let ImplItemKind::Method(ref sig, _) = impl_item.node { | ||
self.check_trait_fn_not_const(sig.constness); | ||
} | ||
} | ||
generics.lifetimes.iter().for_each(|l| self.check_lifetime(&l.lifetime)) | ||
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. It's not necessary to check lifetimes in all possible contexts manually, you can add a
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. This is actually a good first task because the visitor is a quite important piece of infrastructure and it makes you familiar with it. |
||
} | ||
ItemKind::Impl(.., None, _, _) => { | ||
ItemKind::Impl(.., ref generics, 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); | ||
|
@@ -225,13 +234,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
Some("place qualifiers on individual foreign items \ | ||
instead")); | ||
} | ||
ItemKind::Enum(ref def, _) => { | ||
ItemKind::Enum(ref def, ref generics) => { | ||
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 { | ||
|
@@ -268,6 +278,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
} | ||
} | ||
} | ||
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). | ||
|
@@ -278,7 +289,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
self.session.buffer_lint(lint, item.id, item.span, msg); | ||
} | ||
} | ||
ItemKind::Union(ref vdata, _) => { | ||
ItemKind::Union(ref vdata, ref generics) => { | ||
if !vdata.is_struct() { | ||
self.err_handler().span_err(item.span, | ||
"tuple and unit unions are not permitted"); | ||
|
@@ -287,6 +298,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> { | |
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)) | ||
} | ||
_ => {} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2012-2014 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() { | ||
'break: loop { //~ ERROR invalid label name `'break` | ||
} | ||
} |
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.
lifetime.ident.without_first_quote().is_valid()
->token::Ident(lifetime.ident).is_reserved_ident()
and all the new functions (is_valid
,is_used_keyword
,is_static_keyword
, etc) can be removed.Also
without_first_quote
is used only once so it's probably better inlined right here.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.
Invalid names for lifetimes are all
is_reserved_ident
names except forkeywords::Static
andkeywords::Invalid
.Invalid names for labels are simply all
is_reserved_ident
plus'_
.