Skip to content

Commit 8a61907

Browse files
authored
Rollup merge of rust-lang#54261 - varkor:dyn-keyword-2018, r=petrochenkov
Make `dyn` a keyword in the 2018 edition Proposed in rust-lang#44662 (comment).
2 parents b9d51ed + cb594cf commit 8a61907

File tree

9 files changed

+72
-14
lines changed

9 files changed

+72
-14
lines changed

src/librustc_lint/builtin.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1937,8 +1937,7 @@ impl EarlyLintPass for KeywordIdents {
19371937
let next_edition = match cx.sess.edition() {
19381938
Edition::Edition2015 => {
19391939
match &ident.as_str()[..] {
1940-
"async" |
1941-
"try" => Edition::Edition2018,
1940+
"async" | "try" | "dyn" => Edition::Edition2018,
19421941
_ => return,
19431942
}
19441943
}

src/libsyntax/parse/parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,9 @@ impl<'a> Parser<'a> {
15781578
impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
15791579
TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds)
15801580
} else if self.check_keyword(keywords::Dyn) &&
1581-
self.look_ahead(1, |t| t.can_begin_bound() &&
1582-
!can_continue_type_after_non_fn_ident(t)) {
1581+
(self.span.edition() == Edition::Edition2018 ||
1582+
self.look_ahead(1, |t| t.can_begin_bound() &&
1583+
!can_continue_type_after_non_fn_ident(t))) {
15831584
self.bump(); // `dyn`
15841585
// Always parse bounds greedily for better error recovery.
15851586
let bounds = self.parse_generic_bounds()?;

src/libsyntax/parse/token.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn ident_can_begin_type(ident: ast::Ident, is_raw: bool) -> bool {
136136
keywords::Unsafe.name(),
137137
keywords::Extern.name(),
138138
keywords::Typeof.name(),
139+
keywords::Dyn.name(),
139140
].contains(&ident.name)
140141
}
141142

src/libsyntax_pos/symbol.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -414,26 +414,25 @@ declare_keywords! {
414414
(50, Yield, "yield")
415415

416416
// Edition-specific keywords reserved for future use.
417-
(51, Async, "async") // >= 2018 Edition Only
418-
(52, Try, "try") // >= 2018 Edition Only
417+
(51, Async, "async") // >= 2018 Edition only
418+
(52, Dyn, "dyn") // >= 2018 Edition only
419+
(53, Try, "try") // >= 2018 Edition only
419420

420421
// Special lifetime names
421-
(53, UnderscoreLifetime, "'_")
422-
(54, StaticLifetime, "'static")
422+
(54, UnderscoreLifetime, "'_")
423+
(55, StaticLifetime, "'static")
423424

424425
// Weak keywords, have special meaning only in specific contexts.
425-
(55, Auto, "auto")
426-
(56, Catch, "catch")
427-
(57, Default, "default")
428-
(58, Dyn, "dyn")
426+
(56, Auto, "auto")
427+
(57, Catch, "catch")
428+
(58, Default, "default")
429429
(59, Union, "union")
430430
(60, Existential, "existential")
431431
}
432432

433433
impl Symbol {
434434
fn is_unused_keyword_2018(self) -> bool {
435-
self >= keywords::Async.name() &&
436-
self <= keywords::Try.name()
435+
self >= keywords::Async.name() && self <= keywords::Try.name()
437436
}
438437
}
439438

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition:2015
2+
// run-rustfix
3+
4+
#![allow(unused_variables)]
5+
#![deny(keyword_idents)]
6+
7+
fn main() {
8+
let r#dyn = (); //~ ERROR dyn
9+
//~^ WARN hard error in the 2018 edition
10+
}

src/test/ui/rust-2018/dyn-keyword.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition:2015
2+
// run-rustfix
3+
4+
#![allow(unused_variables)]
5+
#![deny(keyword_idents)]
6+
7+
fn main() {
8+
let dyn = (); //~ ERROR dyn
9+
//~^ WARN hard error in the 2018 edition
10+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: `dyn` is a keyword in the 2018 edition
2+
--> $DIR/dyn-keyword.rs:8:9
3+
|
4+
LL | let dyn = (); //~ ERROR dyn
5+
| ^^^ help: you can use a raw identifier to stay compatible: `r#dyn`
6+
|
7+
note: lint level defined here
8+
--> $DIR/dyn-keyword.rs:5: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: aborting due to previous error
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// edition:2018
2+
3+
type A0 = dyn;
4+
type A1 = dyn::dyn; //~ERROR expected identifier, found reserved keyword
5+
type A2 = dyn<dyn, dyn>; //~ERROR expected identifier, found `<`
6+
type A3 = dyn<<dyn as dyn>::dyn>;
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: expected identifier, found reserved keyword `dyn`
2+
--> $DIR/dyn-trait-compatibility.rs:4:16
3+
|
4+
LL | type A1 = dyn::dyn; //~ERROR expected identifier, found reserved keyword
5+
| ^^^ expected identifier, found reserved keyword
6+
7+
error: expected identifier, found `<`
8+
--> $DIR/dyn-trait-compatibility.rs:5:14
9+
|
10+
LL | type A2 = dyn<dyn, dyn>; //~ERROR expected identifier, found `<`
11+
| ^ expected identifier
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)