Rollup merge of #106540 - lowr:patch/remove-paren-whitespace, r=cjgillot

Insert whitespace to avoid ident concatenation in suggestion

This PR tweaks the suggestion of removing misplaced parentheses around trait bounds so as to avoid concatenating two identifiers. Although subtle, this should make outputs less surprising especially when applying the `MachineApplicable` suggestions automatically.
This commit is contained in:
Matthias Krüger 2023-01-29 20:03:35 +01:00 committed by GitHub
commit 8691602151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View File

@ -1048,7 +1048,7 @@ impl<'a> Parser<'a> {
self.parse_remaining_bounds(bounds, true)?; self.parse_remaining_bounds(bounds, true)?;
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; self.expect(&token::CloseDelim(Delimiter::Parenthesis))?;
let sp = vec![lo, self.prev_token.span]; let sp = vec![lo, self.prev_token.span];
let sugg: Vec<_> = sp.iter().map(|sp| (*sp, String::new())).collect(); let sugg = vec![(lo, String::from(" ")), (self.prev_token.span, String::new())];
self.struct_span_err(sp, "incorrect braces around trait bounds") self.struct_span_err(sp, "incorrect braces around trait bounds")
.multipart_suggestion( .multipart_suggestion(
"remove the parentheses", "remove the parentheses",

View File

@ -5,6 +5,8 @@ fn foo1(_: &dyn Drop + AsRef<str>) {} //~ ERROR ambiguous `+` in a type
fn foo2(_: &dyn (Drop + AsRef<str>)) {} //~ ERROR incorrect braces around trait bounds fn foo2(_: &dyn (Drop + AsRef<str>)) {} //~ ERROR incorrect braces around trait bounds
fn foo2_no_space(_: &dyn(Drop + AsRef<str>)) {} //~ ERROR incorrect braces around trait bounds
fn foo3(_: &dyn {Drop + AsRef<str>}) {} //~ ERROR expected parameter name, found `{` fn foo3(_: &dyn {Drop + AsRef<str>}) {} //~ ERROR expected parameter name, found `{`
//~^ ERROR expected one of `!`, `(`, `)`, `*`, `,`, `?`, `for`, `~`, lifetime, or path, found `{` //~^ ERROR expected one of `!`, `(`, `)`, `*`, `,`, `?`, `for`, `~`, lifetime, or path, found `{`
//~| ERROR at least one trait is required for an object type //~| ERROR at least one trait is required for an object type

View File

@ -13,17 +13,29 @@ LL | fn foo2(_: &dyn (Drop + AsRef<str>)) {}
help: remove the parentheses help: remove the parentheses
| |
LL - fn foo2(_: &dyn (Drop + AsRef<str>)) {} LL - fn foo2(_: &dyn (Drop + AsRef<str>)) {}
LL + fn foo2(_: &dyn Drop + AsRef<str>) {} LL + fn foo2(_: &dyn Drop + AsRef<str>) {}
|
error: incorrect braces around trait bounds
--> $DIR/trait-object-delimiters.rs:8:25
|
LL | fn foo2_no_space(_: &dyn(Drop + AsRef<str>)) {}
| ^ ^
|
help: remove the parentheses
|
LL - fn foo2_no_space(_: &dyn(Drop + AsRef<str>)) {}
LL + fn foo2_no_space(_: &dyn Drop + AsRef<str>) {}
| |
error: expected parameter name, found `{` error: expected parameter name, found `{`
--> $DIR/trait-object-delimiters.rs:8:17 --> $DIR/trait-object-delimiters.rs:10:17
| |
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {} LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
| ^ expected parameter name | ^ expected parameter name
error: expected one of `!`, `(`, `)`, `*`, `,`, `?`, `for`, `~`, lifetime, or path, found `{` error: expected one of `!`, `(`, `)`, `*`, `,`, `?`, `for`, `~`, lifetime, or path, found `{`
--> $DIR/trait-object-delimiters.rs:8:17 --> $DIR/trait-object-delimiters.rs:10:17
| |
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {} LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
| -^ expected one of 10 possible tokens | -^ expected one of 10 possible tokens
@ -31,13 +43,13 @@ LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
| help: missing `,` | help: missing `,`
error: expected identifier, found `<` error: expected identifier, found `<`
--> $DIR/trait-object-delimiters.rs:12:17 --> $DIR/trait-object-delimiters.rs:14:17
| |
LL | fn foo4(_: &dyn <Drop + AsRef<str>>) {} LL | fn foo4(_: &dyn <Drop + AsRef<str>>) {}
| ^ expected identifier | ^ expected identifier
error: invalid `dyn` keyword error: invalid `dyn` keyword
--> $DIR/trait-object-delimiters.rs:14:25 --> $DIR/trait-object-delimiters.rs:16:25
| |
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
| ^^^ help: remove this keyword | ^^^ help: remove this keyword
@ -56,13 +68,13 @@ LL | fn foo1(_: &dyn 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> = 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 error[E0224]: at least one trait is required for an object type
--> $DIR/trait-object-delimiters.rs:8:13 --> $DIR/trait-object-delimiters.rs:10:13
| |
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {} LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
| ^^^ | ^^^
error[E0225]: only auto traits can be used as additional traits in a trait object error[E0225]: only auto traits can be used as additional traits in a trait object
--> $DIR/trait-object-delimiters.rs:14:29 --> $DIR/trait-object-delimiters.rs:16:29
| |
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
| ---- ^^^^^^^^^^ additional non-auto trait | ---- ^^^^^^^^^^ additional non-auto trait
@ -72,7 +84,7 @@ LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}` = help: consider creating a new trait with all of these as supertraits 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> = 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 error: aborting due to 10 previous errors
Some errors have detailed explanations: E0224, E0225. Some errors have detailed explanations: E0224, E0225.
For more information about an error, try `rustc --explain E0224`. For more information about an error, try `rustc --explain E0224`.