Skip to content

Commit

Permalink
Add try to syntax_pos as an edition-2018-only keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Aug 19, 2018
1 parent f2445fb commit 1c90609
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,23 +413,30 @@ declare_keywords! {
(49, Virtual, "virtual")
(50, Yield, "yield")

// Edition-specific keywords currently in use.
(51, Try, "try") // >= 2018 Edition Only

// Edition-specific keywords reserved for future use.
(51, Async, "async") // >= 2018 Edition Only
(52, Async, "async") // >= 2018 Edition Only

// Special lifetime names
(52, UnderscoreLifetime, "'_")
(53, StaticLifetime, "'static")
(53, UnderscoreLifetime, "'_")
(54, StaticLifetime, "'static")

// Weak keywords, have special meaning only in specific contexts.
(54, Auto, "auto")
(55, Catch, "catch")
(56, Default, "default")
(57, Dyn, "dyn")
(58, Union, "union")
(59, Existential, "existential")
(55, Auto, "auto")
(56, Catch, "catch")
(57, Default, "default")
(58, Dyn, "dyn")
(59, Union, "union")
(60, Existential, "existential")
}

impl Symbol {
fn is_used_keyword_2018(self) -> bool {
self == keywords::Try.name()
}

fn is_unused_keyword_2018(self) -> bool {
self == keywords::Async.name()
}
Expand All @@ -444,7 +451,9 @@ impl Ident {

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

/// Returns `true` if the token is a keyword reserved for possible future use.
Expand Down
15 changes: 15 additions & 0 deletions src/test/parse-fail/keyword-try-as-identifier-edition2018.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z parse-only --edition 2018

fn main() {
let try = "foo"; //~ error: expected pattern, found keyword `try`
}
18 changes: 18 additions & 0 deletions src/test/run-pass/try-is-identifier-edition2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: --edition 2015

fn main() {
let try = 2;
struct try { try: u32 };
let try: try = try { try };
assert_eq!(try.try, 2);
}

0 comments on commit 1c90609

Please sign in to comment.