Skip to content

Commit c4352ff

Browse files
committed
Turn some functions from token.rs into methods on Ident
1 parent f4cbc23 commit c4352ff

File tree

10 files changed

+80
-67
lines changed

10 files changed

+80
-67
lines changed

src/librustc/hir/print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::AnnNode::*;
1313
use rustc_target::spec::abi::Abi;
1414
use syntax::ast;
1515
use syntax::codemap::{CodeMap, Spanned};
16-
use syntax::parse::{token, ParseSess};
16+
use syntax::parse::ParseSess;
1717
use syntax::parse::lexer::comments;
1818
use syntax::print::pp::{self, Breaks};
1919
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
@@ -1559,7 +1559,7 @@ impl<'a> State<'a> {
15591559
}
15601560

15611561
pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1562-
if token::is_raw_guess(ast::Ident::with_empty_ctxt(name)) {
1562+
if name.to_ident().is_raw_guess() {
15631563
self.s.word(&format!("r#{}", name))?;
15641564
} else {
15651565
self.s.word(&name.as_str())?;

src/librustc_passes/ast_validation.rs

+2-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,13 @@ 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) && ident.without_first_quote().is_reserved() {
4543
self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
4644
}
4745
}
4846

4947
fn check_label(&self, ident: Ident) {
50-
if token::is_reserved_ident(ident.without_first_quote()) {
48+
if ident.without_first_quote().is_reserved() {
5149
self.err_handler()
5250
.span_err(ident.span, &format!("invalid label name `{}`", ident.name));
5351
}

src/librustc_resolve/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
5858
use syntax::ast::{Label, Local, Mutability, Pat, PatKind, Path};
5959
use syntax::ast::{QSelf, TraitItemKind, TraitRef, Ty, TyKind};
6060
use syntax::feature_gate::{feature_err, GateIssue};
61-
use syntax::parse::token;
6261
use syntax::ptr::P;
6362

6463
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
@@ -3274,7 +3273,7 @@ impl<'a> Resolver<'a> {
32743273
// `$crate::a::b`
32753274
module = Some(self.resolve_crate_root(ident.span.ctxt(), true));
32763275
continue
3277-
} else if i == 1 && !token::is_path_segment_keyword(ident) {
3276+
} else if i == 1 && !ident.is_path_segment_keyword() {
32783277
let prev_name = path[0].name;
32793278
if prev_name == keywords::Extern.name() ||
32803279
prev_name == keywords::CrateRoot.name() &&

src/librustc_resolve/resolve_imports.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc::util::nodemap::{FxHashMap, FxHashSet};
2727
use syntax::ast::{Ident, Name, NodeId};
2828
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
2929
use syntax::ext::hygiene::Mark;
30-
use syntax::parse::token;
3130
use syntax::symbol::keywords;
3231
use syntax::util::lev_distance::find_best_match_for_name;
3332
use syntax_pos::Span;
@@ -667,7 +666,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
667666
} else {
668667
Some(self.resolve_crate_root(source.span.ctxt().modern(), false))
669668
}
670-
} else if is_extern && !token::is_path_segment_keyword(source) {
669+
} else if is_extern && !source.is_path_segment_keyword() {
671670
let crate_id =
672671
self.resolver.crate_loader.process_use_extern(
673672
source.name,
@@ -715,8 +714,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
715714
}
716715
PathResult::Failed(span, msg, true) => {
717716
let (mut self_path, mut self_result) = (module_path.clone(), None);
718-
let is_special = |ident| token::is_path_segment_keyword(ident) &&
719-
ident.name != keywords::CrateRoot.name();
717+
let is_special = |ident: Ident| ident.is_path_segment_keyword() &&
718+
ident.name != keywords::CrateRoot.name();
720719
if !self_path.is_empty() && !is_special(self_path[0]) &&
721720
!(self_path.len() > 1 && is_special(self_path[1])) {
722721
self_path[0].name = keywords::SelfValue.name();

src/libsyntax/ast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ impl Path {
107107
// or starts with something like `self`/`super`/`$crate`/etc.
108108
pub fn make_root(&self) -> Option<PathSegment> {
109109
if let Some(ident) = self.segments.get(0).map(|seg| seg.ident) {
110-
if ::parse::token::is_path_segment_keyword(ident) &&
111-
ident.name != keywords::Crate.name() {
110+
if ident.is_path_segment_keyword() && ident.name != keywords::Crate.name() {
112111
return None;
113112
}
114113
}

src/libsyntax/parse/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ impl<'a> StringReader<'a> {
11491149
return Ok(self.with_str_from(start, |string| {
11501150
// FIXME: perform NFKC normalization here. (Issue #2253)
11511151
let ident = self.mk_ident(string);
1152-
if is_raw_ident && (token::is_path_segment_keyword(ident) ||
1152+
if is_raw_ident && (ident.is_path_segment_keyword() ||
11531153
ident.name == keywords::Underscore.name()) {
11541154
self.fatal_span_(raw_start, self.pos,
11551155
&format!("`r#{}` is not currently supported.", ident.name)

src/libsyntax/parse/token.rs

+7-50
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ pub use self::Lit::*;
1515
pub use self::Token::*;
1616

1717
use ast::{self};
18-
use edition::Edition;
1918
use parse::ParseSess;
2019
use print::pprust;
2120
use ptr::P;
2221
use serialize::{Decodable, Decoder, Encodable, Encoder};
2322
use symbol::keywords;
2423
use syntax::parse::parse_stream_from_source_str;
25-
use syntax_pos::{self, hygiene, Span, FileName};
24+
use syntax_pos::{self, Span, FileName};
2625
use tokenstream::{TokenStream, TokenTree};
2726
use tokenstream;
2827

@@ -139,48 +138,6 @@ fn ident_can_begin_type(ident: ast::Ident, is_raw: bool) -> bool {
139138
].contains(&ident.name)
140139
}
141140

142-
pub fn is_path_segment_keyword(id: ast::Ident) -> bool {
143-
id.name == keywords::Super.name() ||
144-
id.name == keywords::SelfValue.name() ||
145-
id.name == keywords::SelfType.name() ||
146-
id.name == keywords::Extern.name() ||
147-
id.name == keywords::Crate.name() ||
148-
id.name == keywords::CrateRoot.name() ||
149-
id.name == keywords::DollarCrate.name()
150-
}
151-
152-
// We see this identifier in a normal identifier position, like variable name or a type.
153-
// How was it written originally? Did it use the raw form? Let's try to guess.
154-
pub fn is_raw_guess(ident: ast::Ident) -> bool {
155-
ident.name != keywords::Invalid.name() &&
156-
is_reserved_ident(ident) && !is_path_segment_keyword(ident)
157-
}
158-
159-
// Returns true for reserved identifiers used internally for elided lifetimes,
160-
// unnamed method parameters, crate root module, error recovery etc.
161-
pub fn is_special_ident(id: ast::Ident) -> bool {
162-
id.name <= keywords::Underscore.name()
163-
}
164-
165-
/// Returns `true` if the token is a keyword used in the language.
166-
pub fn is_used_keyword(id: ast::Ident) -> bool {
167-
id.name >= keywords::As.name() && id.name <= keywords::While.name()
168-
}
169-
170-
/// Returns `true` if the token is a keyword reserved for possible future use.
171-
pub fn is_unused_keyword(id: ast::Ident) -> bool {
172-
let edition = || id.span.ctxt().outer().expn_info().map_or_else(|| hygiene::default_edition(),
173-
|einfo| einfo.callee.edition);
174-
id.name >= keywords::Abstract.name() && id.name <= keywords::Yield.name() ||
175-
id.name == keywords::Proc.name() && edition() == Edition::Edition2015 ||
176-
id.name == keywords::Async.name() && edition() == Edition::Edition2018
177-
}
178-
179-
/// Returns `true` if the token is either a special identifier or a keyword.
180-
pub fn is_reserved_ident(id: ast::Ident) -> bool {
181-
is_special_ident(id) || is_used_keyword(id) || is_unused_keyword(id)
182-
}
183-
184141
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug)]
185142
pub enum Token {
186143
/* Expression-operator symbols. */
@@ -256,7 +213,7 @@ impl Token {
256213

257214
/// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
258215
pub fn from_ast_ident(ident: ast::Ident) -> Token {
259-
Ident(ident, is_raw_guess(ident))
216+
Ident(ident, ident.is_raw_guess())
260217
}
261218

262219
/// Returns `true` if the token starts with '>'.
@@ -436,7 +393,7 @@ impl Token {
436393

437394
pub fn is_path_segment_keyword(&self) -> bool {
438395
match self.ident() {
439-
Some((id, false)) => is_path_segment_keyword(id),
396+
Some((id, false)) => id.is_path_segment_keyword(),
440397
_ => false,
441398
}
442399
}
@@ -445,31 +402,31 @@ impl Token {
445402
// unnamed method parameters, crate root module, error recovery etc.
446403
pub fn is_special_ident(&self) -> bool {
447404
match self.ident() {
448-
Some((id, false)) => is_special_ident(id),
405+
Some((id, false)) => id.is_special(),
449406
_ => false,
450407
}
451408
}
452409

453410
/// Returns `true` if the token is a keyword used in the language.
454411
pub fn is_used_keyword(&self) -> bool {
455412
match self.ident() {
456-
Some((id, false)) => is_used_keyword(id),
413+
Some((id, false)) => id.is_used_keyword(),
457414
_ => false,
458415
}
459416
}
460417

461418
/// Returns `true` if the token is a keyword reserved for possible future use.
462419
pub fn is_unused_keyword(&self) -> bool {
463420
match self.ident() {
464-
Some((id, false)) => is_unused_keyword(id),
421+
Some((id, false)) => id.is_unused_keyword(),
465422
_ => false,
466423
}
467424
}
468425

469426
/// Returns `true` if the token is either a special identifier or a keyword.
470427
pub fn is_reserved_ident(&self) -> bool {
471428
match self.ident() {
472-
Some((id, false)) => is_reserved_ident(id),
429+
Some((id, false)) => id.is_reserved(),
473430
_ => false,
474431
}
475432
}

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ impl<'a> State<'a> {
23742374
}
23752375

23762376
pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
2377-
if token::is_raw_guess(ident) {
2377+
if ident.is_raw_guess() {
23782378
self.s.word(&format!("r#{}", ident))?;
23792379
} else {
23802380
self.s.word(&ident.name.as_str())?;

src/libsyntax_pos/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ impl Span {
300300
self.ctxt().outer().expn_info().map(|i| i.call_site)
301301
}
302302

303+
/// Edition of the crate from which this span came.
304+
pub fn edition(self) -> edition::Edition {
305+
self.ctxt().outer().expn_info().map_or_else(|| hygiene::default_edition(),
306+
|einfo| einfo.callee.edition)
307+
}
308+
303309
/// Return the source callee.
304310
///
305311
/// Returns None if the supplied span has no expansion trace,

src/libsyntax_pos/symbol.rs

+56-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! allows bidirectional lookup; i.e. given a value, one can easily find the
1313
//! type, and vice versa.
1414
15+
use edition::Edition;
1516
use hygiene::SyntaxContext;
1617
use {Span, DUMMY_SP, GLOBALS};
1718

@@ -318,7 +319,7 @@ macro_rules! declare_keywords {(
318319
// NB: leaving holes in the ident table is bad! a different ident will get
319320
// interned with the id from the hole, but it will be between the min and max
320321
// of the reserved words, and thus tagged as "reserved".
321-
// After modifying this list adjust `is_special_ident`, `is_used_keyword`/`is_unused_keyword`,
322+
// After modifying this list adjust `is_special`, `is_used_keyword`/`is_unused_keyword`,
322323
// this should be rarely necessary though if the keywords are kept in alphabetic order.
323324
declare_keywords! {
324325
// Special reserved identifiers used internally for elided lifetimes,
@@ -399,6 +400,60 @@ declare_keywords! {
399400
(63, Union, "union")
400401
}
401402

403+
impl Symbol {
404+
fn is_unused_keyword_2015(self) -> bool {
405+
self == keywords::Proc.name()
406+
}
407+
408+
fn is_unused_keyword_2018(self) -> bool {
409+
self == keywords::Async.name()
410+
}
411+
}
412+
413+
impl Ident {
414+
// Returns true for reserved identifiers used internally for elided lifetimes,
415+
// unnamed method parameters, crate root module, error recovery etc.
416+
pub fn is_special(self) -> bool {
417+
self.name <= keywords::Underscore.name()
418+
}
419+
420+
/// Returns `true` if the token is a keyword used in the language.
421+
pub fn is_used_keyword(self) -> bool {
422+
self.name >= keywords::As.name() && self.name <= keywords::While.name()
423+
}
424+
425+
/// Returns `true` if the token is a keyword reserved for possible future use.
426+
pub fn is_unused_keyword(self) -> bool {
427+
// Note: `span.edition()` is relatively expensive, don't call it unless necessary.
428+
self.name >= keywords::Abstract.name() && self.name <= keywords::Yield.name() ||
429+
self.name.is_unused_keyword_2015() && self.span.edition() == Edition::Edition2015 ||
430+
self.name.is_unused_keyword_2018() && self.span.edition() == Edition::Edition2018
431+
}
432+
433+
/// Returns `true` if the token is either a special identifier or a keyword.
434+
pub fn is_reserved(self) -> bool {
435+
self.is_special() || self.is_used_keyword() || self.is_unused_keyword()
436+
}
437+
438+
/// A keyword or reserved identifier that can be used as a path segment.
439+
pub fn is_path_segment_keyword(self) -> bool {
440+
self.name == keywords::Super.name() ||
441+
self.name == keywords::SelfValue.name() ||
442+
self.name == keywords::SelfType.name() ||
443+
self.name == keywords::Extern.name() ||
444+
self.name == keywords::Crate.name() ||
445+
self.name == keywords::CrateRoot.name() ||
446+
self.name == keywords::DollarCrate.name()
447+
}
448+
449+
// We see this identifier in a normal identifier position, like variable name or a type.
450+
// How was it written originally? Did it use the raw form? Let's try to guess.
451+
pub fn is_raw_guess(self) -> bool {
452+
self.name != keywords::Invalid.name() &&
453+
self.is_reserved() && !self.is_path_segment_keyword()
454+
}
455+
}
456+
402457
// If an interner exists, return it. Otherwise, prepare a fresh one.
403458
#[inline]
404459
fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {

0 commit comments

Comments
 (0)