Skip to content

Commit 1c90609

Browse files
committed
Add try to syntax_pos as an edition-2018-only keyword
1 parent f2445fb commit 1c90609

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/libsyntax_pos/symbol.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,23 +413,30 @@ declare_keywords! {
413413
(49, Virtual, "virtual")
414414
(50, Yield, "yield")
415415

416+
// Edition-specific keywords currently in use.
417+
(51, Try, "try") // >= 2018 Edition Only
418+
416419
// Edition-specific keywords reserved for future use.
417-
(51, Async, "async") // >= 2018 Edition Only
420+
(52, Async, "async") // >= 2018 Edition Only
418421

419422
// Special lifetime names
420-
(52, UnderscoreLifetime, "'_")
421-
(53, StaticLifetime, "'static")
423+
(53, UnderscoreLifetime, "'_")
424+
(54, StaticLifetime, "'static")
422425

423426
// Weak keywords, have special meaning only in specific contexts.
424-
(54, Auto, "auto")
425-
(55, Catch, "catch")
426-
(56, Default, "default")
427-
(57, Dyn, "dyn")
428-
(58, Union, "union")
429-
(59, Existential, "existential")
427+
(55, Auto, "auto")
428+
(56, Catch, "catch")
429+
(57, Default, "default")
430+
(58, Dyn, "dyn")
431+
(59, Union, "union")
432+
(60, Existential, "existential")
430433
}
431434

432435
impl Symbol {
436+
fn is_used_keyword_2018(self) -> bool {
437+
self == keywords::Try.name()
438+
}
439+
433440
fn is_unused_keyword_2018(self) -> bool {
434441
self == keywords::Async.name()
435442
}
@@ -444,7 +451,9 @@ impl Ident {
444451

445452
/// Returns `true` if the token is a keyword used in the language.
446453
pub fn is_used_keyword(self) -> bool {
447-
self.name >= keywords::As.name() && self.name <= keywords::While.name()
454+
// Note: `span.edition()` is relatively expensive, don't call it unless necessary.
455+
self.name >= keywords::As.name() && self.name <= keywords::While.name() ||
456+
self.name.is_used_keyword_2018() && self.span.edition() == Edition::Edition2018
448457
}
449458

450459
/// Returns `true` if the token is a keyword reserved for possible future use.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only --edition 2018
12+
13+
fn main() {
14+
let try = "foo"; //~ error: expected pattern, found keyword `try`
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --edition 2015
12+
13+
fn main() {
14+
let try = 2;
15+
struct try { try: u32 };
16+
let try: try = try { try };
17+
assert_eq!(try.try, 2);
18+
}

0 commit comments

Comments
 (0)