-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle incorrect placement of parentheses in trait bounds more gracefully #84896
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// edition:2018 | ||
|
||
fn foo1(_: &dyn Drop + AsRef<str>) {} //~ ERROR ambiguous `+` in a type | ||
//~^ ERROR only auto traits can be used as additional traits in a trait object | ||
|
||
fn foo2(_: &dyn (Drop + AsRef<str>)) {} //~ ERROR incorrect braces around trait bounds | ||
|
||
fn foo3(_: &dyn {Drop + AsRef<str>}) {} //~ ERROR expected parameter name, found `{` | ||
//~^ ERROR expected one of `!`, `(`, `)`, `,`, `?`, `for`, lifetime, or path, found `{` | ||
//~| ERROR at least one trait is required for an object type | ||
|
||
fn foo4(_: &dyn <Drop + AsRef<str>>) {} //~ ERROR expected identifier, found `<` | ||
|
||
fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} //~ ERROR invalid `dyn` keyword | ||
//~^ ERROR only auto traits can be used as additional traits in a trait object | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
error: ambiguous `+` in a type | ||
--> $DIR/trait-object-delimiters.rs:3:13 | ||
| | ||
LL | fn foo1(_: &dyn Drop + AsRef<str>) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(dyn Drop + AsRef<str>)` | ||
|
||
error: incorrect braces around trait bounds | ||
--> $DIR/trait-object-delimiters.rs:6:17 | ||
| | ||
LL | fn foo2(_: &dyn (Drop + AsRef<str>)) {} | ||
| ^ ^ | ||
| | ||
help: remove the parentheses | ||
| | ||
LL | fn foo2(_: &dyn Drop + AsRef<str>) {} | ||
| -- -- | ||
|
||
error: expected parameter name, found `{` | ||
--> $DIR/trait-object-delimiters.rs:8:17 | ||
| | ||
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {} | ||
| ^ expected parameter name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really weird to me — it's not expecting a parameter name, but rather a type/trait, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a consequence of the recovery machinery. Making the errors for the |
||
|
||
error: expected one of `!`, `(`, `)`, `,`, `?`, `for`, lifetime, or path, found `{` | ||
--> $DIR/trait-object-delimiters.rs:8:17 | ||
| | ||
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {} | ||
| -^ expected one of 8 possible tokens | ||
| | | ||
| help: missing `,` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also misleading. |
||
|
||
error: expected identifier, found `<` | ||
--> $DIR/trait-object-delimiters.rs:12:17 | ||
| | ||
LL | fn foo4(_: &dyn <Drop + AsRef<str>>) {} | ||
| ^ expected identifier | ||
Comment on lines
+18
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that these two give different errors is a bit confusing. One expects a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree. I had a fix that improved this case, but it regressed other cases on malformed super-traits badly, so I pulled it of. I still left the test in in case we ever revisit this. |
||
|
||
error: invalid `dyn` keyword | ||
--> $DIR/trait-object-delimiters.rs:14:25 | ||
| | ||
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} | ||
| ^^^ help: remove this keyword | ||
| | ||
= help: `dyn` is only needed at the start of a trait `+`-separated list | ||
|
||
error[E0225]: only auto traits can be used as additional traits in a trait object | ||
--> $DIR/trait-object-delimiters.rs:3:24 | ||
| | ||
LL | fn foo1(_: &dyn Drop + AsRef<str>) {} | ||
| ---- ^^^^^^^^^^ additional non-auto trait | ||
| | | ||
| first non-auto trait | ||
| | ||
= help: consider creating a new trait with all of these as super-traits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}` | ||
= 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> | ||
|
||
error[E0224]: at least one trait is required for an object type | ||
--> $DIR/trait-object-delimiters.rs:8:13 | ||
| | ||
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {} | ||
| ^^^ | ||
|
||
error[E0225]: only auto traits can be used as additional traits in a trait object | ||
--> $DIR/trait-object-delimiters.rs:14:29 | ||
| | ||
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} | ||
| ---- ^^^^^^^^^^ additional non-auto trait | ||
| | | ||
| first non-auto trait | ||
| | ||
= help: consider creating a new trait with all of these as super-traits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}` | ||
= 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> | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
Some errors have detailed explanations: E0224, E0225. | ||
For more information about an error, try `rustc --explain E0224`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion seems odd, since if you made this change, you'd then get the error above of "use parentheses to disambiguate". Shouldn't this suggest to use
&(dyn Drop + AsRef<str>)
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is less than ideal. But I don't think we have the right information to give that suggestion. This will eventually lead the user to the right solution though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An unwritten rule is that we are ok with giving incomplete suggestions if the resulting code will give you a second correct suggestion. We try to get ahead and give a good direct fix when possible, but the amount of metadata we'd need to carry seems low benefit for the maintenance burden. Particularly in the parser, I have to be very careful not to introduce invalid changes to the grammar by accident.