Skip to content

Commit 003cab2

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 e6b35b0 commit 003cab2

14 files changed

+125
-45
lines changed

src/librustc/lint/context.rs

+1-1
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

+46-33
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use syntax::tokenstream::{TokenTree, TokenStream};
4646
use syntax::ast;
4747
use syntax::attr;
4848
use syntax::source_map::Spanned;
49+
use syntax::edition::Edition;
4950
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
5051
use syntax_pos::{BytePos, Span, SyntaxContext};
5152
use syntax::symbol::keywords;
@@ -1876,30 +1877,33 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestFunctions {
18761877
}
18771878

18781879
declare_lint! {
1879-
pub ASYNC_IDENTS,
1880+
pub KEYWORD_IDENTS,
18801881
Allow,
1881-
"detects `async` being used as an identifier"
1882+
"detects edition keywords being used as an identifier"
18821883
}
18831884

1884-
/// Checks for uses of `async` as an identifier
1885+
/// Checks for uses of edtion keywords used as an identifier
18851886
#[derive(Clone)]
1886-
pub struct Async2018;
1887+
pub struct KeywordIdents;
18871888

1888-
impl LintPass for Async2018 {
1889+
impl LintPass for KeywordIdents {
18891890
fn get_lints(&self) -> LintArray {
1890-
lint_array!(ASYNC_IDENTS)
1891+
lint_array!(KEYWORD_IDENTS)
18911892
}
18921893
}
18931894

1894-
impl Async2018 {
1895+
impl KeywordIdents {
18951896
fn check_tokens(&mut self, cx: &EarlyContext, tokens: TokenStream) {
18961897
for tt in tokens.into_trees() {
18971898
match tt {
18981899
TokenTree::Token(span, tok) => match tok.ident() {
18991900
// only report non-raw idents
1900-
Some((ident, false)) if ident.as_str() == "async" => {
1901-
self.report(cx, span.substitute_dummy(ident.span))
1902-
},
1901+
Some((ident, false)) => {
1902+
self.check_ident(cx, ast::Ident {
1903+
span: span.substitute_dummy(ident.span),
1904+
..ident
1905+
});
1906+
}
19031907
_ => {},
19041908
}
19051909
TokenTree::Delimited(_, ref delim) => {
@@ -1908,38 +1912,47 @@ impl Async2018 {
19081912
}
19091913
}
19101914
}
1911-
fn report(&mut self, cx: &EarlyContext, span: Span) {
1912-
// don't lint `r#async`
1913-
if cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&span) {
1914-
return;
1915-
}
1916-
let mut lint = cx.struct_span_lint(
1917-
ASYNC_IDENTS,
1918-
span,
1919-
"`async` is a keyword in the 2018 edition",
1920-
);
1921-
1922-
// Don't suggest about raw identifiers if the feature isn't active
1923-
lint.span_suggestion_with_applicability(
1924-
span,
1925-
"you can use a raw identifier to stay compatible",
1926-
"r#async".to_string(),
1927-
Applicability::MachineApplicable,
1928-
);
1929-
lint.emit()
1930-
}
19311915
}
19321916

1933-
impl EarlyLintPass for Async2018 {
1917+
impl EarlyLintPass for KeywordIdents {
19341918
fn check_mac_def(&mut self, cx: &EarlyContext, mac_def: &ast::MacroDef, _id: ast::NodeId) {
19351919
self.check_tokens(cx, mac_def.stream());
19361920
}
19371921
fn check_mac(&mut self, cx: &EarlyContext, mac: &ast::Mac) {
19381922
self.check_tokens(cx, mac.node.tts.clone().into());
19391923
}
19401924
fn check_ident(&mut self, cx: &EarlyContext, ident: ast::Ident) {
1941-
if ident.as_str() == "async" {
1942-
self.report(cx, ident.span);
1925+
let next_edition = match cx.sess.edition() {
1926+
Edition::Edition2015 => {
1927+
match &ident.as_str()[..] {
1928+
"async" |
1929+
"try" => Edition::Edition2018,
1930+
_ => return,
1931+
}
1932+
}
1933+
1934+
// no new keywords yet for 2018 edition and beyond
1935+
_ => return,
1936+
};
1937+
1938+
// don't lint `r#foo`
1939+
if cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&ident.span) {
1940+
return;
19431941
}
1942+
1943+
let mut lint = cx.struct_span_lint(
1944+
KEYWORD_IDENTS,
1945+
ident.span,
1946+
&format!("`{}` is a keyword in the {} edition",
1947+
ident.as_str(),
1948+
next_edition),
1949+
);
1950+
lint.span_suggestion_with_applicability(
1951+
ident.span,
1952+
"you can use a raw identifier to stay compatible",
1953+
format!("r#{}", ident.as_str()),
1954+
Applicability::MachineApplicable,
1955+
);
1956+
lint.emit()
19441957
}
19451958
}

src/librustc_lint/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
110110
}
111111

112112
add_pre_expansion_builtin!(sess,
113-
Async2018,
113+
KeywordIdents,
114114
);
115115

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

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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+21
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

+21
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+
}
+24
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)