Skip to content

Commit 0eabb1b

Browse files
committed
Move stuff to methods on Ident and Symbol
1 parent d55832f commit 0eabb1b

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

src/librustc_passes/ast_validation.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc::session::Session;
2121
use syntax::ast::*;
2222
use syntax::attr;
2323
use syntax::codemap::Spanned;
24-
use syntax::parse::token;
2524
use syntax::symbol::keywords;
2625
use syntax::visit::{self, Visitor};
2726
use syntax_pos::Span;
@@ -40,14 +39,15 @@ impl<'a> AstValidator<'a> {
4039
let valid_names = [keywords::UnderscoreLifetime.name(),
4140
keywords::StaticLifetime.name(),
4241
keywords::Invalid.name()];
43-
if !valid_names.contains(&ident.name) &&
44-
token::is_reserved_ident(ident.without_first_quote()) {
42+
if !valid_names.contains(&ident.name)
43+
&& ident.without_first_quote().is_reserved()
44+
{
4545
self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
4646
}
4747
}
4848

4949
fn check_label(&self, ident: Ident) {
50-
if token::is_reserved_ident(ident.without_first_quote()) {
50+
if ident.without_first_quote().is_reserved() {
5151
self.err_handler()
5252
.span_err(ident.span, &format!("invalid label name `{}`", ident.name));
5353
}

src/libsyntax/edition.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use std::fmt;
1212
use std::str::FromStr;
13-
use symbol::{self, Symbol};
13+
use symbol::Symbol;
1414

1515
/// The edition of the compiler (RFC 2052)
1616
#[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug)]
@@ -59,8 +59,8 @@ impl Edition {
5959

6060
pub fn is_future_edition_keyword(&self, sym: Symbol) -> bool {
6161
match *self {
62-
Edition::Edition2015 => symbol::is_future_edition_keyword_2015(sym),
63-
Edition::Edition2018 => symbol::is_future_edition_keyword_2018(sym),
62+
Edition::Edition2015 => sym.is_future_edition_keyword_2015(),
63+
Edition::Edition2018 => sym.is_future_edition_keyword_2018(),
6464
}
6565
}
6666

src/libsyntax/parse/token.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,7 @@ pub fn is_path_segment_keyword(id: ast::Ident) -> bool {
145145
// How was it written originally? Did it use the raw form? Let's try to guess.
146146
pub fn is_raw_guess(ident: ast::Ident) -> bool {
147147
ident.name != keywords::Invalid.name() &&
148-
is_reserved_ident(ident) && !is_path_segment_keyword(ident)
149-
}
150-
151-
pub use syntax_pos::symbol::{is_special_ident, is_used_keyword, is_unused_keyword};
152-
153-
/// Returns `true` if the token is either a special identifier or a keyword.
154-
pub fn is_reserved_ident(id: ast::Ident) -> bool {
155-
is_special_ident(id) || is_used_keyword(id) || is_unused_keyword(id)
148+
ident.is_reserved() && !is_path_segment_keyword(ident)
156149
}
157150

158151
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug)]
@@ -399,31 +392,31 @@ impl Token {
399392
// unnamed method parameters, crate root module, error recovery etc.
400393
pub fn is_special_ident(&self) -> bool {
401394
match self.ident() {
402-
Some((id, false)) => is_special_ident(id),
395+
Some((id, false)) => id.is_special(),
403396
_ => false,
404397
}
405398
}
406399

407400
/// Returns `true` if the token is a keyword used in the language.
408401
pub fn is_used_keyword(&self) -> bool {
409402
match self.ident() {
410-
Some((id, false)) => is_used_keyword(id),
403+
Some((id, false)) => id.is_used_keyword(),
411404
_ => false,
412405
}
413406
}
414407

415408
/// Returns `true` if the token is a keyword reserved for possible future use.
416409
pub fn is_unused_keyword(&self) -> bool {
417410
match self.ident() {
418-
Some((id, false)) => is_unused_keyword(id),
411+
Some((id, false)) => id.is_unused_keyword(),
419412
_ => false,
420413
}
421414
}
422415

423416
/// Returns `true` if the token is either a special identifier or a keyword.
424417
pub fn is_reserved_ident(&self) -> bool {
425418
match self.ident() {
426-
Some((id, false)) => is_reserved_ident(id),
419+
Some((id, false)) => id.is_reserved(),
427420
_ => false,
428421
}
429422
}

src/libsyntax_pos/symbol.rs

+40-19
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ macro_rules! declare_keywords {(
278278
// NB: leaving holes in the ident table is bad! a different ident will get
279279
// interned with the id from the hole, but it will be between the min and max
280280
// of the reserved words, and thus tagged as "reserved".
281-
// After modifying this list adjust `is_special_ident`, `is_used_keyword`/`is_unused_keyword`,
282-
// this should be rarely necessary though if the keywords are kept in alphabetic order.
281+
// After modifying this list adjust `is_special`, `is_used_keyword`/`is_unused_keyword`
282+
// velow
283283
declare_keywords! {
284284
// Special reserved identifiers used internally for elided lifetimes,
285285
// unnamed method parameters, crate root module, error recovery etc.
@@ -363,28 +363,49 @@ declare_keywords! {
363363
(63, Union, "union")
364364
}
365365

366-
// Returns true for reserved identifiers used internally for elided lifetimes,
367-
// unnamed method parameters, crate root module, error recovery etc.
368-
pub fn is_special_ident(id: Ident) -> bool {
369-
id.name <= self::keywords::Underscore.name()
370-
}
366+
impl Ident {
367+
// Returns true for reserved identifiers used internally for elided lifetimes,
368+
// unnamed method parameters, crate root module, error recovery etc.
369+
#[inline]
370+
pub fn is_special(self) -> bool {
371+
self.name <= self::keywords::Underscore.name()
372+
}
371373

372-
/// Returns `true` if the token is a keyword used in the language.
373-
pub fn is_used_keyword(id: Ident) -> bool {
374-
id.name >= self::keywords::As.name() && id.name <= self::keywords::While.name()
375-
}
374+
/// Returns `true` if the token is a keyword used in the language, for
375+
/// at least one edition.
376+
///
377+
/// Keywords from future editions will be lexed as if they were raw identifiers
378+
/// so they will not reach this step.
379+
#[inline]
380+
pub fn is_used_keyword(self) -> bool {
381+
self.name >= self::keywords::As.name() && self.name <= self::keywords::While.name()
382+
}
383+
384+
/// Returns `true` if the token is a keyword reserved for possible future use, for
385+
/// at least one edition.
386+
#[inline]
387+
pub fn is_unused_keyword(self) -> bool {
388+
self.name >= self::keywords::Abstract.name() && self.name <= self::keywords::Async.name()
389+
}
376390

377-
/// Returns `true` if the token is a keyword reserved for possible future use.
378-
pub fn is_unused_keyword(id: Ident) -> bool {
379-
id.name >= self::keywords::Abstract.name() && id.name <= self::keywords::Async.name()
380-
}
381391

382-
pub fn is_future_edition_keyword_2015(sym: Symbol) -> bool {
383-
sym == self::keywords::Async.name()
392+
/// Returns `true` if the token is either a special identifier or a keyword.
393+
#[inline]
394+
pub fn is_reserved(self) -> bool {
395+
self.is_special() || self.is_used_keyword() || self.is_unused_keyword()
396+
}
384397
}
385398

386-
pub fn is_future_edition_keyword_2018(_: Symbol) -> bool {
387-
false
399+
impl Symbol {
400+
#[inline]
401+
pub fn is_future_edition_keyword_2015(self) -> bool {
402+
self == self::keywords::Async.name()
403+
}
404+
405+
#[inline]
406+
pub fn is_future_edition_keyword_2018(self) -> bool {
407+
false
408+
}
388409
}
389410

390411
// If an interner exists, return it. Otherwise, prepare a fresh one.

0 commit comments

Comments
 (0)