Skip to content

Commit 31789a6

Browse files
committed
Auto merge of #54411 - cramertj:await-keyword-error, r=nikomatsakis
Make "await" a pseudo-edition keyword This change makes "await" ident an error in 2018 edition without async_await feature and adds "await" to the 2018 edition keyword lint group that suggest migration on the 2015 edition. cc #53834 r? @nikomatsakis
2 parents ae36663 + fb14662 commit 31789a6

12 files changed

+244
-4
lines changed

src/librustc_lint/builtin.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -1934,20 +1934,52 @@ impl EarlyLintPass for KeywordIdents {
19341934
self.check_tokens(cx, mac.node.tts.clone().into());
19351935
}
19361936
fn check_ident(&mut self, cx: &EarlyContext, ident: ast::Ident) {
1937-
let next_edition = match cx.sess.edition() {
1937+
let ident_str = &ident.as_str()[..];
1938+
let cur_edition = cx.sess.edition();
1939+
let is_raw_ident = |ident: ast::Ident| {
1940+
cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&ident.span)
1941+
};
1942+
let next_edition = match cur_edition {
19381943
Edition::Edition2015 => {
1939-
match &ident.as_str()[..] {
1944+
match ident_str {
19401945
"async" | "try" | "dyn" => Edition::Edition2018,
1946+
// Only issue warnings for `await` if the `async_await`
1947+
// feature isn't being used. Otherwise, users need
1948+
// to keep using `await` for the macro exposed by std.
1949+
"await" if !cx.sess.features_untracked().async_await => Edition::Edition2018,
19411950
_ => return,
19421951
}
19431952
}
19441953

19451954
// no new keywords yet for 2018 edition and beyond
1946-
_ => return,
1955+
// However, `await` is a "false" keyword in the 2018 edition,
1956+
// and can only be used if the `async_await` feature is enabled.
1957+
// Otherwise, we emit an error.
1958+
_ => {
1959+
if "await" == ident_str
1960+
&& !cx.sess.features_untracked().async_await
1961+
&& !is_raw_ident(ident)
1962+
{
1963+
let mut err = struct_span_err!(
1964+
cx.sess,
1965+
ident.span,
1966+
E0721,
1967+
"`await` is a keyword in the {} edition", cur_edition,
1968+
);
1969+
err.span_suggestion_with_applicability(
1970+
ident.span,
1971+
"you can use a raw identifier to stay compatible",
1972+
"r#await".to_string(),
1973+
Applicability::MachineApplicable,
1974+
);
1975+
err.emit();
1976+
}
1977+
return
1978+
},
19471979
};
19481980

19491981
// don't lint `r#foo`
1950-
if cx.sess.parse_sess.raw_identifier_spans.borrow().contains(&ident.span) {
1982+
if is_raw_ident(ident) {
19511983
return;
19521984
}
19531985

src/librustc_lint/diagnostics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
register_diagnostics! {
12+
E0721, // `await` keyword
13+
}

src/librustc_lint/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(rustc_diagnostic_macros)]
3333
#![feature(macro_at_most_once_rep)]
3434

35+
#[macro_use]
3536
extern crate syntax;
3637
#[macro_use]
3738
extern crate rustc;
@@ -61,6 +62,7 @@ use syntax::edition::Edition;
6162
use lint::LintId;
6263
use lint::FutureIncompatibleInfo;
6364

65+
mod diagnostics;
6466
mod nonstandard_style;
6567
pub mod builtin;
6668
mod types;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-pass
2+
3+
#![feature(async_await)]
4+
#![allow(non_camel_case_types)]
5+
#![deny(keyword_idents)]
6+
7+
mod outer_mod {
8+
pub mod await {
9+
pub struct await;
10+
}
11+
}
12+
use outer_mod::await::await;
13+
14+
fn main() {
15+
match await { await => {} }
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
3+
#![allow(non_camel_case_types)]
4+
#![deny(keyword_idents)]
5+
6+
mod outer_mod {
7+
pub mod r#await {
8+
pub struct r#await;
9+
}
10+
}
11+
use outer_mod::r#await::r#await;
12+
13+
fn main() {
14+
match r#await { r#await => {} }
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
3+
#![allow(non_camel_case_types)]
4+
#![deny(keyword_idents)]
5+
6+
mod outer_mod {
7+
pub mod await {
8+
pub struct await;
9+
}
10+
}
11+
use outer_mod::await::await;
12+
13+
fn main() {
14+
match await { await => {} }
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
error: `await` is a keyword in the 2018 edition
2+
--> $DIR/2015-edition-warning.rs:7:13
3+
|
4+
LL | pub mod await {
5+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
6+
|
7+
note: lint level defined here
8+
--> $DIR/2015-edition-warning.rs:4:9
9+
|
10+
LL | #![deny(keyword_idents)]
11+
| ^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
13+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
14+
15+
error: `await` is a keyword in the 2018 edition
16+
--> $DIR/2015-edition-warning.rs:8:20
17+
|
18+
LL | pub struct await;
19+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
22+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
23+
24+
error: `await` is a keyword in the 2018 edition
25+
--> $DIR/2015-edition-warning.rs:11:16
26+
|
27+
LL | use outer_mod::await::await;
28+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
29+
|
30+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
31+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
32+
33+
error: `await` is a keyword in the 2018 edition
34+
--> $DIR/2015-edition-warning.rs:11:23
35+
|
36+
LL | use outer_mod::await::await;
37+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
38+
|
39+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
40+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
41+
42+
error: `await` is a keyword in the 2018 edition
43+
--> $DIR/2015-edition-warning.rs:14:11
44+
|
45+
LL | match await { await => {} }
46+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
47+
|
48+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
49+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
50+
51+
error: `await` is a keyword in the 2018 edition
52+
--> $DIR/2015-edition-warning.rs:14:19
53+
|
54+
LL | match await { await => {} }
55+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
56+
|
57+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
58+
= note: for more information, see issue #49716 <https://github.com/rust-lang/rust/issues/49716>
59+
60+
error: aborting due to 6 previous errors
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
#![allow(non_camel_case_types)]
3+
4+
mod outer_mod {
5+
pub mod await {
6+
pub struct await;
7+
}
8+
}
9+
use self::outer_mod::await::await;
10+
11+
fn main() {
12+
match await { await => () }
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0721]: `await` is a keyword in the 2018 edition
2+
--> $DIR/2018-edition-error.rs:5:13
3+
|
4+
LL | pub mod await {
5+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
6+
7+
error[E0721]: `await` is a keyword in the 2018 edition
8+
--> $DIR/2018-edition-error.rs:6:20
9+
|
10+
LL | pub struct await;
11+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
12+
13+
error[E0721]: `await` is a keyword in the 2018 edition
14+
--> $DIR/2018-edition-error.rs:9:22
15+
|
16+
LL | use self::outer_mod::await::await;
17+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
18+
19+
error[E0721]: `await` is a keyword in the 2018 edition
20+
--> $DIR/2018-edition-error.rs:9:29
21+
|
22+
LL | use self::outer_mod::await::await;
23+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
24+
25+
error[E0721]: `await` is a keyword in the 2018 edition
26+
--> $DIR/2018-edition-error.rs:12:11
27+
|
28+
LL | match await { await => () }
29+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
30+
31+
error[E0721]: `await` is a keyword in the 2018 edition
32+
--> $DIR/2018-edition-error.rs:12:19
33+
|
34+
LL | match await { await => () }
35+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
36+
37+
error: aborting due to 6 previous errors
38+
39+
For more information about this error, try `rustc --explain E0721`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-pass
2+
// edition:2018
3+
4+
#![allow(non_camel_case_types)]
5+
#![feature(async_await)]
6+
7+
mod outer_mod {
8+
pub mod await {
9+
pub struct await;
10+
}
11+
}
12+
use self::outer_mod::await::await;
13+
14+
fn main() {
15+
match await { await => () }
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
3+
macro_rules! r#await {
4+
() => { println!("Hello, world!") }
5+
}
6+
7+
fn main() {
8+
await!()
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0721]: `await` is a keyword in the 2018 edition
2+
--> $DIR/post_expansion_error.rs:8:5
3+
|
4+
LL | await!()
5+
| ^^^^^ help: you can use a raw identifier to stay compatible: `r#await`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0721`.

0 commit comments

Comments
 (0)