Skip to content

Commit bb6c37a

Browse files
Rollup merge of rust-lang#157577 - i-like-ICEs:fix-dyn-2015-parser, r=fmease
Fix parser error recovery treating 'dyn' as a strict keyword in Rust … …2015 when used in `dyn + dyn` Fixes: rust-lang#157565 ### Description A diagnostics improvement rust-lang#84896 in 2015 edition caused errors when `dyn + dyn` was used, even though `dyn` is a contextual keyword in the 2015 edition. ### Solution A check was added in `rustc_parse/src/parser/ty.rs:1095`: ```rust if self.token.is_keyword(kw::Dyn) && self.token.span.edition().at_least_rust_2018() { ``` This checks that the version is not 2015 before throwing an error. A regression test was also added, in `tests/ui/parser/dyn-2015-identifier.rs`
2 parents 0c0e37d + 5bc21b1 commit bb6c37a

5 files changed

Lines changed: 109 additions & 71 deletions

File tree

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ impl<'a> Parser<'a> {
10921092
&& (self.token.can_begin_type()
10931093
|| (self.token.is_reserved_ident() && !self.token.is_keyword(kw::Where))))
10941094
{
1095-
if self.token.is_keyword(kw::Dyn) {
1095+
if self.token.is_keyword(kw::Dyn) && self.token.span.edition().at_least_rust_2018() {
10961096
// Account for `&dyn Trait + dyn Other`.
10971097
self.bump();
10981098
self.dcx().emit_err(InvalidDynKeyword {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
error[E0425]: cannot find type `dyn` in this scope
2+
--> $DIR/dyn-2015-identifier.rs:7:11
3+
|
4+
LL | type A0 = dyn;
5+
| ^^^ not found in this scope
6+
7+
error[E0425]: cannot find type `dyn` in this scope
8+
--> $DIR/dyn-2015-identifier.rs:13:11
9+
|
10+
LL | type A2 = dyn<dyn, dyn>;
11+
| ^^^ not found in this scope
12+
13+
error[E0425]: cannot find type `dyn` in this scope
14+
--> $DIR/dyn-2015-identifier.rs:13:15
15+
|
16+
LL | type A2 = dyn<dyn, dyn>;
17+
| ^^^ not found in this scope
18+
19+
error[E0425]: cannot find type `dyn` in this scope
20+
--> $DIR/dyn-2015-identifier.rs:13:20
21+
|
22+
LL | type A2 = dyn<dyn, dyn>;
23+
| ^^^ not found in this scope
24+
25+
error[E0425]: cannot find type `dyn` in this scope
26+
--> $DIR/dyn-2015-identifier.rs:18:11
27+
|
28+
LL | type A3 = dyn<<dyn as dyn>::dyn>;
29+
| ^^^ not found in this scope
30+
31+
error[E0405]: cannot find trait `dyn` in this scope
32+
--> $DIR/dyn-2015-identifier.rs:18:23
33+
|
34+
LL | type A3 = dyn<<dyn as dyn>::dyn>;
35+
| ^^^ not found in this scope
36+
37+
error[E0425]: cannot find type `dyn` in this scope
38+
--> $DIR/dyn-2015-identifier.rs:18:16
39+
|
40+
LL | type A3 = dyn<<dyn as dyn>::dyn>;
41+
| ^^^ not found in this scope
42+
43+
error[E0405]: cannot find trait `dyn` in this scope
44+
--> $DIR/dyn-2015-identifier.rs:24:11
45+
|
46+
LL | type A4 = dyn + dyn;
47+
| ^^^ not found in this scope
48+
49+
error[E0405]: cannot find trait `dyn` in this scope
50+
--> $DIR/dyn-2015-identifier.rs:24:17
51+
|
52+
LL | type A4 = dyn + dyn;
53+
| ^^^ not found in this scope
54+
55+
warning: trait objects without an explicit `dyn` are deprecated
56+
--> $DIR/dyn-2015-identifier.rs:24:11
57+
|
58+
LL | type A4 = dyn + dyn;
59+
| ^^^^^^^^^
60+
|
61+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
62+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/warnings-promoted-to-error.html>
63+
= note: `#[warn(bare_trait_objects)]` (part of `#[warn(rust_2021_compatibility)]`) on by default
64+
help: if this is a dyn-compatible trait, use `dyn`
65+
|
66+
LL | type A4 = dyn dyn + dyn;
67+
| +++
68+
69+
error[E0433]: cannot find module or crate `dyn` in this scope
70+
--> $DIR/dyn-2015-identifier.rs:10:11
71+
|
72+
LL | type A1 = dyn::dyn;
73+
| ^^^ use of unresolved module or unlinked crate `dyn`
74+
|
75+
= help: you might be missing a crate named `dyn`
76+
77+
error: aborting due to 10 previous errors; 1 warning emitted
78+
79+
Some errors have detailed explanations: E0405, E0425, E0433.
80+
For more information about an error, try `rustc --explain E0405`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ edition: 2015
2+
//@ compile-flags: --crate-type=lib
3+
//@ revisions: pass fail
4+
//@[pass] check-pass
5+
#![cfg(fail)]
6+
7+
type A0 = dyn;
8+
//[fail]~^ ERROR cannot find type `dyn` in this scope
9+
10+
type A1 = dyn::dyn;
11+
//[fail]~^ ERROR cannot find module or crate `dyn` in this scope
12+
13+
type A2 = dyn<dyn, dyn>;
14+
//[fail]~^ ERROR cannot find type `dyn` in this scope
15+
//[fail]~| ERROR cannot find type `dyn` in this scope
16+
//[fail]~| ERROR cannot find type `dyn` in this scope
17+
18+
type A3 = dyn<<dyn as dyn>::dyn>;
19+
//[fail]~^ ERROR cannot find type `dyn` in this scope
20+
//[fail]~| ERROR cannot find type `dyn` in this scope
21+
//[fail]~| ERROR cannot find trait `dyn` in this scope
22+
23+
// issue: <https://github.com/rust-lang/rust/issues/157565>
24+
type A4 = dyn + dyn;
25+
//[fail]~^ ERROR cannot find trait `dyn` in this scope
26+
//[fail]~| ERROR cannot find trait `dyn` in this scope
27+
//[fail]~| WARN trait objects without an explicit `dyn` are deprecated
28+
//[fail]~| WARN this is accepted in the current edition

tests/ui/parser/dyn-trait-compatibility.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/ui/parser/dyn-trait-compatibility.stderr

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)