Skip to content

Commit 4956b12

Browse files
authoredMay 6, 2021
Rollup merge of rust-lang#84896 - estebank:issue-84772, r=jackh726
Handle incorrect placement of parentheses in trait bounds more gracefully Fix rust-lang#84772. CC ```@jonhoo```
2 parents c6556ea + 6b64202 commit 4956b12

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed
 

‎compiler/rustc_parse/src/parser/ty.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<'a> Parser<'a> {
470470
/// Is a `dyn B0 + ... + Bn` type allowed here?
471471
fn is_explicit_dyn_type(&mut self) -> bool {
472472
self.check_keyword(kw::Dyn)
473-
&& (self.token.uninterpolated_span().rust_2018()
473+
&& (!self.token.uninterpolated_span().rust_2015()
474474
|| self.look_ahead(1, |t| {
475475
t.can_begin_bound() && !can_continue_type_after_non_fn_ident(t)
476476
}))
@@ -539,7 +539,21 @@ impl<'a> Parser<'a> {
539539
) -> PResult<'a, GenericBounds> {
540540
let mut bounds = Vec::new();
541541
let mut negative_bounds = Vec::new();
542-
while self.can_begin_bound() {
542+
543+
while self.can_begin_bound() || self.token.is_keyword(kw::Dyn) {
544+
if self.token.is_keyword(kw::Dyn) {
545+
// Account for `&dyn Trait + dyn Other`.
546+
self.struct_span_err(self.token.span, "invalid `dyn` keyword")
547+
.help("`dyn` is only needed at the start of a trait `+`-separated list")
548+
.span_suggestion(
549+
self.token.span,
550+
"remove this keyword",
551+
String::new(),
552+
Applicability::MachineApplicable,
553+
)
554+
.emit();
555+
self.bump();
556+
}
543557
match self.parse_generic_bound()? {
544558
Ok(bound) => bounds.push(bound),
545559
Err(neg_sp) => negative_bounds.push(neg_sp),
@@ -721,7 +735,26 @@ impl<'a> Parser<'a> {
721735
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;
722736
let path = self.parse_path(PathStyle::Type)?;
723737
if has_parens {
724-
self.expect(&token::CloseDelim(token::Paren))?;
738+
if self.token.is_like_plus() {
739+
// Someone has written something like `&dyn (Trait + Other)`. The correct code
740+
// would be `&(dyn Trait + Other)`, but we don't have access to the appropriate
741+
// span to suggest that. When written as `&dyn Trait + Other`, an appropriate
742+
// suggestion is given.
743+
let bounds = vec![];
744+
self.parse_remaining_bounds(bounds, true)?;
745+
self.expect(&token::CloseDelim(token::Paren))?;
746+
let sp = vec![lo, self.prev_token.span];
747+
let sugg: Vec<_> = sp.iter().map(|sp| (*sp, String::new())).collect();
748+
self.struct_span_err(sp, "incorrect braces around trait bounds")
749+
.multipart_suggestion(
750+
"remove the parentheses",
751+
sugg,
752+
Applicability::MachineApplicable,
753+
)
754+
.emit();
755+
} else {
756+
self.expect(&token::CloseDelim(token::Paren))?;
757+
}
725758
}
726759

727760
let modifier = modifiers.to_trait_bound_modifier();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition:2018
2+
3+
fn foo1(_: &dyn Drop + AsRef<str>) {} //~ ERROR ambiguous `+` in a type
4+
//~^ ERROR only auto traits can be used as additional traits in a trait object
5+
6+
fn foo2(_: &dyn (Drop + AsRef<str>)) {} //~ ERROR incorrect braces around trait bounds
7+
8+
fn foo3(_: &dyn {Drop + AsRef<str>}) {} //~ ERROR expected parameter name, found `{`
9+
//~^ ERROR expected one of `!`, `(`, `)`, `,`, `?`, `for`, lifetime, or path, found `{`
10+
//~| ERROR at least one trait is required for an object type
11+
12+
fn foo4(_: &dyn <Drop + AsRef<str>>) {} //~ ERROR expected identifier, found `<`
13+
14+
fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} //~ ERROR invalid `dyn` keyword
15+
//~^ ERROR only auto traits can be used as additional traits in a trait object
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
error: ambiguous `+` in a type
2+
--> $DIR/trait-object-delimiters.rs:3:13
3+
|
4+
LL | fn foo1(_: &dyn Drop + AsRef<str>) {}
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(dyn Drop + AsRef<str>)`
6+
7+
error: incorrect braces around trait bounds
8+
--> $DIR/trait-object-delimiters.rs:6:17
9+
|
10+
LL | fn foo2(_: &dyn (Drop + AsRef<str>)) {}
11+
| ^ ^
12+
|
13+
help: remove the parentheses
14+
|
15+
LL | fn foo2(_: &dyn Drop + AsRef<str>) {}
16+
| -- --
17+
18+
error: expected parameter name, found `{`
19+
--> $DIR/trait-object-delimiters.rs:8:17
20+
|
21+
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
22+
| ^ expected parameter name
23+
24+
error: expected one of `!`, `(`, `)`, `,`, `?`, `for`, lifetime, or path, found `{`
25+
--> $DIR/trait-object-delimiters.rs:8:17
26+
|
27+
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
28+
| -^ expected one of 8 possible tokens
29+
| |
30+
| help: missing `,`
31+
32+
error: expected identifier, found `<`
33+
--> $DIR/trait-object-delimiters.rs:12:17
34+
|
35+
LL | fn foo4(_: &dyn <Drop + AsRef<str>>) {}
36+
| ^ expected identifier
37+
38+
error: invalid `dyn` keyword
39+
--> $DIR/trait-object-delimiters.rs:14:25
40+
|
41+
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
42+
| ^^^ help: remove this keyword
43+
|
44+
= help: `dyn` is only needed at the start of a trait `+`-separated list
45+
46+
error[E0225]: only auto traits can be used as additional traits in a trait object
47+
--> $DIR/trait-object-delimiters.rs:3:24
48+
|
49+
LL | fn foo1(_: &dyn Drop + AsRef<str>) {}
50+
| ---- ^^^^^^^^^^ additional non-auto trait
51+
| |
52+
| first non-auto trait
53+
|
54+
= help: consider creating a new trait with all of these as super-traits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}`
55+
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
56+
57+
error[E0224]: at least one trait is required for an object type
58+
--> $DIR/trait-object-delimiters.rs:8:13
59+
|
60+
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
61+
| ^^^
62+
63+
error[E0225]: only auto traits can be used as additional traits in a trait object
64+
--> $DIR/trait-object-delimiters.rs:14:29
65+
|
66+
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
67+
| ---- ^^^^^^^^^^ additional non-auto trait
68+
| |
69+
| first non-auto trait
70+
|
71+
= help: consider creating a new trait with all of these as super-traits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}`
72+
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
73+
74+
error: aborting due to 9 previous errors
75+
76+
Some errors have detailed explanations: E0224, E0225.
77+
For more information about an error, try `rustc --explain E0224`.

0 commit comments

Comments
 (0)
Please sign in to comment.