Skip to content

Commit b33fd6d

Browse files
committed
Change some terminology around keywords and reserved identifiers
1 parent e03948e commit b33fd6d

File tree

7 files changed

+100
-110
lines changed

7 files changed

+100
-110
lines changed

src/librustdoc/html/highlight.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'a> Classifier<'a> {
300300
"Some" | "None" | "Ok" | "Err" => Class::PreludeVal,
301301

302302
"$crate" => Class::KeyWord,
303-
_ if tas.tok.is_any_keyword() => Class::KeyWord,
303+
_ if tas.tok.is_reserved_ident() => Class::KeyWord,
304304

305305
_ => {
306306
if self.in_macro_nonterminal {

src/libsyntax/parse/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ impl<'a> StringReader<'a> {
12831283
});
12841284
let keyword_checking_token = &token::Ident(keyword_checking_ident);
12851285
let last_bpos = self.pos;
1286-
if keyword_checking_token.is_any_keyword() &&
1286+
if keyword_checking_token.is_reserved_ident() &&
12871287
!keyword_checking_token.is_keyword(keywords::Static) {
12881288
self.err_span_(start, last_bpos, "lifetimes cannot use keyword names");
12891289
}

src/libsyntax/parse/parser.rs

+14-32
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,13 @@ impl<'a> Parser<'a> {
511511
}
512512

513513
pub fn this_token_descr(&self) -> String {
514-
let s = self.this_token_to_string();
515-
if self.token.is_strict_keyword() {
516-
format!("keyword `{}`", s)
517-
} else if self.token.is_reserved_keyword() {
518-
format!("reserved keyword `{}`", s)
519-
} else {
520-
format!("`{}`", s)
521-
}
514+
let prefix = match &self.token {
515+
t if t.is_special_ident() => "reserved identifier ",
516+
t if t.is_used_keyword() => "keyword ",
517+
t if t.is_unused_keyword() => "reserved keyword ",
518+
_ => "",
519+
};
520+
format!("{}`{}`", prefix, self.this_token_to_string())
522521
}
523522

524523
pub fn unexpected_last<T>(&self, t: &token::Token) -> PResult<'a, T> {
@@ -637,10 +636,12 @@ impl<'a> Parser<'a> {
637636
}
638637

639638
pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
640-
self.check_strict_keywords();
641-
self.check_reserved_keywords();
642639
match self.token {
643640
token::Ident(i) => {
641+
if self.token.is_reserved_ident() {
642+
self.span_err(self.span, &format!("expected identifier, found {}",
643+
self.this_token_descr()));
644+
}
644645
self.bump();
645646
Ok(i)
646647
}
@@ -713,25 +714,6 @@ impl<'a> Parser<'a> {
713714
}
714715
}
715716

716-
/// Signal an error if the given string is a strict keyword
717-
pub fn check_strict_keywords(&mut self) {
718-
if self.token.is_strict_keyword() {
719-
let token_str = self.this_token_to_string();
720-
let span = self.span;
721-
self.span_err(span,
722-
&format!("expected identifier, found keyword `{}`",
723-
token_str));
724-
}
725-
}
726-
727-
/// Signal an error if the current token is a reserved keyword
728-
pub fn check_reserved_keywords(&mut self) {
729-
if self.token.is_reserved_keyword() {
730-
let token_str = self.this_token_to_string();
731-
self.fatal(&format!("`{}` is a reserved keyword", token_str)).emit()
732-
}
733-
}
734-
735717
fn check_ident(&mut self) -> bool {
736718
if self.token.is_ident() {
737719
true
@@ -2301,7 +2283,7 @@ impl<'a> Parser<'a> {
23012283
ex = ExprKind::Break(lt, e);
23022284
hi = self.prev_span;
23032285
} else if self.token.is_keyword(keywords::Let) {
2304-
// Catch this syntax error here, instead of in `check_strict_keywords`, so
2286+
// Catch this syntax error here, instead of in `parse_ident`, so
23052287
// that we can explicitly mention that let is not to be used as an expression
23062288
let mut db = self.fatal("expected expression, found statement (`let`)");
23072289
db.note("variable declaration using `let` is a statement");
@@ -3540,7 +3522,7 @@ impl<'a> Parser<'a> {
35403522
// Parse box pat
35413523
let subpat = self.parse_pat()?;
35423524
pat = PatKind::Box(subpat);
3543-
} else if self.token.is_ident() && !self.token.is_any_keyword() &&
3525+
} else if self.token.is_ident() && !self.token.is_reserved_ident() &&
35443526
self.parse_as_ident() {
35453527
// Parse ident @ pat
35463528
// This can give false positives and parse nullary enums,
@@ -3815,7 +3797,7 @@ impl<'a> Parser<'a> {
38153797

38163798
fn is_union_item(&self) -> bool {
38173799
self.token.is_keyword(keywords::Union) &&
3818-
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword())
3800+
self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
38193801
}
38203802

38213803
fn is_defaultness(&self) -> bool {

src/libsyntax/parse/token.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Lit {
8787
fn ident_can_begin_expr(ident: ast::Ident) -> bool {
8888
let ident_token: Token = Ident(ident);
8989

90-
!ident_token.is_any_keyword() ||
90+
!ident_token.is_reserved_ident() ||
9191
ident_token.is_path_segment_keyword() ||
9292
[
9393
keywords::Do.name(),
@@ -110,7 +110,7 @@ fn ident_can_begin_expr(ident: ast::Ident) -> bool {
110110
fn ident_can_begin_type(ident: ast::Ident) -> bool {
111111
let ident_token: Token = Ident(ident);
112112

113-
!ident_token.is_any_keyword() ||
113+
!ident_token.is_reserved_ident() ||
114114
ident_token.is_path_segment_keyword() ||
115115
[
116116
keywords::For.name(),
@@ -315,7 +315,7 @@ impl Token {
315315

316316
pub fn is_path_start(&self) -> bool {
317317
self == &ModSep || self.is_qpath_start() || self.is_path() ||
318-
self.is_path_segment_keyword() || self.is_ident() && !self.is_any_keyword()
318+
self.is_path_segment_keyword() || self.is_ident() && !self.is_reserved_ident()
319319
}
320320

321321
/// Returns `true` if the token is a given keyword, `kw`.
@@ -333,26 +333,35 @@ impl Token {
333333
}
334334
}
335335

336-
/// Returns `true` if the token is either a strict or reserved keyword.
337-
pub fn is_any_keyword(&self) -> bool {
338-
self.is_strict_keyword() || self.is_reserved_keyword()
336+
// Returns true for reserved identifiers used internally for elided lifetimes,
337+
// unnamed method parameters, crate root module, error recovery etc.
338+
pub fn is_special_ident(&self) -> bool {
339+
match self.ident() {
340+
Some(id) => id.name <= keywords::DollarCrate.name(),
341+
_ => false,
342+
}
339343
}
340344

341-
/// Returns `true` if the token is a strict keyword.
342-
pub fn is_strict_keyword(&self) -> bool {
345+
/// Returns `true` if the token is a keyword used in the language.
346+
pub fn is_used_keyword(&self) -> bool {
343347
match self.ident() {
344348
Some(id) => id.name >= keywords::As.name() && id.name <= keywords::While.name(),
345349
_ => false,
346350
}
347351
}
348352

349353
/// Returns `true` if the token is a keyword reserved for possible future use.
350-
pub fn is_reserved_keyword(&self) -> bool {
354+
pub fn is_unused_keyword(&self) -> bool {
351355
match self.ident() {
352356
Some(id) => id.name >= keywords::Abstract.name() && id.name <= keywords::Yield.name(),
353357
_ => false,
354358
}
355359
}
360+
361+
/// Returns `true` if the token is either a special identifier or a keyword.
362+
pub fn is_reserved_ident(&self) -> bool {
363+
self.is_special_ident() || self.is_used_keyword() || self.is_unused_keyword()
364+
}
356365
}
357366

358367
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash)]

src/libsyntax_pos/symbol.rs

+63-64
Original file line numberDiff line numberDiff line change
@@ -237,77 +237,76 @@ macro_rules! declare_keywords {(
237237
// NB: leaving holes in the ident table is bad! a different ident will get
238238
// interned with the id from the hole, but it will be between the min and max
239239
// of the reserved words, and thus tagged as "reserved".
240-
// After modifying this list adjust `is_strict_keyword`/`is_reserved_keyword`,
240+
// After modifying this list adjust `is_special_ident`, `is_used_keyword`/`is_unused_keyword`,
241241
// this should be rarely necessary though if the keywords are kept in alphabetic order.
242242
declare_keywords! {
243-
// Invalid identifier
243+
// Special reserved identifiers used internally for elided lifetimes,
244+
// unnamed method parameters, crate root module, error recovery etc.
244245
(0, Invalid, "")
245-
246-
// Strict keywords used in the language.
247-
(1, As, "as")
248-
(2, Box, "box")
249-
(3, Break, "break")
250-
(4, Const, "const")
251-
(5, Continue, "continue")
252-
(6, Crate, "crate")
253-
(7, DollarCrate, "$crate")
254-
(8, Else, "else")
255-
(9, Enum, "enum")
256-
(10, Extern, "extern")
257-
(11, False, "false")
258-
(12, Fn, "fn")
259-
(13, For, "for")
260-
(14, If, "if")
261-
(15, Impl, "impl")
262-
(16, In, "in")
263-
(17, Let, "let")
264-
(18, Loop, "loop")
265-
(19, Match, "match")
266-
(20, Mod, "mod")
267-
(21, Move, "move")
268-
(22, Mut, "mut")
269-
(23, Pub, "pub")
270-
(24, Ref, "ref")
271-
(25, Return, "return")
272-
(26, SelfValue, "self")
273-
(27, SelfType, "Self")
274-
(28, Static, "static")
275-
(29, Struct, "struct")
276-
(30, Super, "super")
277-
(31, Trait, "trait")
278-
(32, True, "true")
279-
(33, Type, "type")
280-
(34, Unsafe, "unsafe")
281-
(35, Use, "use")
282-
(36, Where, "where")
283-
(37, While, "while")
246+
(1, CrateRoot, "{{root}}")
247+
(2, DollarCrate, "$crate")
248+
249+
// Keywords used in the language.
250+
(3, As, "as")
251+
(4, Box, "box")
252+
(5, Break, "break")
253+
(6, Const, "const")
254+
(7, Continue, "continue")
255+
(8, Crate, "crate")
256+
(9, Else, "else")
257+
(10, Enum, "enum")
258+
(11, Extern, "extern")
259+
(12, False, "false")
260+
(13, Fn, "fn")
261+
(14, For, "for")
262+
(15, If, "if")
263+
(16, Impl, "impl")
264+
(17, In, "in")
265+
(18, Let, "let")
266+
(19, Loop, "loop")
267+
(20, Match, "match")
268+
(21, Mod, "mod")
269+
(22, Move, "move")
270+
(23, Mut, "mut")
271+
(24, Pub, "pub")
272+
(25, Ref, "ref")
273+
(26, Return, "return")
274+
(27, SelfValue, "self")
275+
(28, SelfType, "Self")
276+
(29, Static, "static")
277+
(30, Struct, "struct")
278+
(31, Super, "super")
279+
(32, Trait, "trait")
280+
(33, True, "true")
281+
(34, Type, "type")
282+
(35, Unsafe, "unsafe")
283+
(36, Use, "use")
284+
(37, Where, "where")
285+
(38, While, "while")
284286

285287
// Keywords reserved for future use.
286-
(38, Abstract, "abstract")
287-
(39, Alignof, "alignof")
288-
(40, Become, "become")
289-
(41, Do, "do")
290-
(42, Final, "final")
291-
(43, Macro, "macro")
292-
(44, Offsetof, "offsetof")
293-
(45, Override, "override")
294-
(46, Priv, "priv")
295-
(47, Proc, "proc")
296-
(48, Pure, "pure")
297-
(49, Sizeof, "sizeof")
298-
(50, Typeof, "typeof")
299-
(51, Unsized, "unsized")
300-
(52, Virtual, "virtual")
301-
(53, Yield, "yield")
288+
(39, Abstract, "abstract")
289+
(40, Alignof, "alignof")
290+
(41, Become, "become")
291+
(42, Do, "do")
292+
(43, Final, "final")
293+
(44, Macro, "macro")
294+
(45, Offsetof, "offsetof")
295+
(46, Override, "override")
296+
(47, Priv, "priv")
297+
(48, Proc, "proc")
298+
(49, Pure, "pure")
299+
(50, Sizeof, "sizeof")
300+
(51, Typeof, "typeof")
301+
(52, Unsized, "unsized")
302+
(53, Virtual, "virtual")
303+
(54, Yield, "yield")
302304

303305
// Weak keywords, have special meaning only in specific contexts.
304-
(54, Default, "default")
305-
(55, StaticLifetime, "'static")
306-
(56, Union, "union")
307-
(57, Catch, "catch")
308-
309-
// A virtual keyword that resolves to the crate root when used in a lexical scope.
310-
(58, CrateRoot, "{{root}}")
306+
(55, Default, "default")
307+
(56, StaticLifetime, "'static")
308+
(57, Union, "union")
309+
(58, Catch, "catch")
311310
}
312311

313312
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one.

src/test/compile-fail/dollar-crate-is-keyword.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
macro_rules! m {
1212
() => {
13-
struct $crate {} //~ ERROR expected identifier, found keyword `$crate`
13+
struct $crate {} //~ ERROR expected identifier, found reserved identifier `$crate`
1414

1515
use $crate; // OK
1616
//~^ WARN `$crate` may not be imported
17-
use $crate as $crate; //~ ERROR expected identifier, found keyword `$crate`
17+
use $crate as $crate; //~ ERROR expected identifier, found reserved identifier `$crate`
1818
//~^ WARN `$crate` may not be imported
1919
}
2020
}

src/test/parse-fail/macro-keyword.rs

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

1111
// compile-flags: -Z parse-only
1212

13-
fn macro() { //~ ERROR `macro` is a reserved keyword
13+
fn macro() { //~ ERROR expected identifier, found reserved keyword `macro`
1414
}
1515

1616
pub fn main() {

0 commit comments

Comments
 (0)