Skip to content

invalid token removal suggested on unsafe const fn() type #142268

@cyrgani

Description

@cyrgani

Code

type F = unsafe const fn();

Current output

error: expected one of `extern` or `fn`, found keyword `const`
 --> src/main.rs:1:17
  |
1 | type F = unsafe const fn();
  |          -------^^^^^
  |          |      |
  |          |      expected one of `extern` or `fn`
  |          help: `const` must come before `unsafe`: `const unsafe`
  |
  = note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`

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

Desired output

error: expected one of `extern` or `fn`, found keyword `const`
 --> src/main.rs:1:17
  |
1 | type F = unsafe const fn();
  |          -------^^^^^
  |          |      |
  |          |      expected one of `extern` or `fn`
  |          help: `const` must come before `unsafe`: `const unsafe`
  |
  = note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`

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

Rationale and extra context

The span to remove points at the wrong token.
Happens similarly for extern "C" instead of unsafe.

Other cases

Rust Version

rustc 1.89.0-nightly (6ccd44760 2025-06-08)
binary: rustc
commit-hash: 6ccd4476036edfce364e6271f9e190ec7a2a1ff5
commit-date: 2025-06-08
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions