Skip to content

Commit 9e96419

Browse files
committed
Generalize async_idents to all new keywords
This commit generalizes the existing `async_idents` lint to easily encompass other identifiers that will be keywords in future editions. The new lint is called `keyword_idents` and the old `async_idents` lint is registered as renamed to this new lint. As a proof of concept the `try` keyword was added to this list as it looks to be listed as a keyword in the 2018 edition only. The `await` keyword was not added as it's not listed as a keyword yet. Closes #53077
1 parent b638d8c commit 9e96419

14 files changed

+124
-45
lines changed

src/librustc/lint/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
10461046
self.check_id(id);
10471047
}
10481048

1049-
fn visit_mac(&mut self, mac: &'ast ast::Mac) {
1049+
fn visit_mac(&mut self, mac: &'a ast::Mac) {
10501050
run_lints!(self, check_mac, mac);
10511051
}
10521052
}

src/librustc_lint/builtin.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,30 +1884,33 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestFunctions {
18841884
}
18851885

18861886
declare_lint! {
1887-
pub ASYNC_IDENTS,
1887+
pub KEYWORD_IDENTS,
18881888
Allow,
1889-
"detects `async` being used as an identifier"
1889+
"detects edition keywords being used as an identifier"
18901890
}
18911891

1892-
/// Checks for uses of `async` as an identifier
1892+
/// Checks for uses of edtion keywords used as an identifier
18931893
#[derive(Clone)]
1894-
pub struct Async2018;
1894+
pub struct KeywordIdents;
18951895

