rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.58k stars 12.74k forks source link

Extra whitespace when suggesting removal of invalid qualifiers from `fn` pointer #133083

Open tyrone-wu opened 5 hours ago

tyrone-wu commented 5 hours ago

Code

type FOO = const async fn();

Current output

error: an `fn` pointer type cannot be `const`
 --> src/main.rs:1:12
  |
1 | type FOO = const async fn();
  |            -----^^^^^^^^^^^
  |            |
  |            `const` because of this
  |
help: remove the `const` qualifier
  |
1 - type FOO = const async fn();
1 + type FOO =  async fn();
  |

error: an `fn` pointer type cannot be `async`
 --> src/main.rs:1:12
  |
1 | type FOO = const async fn();
  |            ^^^^^^-----^^^^^
  |                  |
  |                  `async` because of this
  |
help: remove the `async` qualifier
  |
1 - type FOO = const async fn();
1 + type FOO = const  fn();
  |

Desired output

1 - type FOO = const async fn();
1 + type FOO = async fn();

...

1 - type FOO = const async fn();
1 + type FOO = const fn();

Rationale and extra context

The suggestion of the current output produces an extra whitespace for each invalid qualifier.

type FOO =   fn();

Ideally, the desired output should fully remove the qualifier and following whitespaces up until the next token.

type FOO = fn();

Other cases

Rust Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Anything else?

No response

tyrone-wu commented 3 hours ago

After looking into FnPointerCannotBeConst and FnPointerCannotBeAsync, is there a reason the diagnostic need the whole_span? Maybe a better visual indicator could be to only highlight the offending Span, something like this:

1 | type FOO = const async fn();
  |            ^^^^^
  |
help: remove the `const` qualifier

...

1 | type FOO = const async fn();
  |                  ^^^^^
  |
help: remove the `async` qualifier

I see the suggestion is maybe-incorrect, so perhaps highlighting the whole_span could be beneficial, although I'm not sure what it would look like when the suggestion is actually incorrect.