Skip to content

Commit d55832f

Browse files
committed
Add edition keywords, use for async/await
fixes #49610
1 parent 4607cc6 commit d55832f

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

src/libsyntax/edition.rs

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

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

1415
/// The edition of the compiler (RFC 2052)
1516
#[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug)]
@@ -56,6 +57,13 @@ impl Edition {
5657
}
5758
}
5859

60+
pub fn is_future_edition_keyword(&self, sym: Symbol) -> bool {
61+
match *self {
62+
Edition::Edition2015 => symbol::is_future_edition_keyword_2015(sym),
63+
Edition::Edition2018 => symbol::is_future_edition_keyword_2018(sym),
64+
}
65+
}
66+
5967
pub fn feature_name(&self) -> &'static str {
6068
match *self {
6169
Edition::Edition2015 => "rust_2015_preview",

src/libsyntax/parse/lexer/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ impl<'a> StringReader<'a> {
11171117
let c = self.ch;
11181118

11191119
if ident_start(c) {
1120-
let (is_ident_start, is_raw_ident) =
1120+
let (is_ident_start, mut is_raw_ident) =
11211121
match (c.unwrap(), self.nextch(), self.nextnextch()) {
11221122
// r# followed by an identifier starter is a raw identifier.
11231123
// This is an exception to the r# case below.
@@ -1155,9 +1155,13 @@ impl<'a> StringReader<'a> {
11551155
&format!("`r#{}` is not currently supported.", ident.name)
11561156
).raise();
11571157
}
1158+
11581159
if is_raw_ident {
11591160
let span = self.mk_sp(raw_start, self.pos);
11601161
self.sess.raw_identifier_spans.borrow_mut().push(span);
1162+
} else if self.sess.edition.is_future_edition_keyword(ident.name) {
1163+
// we pretend these are raw even if they aren't
1164+
is_raw_ident = true;
11611165
}
11621166
token::Ident(ident, is_raw_ident)
11631167
}));

src/libsyntax/parse/token.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,7 @@ pub fn is_raw_guess(ident: ast::Ident) -> bool {
148148
is_reserved_ident(ident) && !is_path_segment_keyword(ident)
149149
}
150150

151-
// Returns true for reserved identifiers used internally for elided lifetimes,
152-
// unnamed method parameters, crate root module, error recovery etc.
153-
pub fn is_special_ident(id: ast::Ident) -> bool {
154-
id.name <= keywords::Underscore.name()
155-
}
156-
157-
/// Returns `true` if the token is a keyword used in the language.
158-
pub fn is_used_keyword(id: ast::Ident) -> bool {
159-
id.name >= keywords::As.name() && id.name <= keywords::While.name()
160-
}
161-
162-
/// Returns `true` if the token is a keyword reserved for possible future use.
163-
pub fn is_unused_keyword(id: ast::Ident) -> bool {
164-
id.name >= keywords::Abstract.name() && id.name <= keywords::Yield.name()
165-
}
151+
pub use syntax_pos::symbol::{is_special_ident, is_used_keyword, is_unused_keyword};
166152

167153
/// Returns `true` if the token is either a special identifier or a keyword.
168154
pub fn is_reserved_ident(id: ast::Ident) -> bool {

src/libsyntax_pos/symbol.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ declare_keywords! {
325325
(37, Use, "use")
326326
(38, Where, "where")
327327
(39, While, "while")
328+
// edition-gated used keywords go here
329+
// be sure to update is_used_keyword and
330+
// is_future_edition_keyword_* below
328331

329332
// Keywords reserved for future use.
330333
(40, Abstract, "abstract")
@@ -343,17 +346,45 @@ declare_keywords! {
343346
(53, Unsized, "unsized")
344347
(54, Virtual, "virtual")
345348
(55, Yield, "yield")
349+
// edition-gated reserved keywords
350+
// be sure to update is_unused_keyword and
351+
// is_future_edition_keyword_* below
352+
(56, Async, "async") // Rust 2018+ only
346353

347354
// Special lifetime names
348-
(56, UnderscoreLifetime, "'_")
349-
(57, StaticLifetime, "'static")
355+
(57, UnderscoreLifetime, "'_")
356+
(58, StaticLifetime, "'static")
350357

351358
// Weak keywords, have special meaning only in specific contexts.
352-
(58, Auto, "auto")
353-
(59, Catch, "catch")
354-
(60, Default, "default")
355-
(61, Dyn, "dyn")
356-
(62, Union, "union")
359+
(59, Auto, "auto")
360+
(60, Catch, "catch")
361+
(61, Default, "default")
362+
(62, Dyn, "dyn")
363+
(63, Union, "union")
364+
}
365+
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+
}
371+
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+
}
376+
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+
}
381+
382+
pub fn is_future_edition_keyword_2015(sym: Symbol) -> bool {
383+
sym == self::keywords::Async.name()
384+
}
385+
386+
pub fn is_future_edition_keyword_2018(_: Symbol) -> bool {
387+
false
357388
}
358389

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

0 commit comments

Comments
 (0)