1896-
impl LintPass for Async2018 {
1896+
impl LintPass for KeywordIdents {
18971897
fn get_lints(&self) -> LintArray {
1898-
lint_array!(ASYNC_IDENTS)
1898+
lint_array!(KEYWORD_IDENTS)
18991899
}
19001900
}
19011901

1902-
impl Async2018 {
1902+
impl KeywordIdents {
19031903
fn check_tokens(&mut self, cx: &EarlyContext, tokens: TokenStream) {
19041904
for tt in tokens.into_trees() {
19051905
match tt {
19061906
TokenTree::Token(span, tok) => match tok.ident() {
19071907
// only report non-raw idents
1908-
Some((ident, false)) if ident.as_str() == "async" => {
1909-
self.report(cx, span.substitute_dummy(ident.span))
1910-
},
1908+
Some((ident, false)) => {
1909+
self.check_ident(cx, ast::Ident {
1910+
span: span.substitute_dummy(ident.span),
1911+
..ident
1912+
});
1913+
}
19111914
_ => {},
19121915
}
19131916
TokenTree::Delimited(_, ref delim) => {
@@ -1916,38 +1919,47 @@ impl Async2018 {
19161919
}
19171920
}
19181921
}
1919-
fn report(&mut self, cx: &EarlyContext, span: Span) {
1920-
// don't lint `r#async`
1921-
if cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&span) {
1922-
return;
1923-
}
1924-
let mut lint = cx.struct_span_lint(
1925-
ASYNC_IDENTS,
1926-
span,
1927-
"`async` is a keyword in the 2018 edition",
1928-
);
1929-
1930-
// Don't suggest about raw identifiers if the feature isn't active
1931-
lint.span_suggestion_with_applicability(
1932-
span,
1933-
"you can use a raw identifier to stay compatible",
1934-
"r#async".to_string(),
1935-
Applicability::MachineApplicable,
1936-
);
1937-
lint.emit()
1938-
}
19391922
}
19401923

1941-
impl EarlyLintPass for Async2018 {
1924+
impl EarlyLintPass for KeywordIdents {
19421925
fn check_mac_def(&mut self, cx: &EarlyContext, mac_def: &ast::MacroDef, _id: ast::NodeId) {
19431926
self.check_tokens(cx, mac_def.stream());
19441927
}
19451928
fn check_mac(&mut self, cx: &EarlyContext, mac: &ast::Mac) {
19461929
self.check_tokens(cx, mac.node.tts.clone().into());
19471930
}
19481931
fn check_ident(&mut self, cx: &EarlyContext, ident: ast::Ident) {
1949-
if ident.as_str() == "async" {
1950-
self.report(cx, ident.span);
1932+
let next_edition = match cx.sess.edition() {
1933+
Edition::Edition2015 => {
1934+
match &ident.as_str()[..] {
1935+
"async" |
1936+
"try" => Edition::Edition2018,
1937+
_ => return,
1938+
}
1939+
}
1940+
1941+
// no new keywords yet for 2018 edition and beyond
1942+
_ => return,
1943+
};
1944+
1945+
// don't lint `r#foo`
1946+
if cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&ident.span) {
1947+
return;
19511948
}
1949+
1950+
let mut lint = cx.struct_span_lint(
1951+
KEYWORD_IDENTS,
1952+
ident.span,
1953+
&format!("`{}` is a keyword in the {} edition",
1954+
ident.as_str(),
1955+
next_edition),
1956+
);
1957+
lint.span_suggestion_with_applicability(
1958+
ident.span,
1959+
"you can use a raw identifier to stay compatible",
1960+
format!("r#{}", ident.as_str()),
1961+
Applicability::MachineApplicable,
1962+
);
1963+
lint.emit()
19521964
}
19531965
}

src/librustc_lint/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
109109
}
110110

111111
add_pre_expansion_builtin!(sess,
112-
Async2018,
112+
KeywordIdents,
113113
);
114114

115115
add_early_builtin!(sess,
@@ -239,7 +239,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
239239
edition: Some(Edition::Edition2018),
240240
},
241241
FutureIncompatibleInfo {
242-
id: LintId::of(ASYNC_IDENTS),
242+
id: LintId::of(KEYWORD_IDENTS),
243243
reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
244244
edition: Some(Edition::Edition2018),
245245
},
@@ -348,6 +348,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
348348
store.register_renamed("bare_trait_object", "bare_trait_objects");
349349
store.register_renamed("unstable_name_collision", "unstable_name_collisions");
350350
store.register_renamed("unused_doc_comment", "unused_doc_comments");
351+
store.register_renamed("async_idents", "keyword_idents");
351352
store.register_removed("unknown_features", "replaced by an error");
352353
store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
353354
store.register_removed("negate_unsigned", "cast a signed value instead");

src/test/ui/editions/auxiliary/edition-kw-macro-2015.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// edition:2015
1212

13-
#![allow(async_idents)]
13+
#![allow(keyword_idents)]
1414

1515
#[macro_export]
1616
macro_rules! produces_async {

src/test/ui/editions/auxiliary/edition-kw-macro-2018.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// edition:2018
1212

13-
#![allow(async_idents)]
13+
#![allow(keyword_idents)]
1414

1515
#[macro_export]
1616
macro_rules! produces_async {

src/test/ui/editions/edition-keywords-2015-2015-expansion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// aux-build:edition-kw-macro-2015.rs
1313
// compile-pass
1414

15-
#![allow(async_idents)]
15+
#![allow(keyword_idents)]
1616

1717
#[macro_use]
1818
extern crate edition_kw_macro_2015;

src/test/ui/editions/edition-keywords-2018-2015-expansion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// aux-build:edition-kw-macro-2015.rs
1313
// compile-pass
1414

15-
#![allow(async_idents)]
15+
#![allow(keyword_idents)]
1616

1717
#[macro_use]
1818
extern crate edition_kw_macro_2015;

src/test/ui/rust-2018/async-ident-allowed.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: lint level defined here
99
|
1010
LL | #![deny(rust_2018_compatibility)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: #[deny(async_idents)] implied by #[deny(rust_2018_compatibility)]
12+
= note: #[deny(keyword_idents)] implied by #[deny(rust_2018_compatibility)]
1313
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
1414
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
1515

src/test/ui/rust-2018/async-ident.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
#![allow(dead_code, unused_variables, non_camel_case_types, non_upper_case_globals)]
12-
#![deny(async_idents)]
12+
#![deny(keyword_idents)]
1313

1414
// edition:2015
1515
// run-rustfix

src/test/ui/rust-2018/async-ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
#![allow(dead_code, unused_variables, non_camel_case_types, non_upper_case_globals)]
12-
#![deny(async_idents)]
12+
#![deny(keyword_idents)]
1313

1414
// edition:2015
1515
// run-rustfix

src/test/ui/rust-2018/async-ident.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | fn async() {} //~ ERROR async
77
note: lint level defined here
88
--> $DIR/async-ident.rs:12:9
99
|
10-
LL | #![deny(async_idents)]
11-
| ^^^^^^^^^^^^
10+
LL | #![deny(keyword_idents)]
11+
| ^^^^^^^^^^^^^^
1212
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
1313
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
1414

src/test/ui/rust-2018/try-ident.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// run-rustfix
12+
// compile-pass
13+
14+
#![warn(rust_2018_compatibility)]
15+
16+
fn main() {
17+
r#try();
18+
}
19+
20+
fn r#try() {
21+
}

src/test/ui/rust-2018/try-ident.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// run-rustfix
12+
// compile-pass
13+
14+
#![warn(rust_2018_compatibility)]
15+
16+
fn main() {
17+
try();
18+
}
19+
20+
fn try() {
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: `try` is a keyword in the 2018 edition
2+
--> $DIR/try-ident.rs:17:5
3+
|
4+
LL | try();
5+
| ^^^ help: you can use a raw identifier to stay compatible: `r#try`
6+
|
7+
note: lint level defined here
8+
--> $DIR/try-ident.rs:14:9
9+
|
10+
LL | #![warn(rust_2018_compatibility)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: #[warn(keyword_idents)] implied by #[warn(rust_2018_compatibility)]
13+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
14+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
15+
16+
warning: `try` is a keyword in the 2018 edition
17+
--> $DIR/try-ident.rs:20:4
18+
|
19+
LL | fn try() {
20+
| ^^^ help: you can use a raw identifier to stay compatible: `r#try`
21+
|
22+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
23+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
24+

0 commit comments

Comments
 (0)