Skip to content

Typo detection for "const" works in const fn but not in pub const fn #153353

@AccessViolation95

Description

@AccessViolation95

Code

pub struct Thing {
    octets: [u8; 8],
}

impl Thing {
    pub contst fn octets(&self) -> [u8; 8] {
        self.octets
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error: visibility `pub` is not followed by an item
 --> src/lib.rs:6:5
  |
6 |     pub contst fn octets(&self) -> [u8; 8] {
  |     ^^^ the visibility
  |
  = help: you likely meant to define an item, e.g., `pub fn foo() {}`

error: non-item in item list
 --> src/lib.rs:6:9
  |
5 | impl Thing {
  |            - item list starts here
6 |     pub contst fn octets(&self) -> [u8; 8] {
  |         ^^^^^^ non-item starts here
...
9 | }
  | - item list ends here

error: could not compile `playground` (lib) due to 2 previous errors

Desired output

Compiling playground v0.0.1 (/playground)
error: visibility `pub` is not followed by an item
 --> src/lib.rs:6:5
  |
6 |     pub contst fn octets(&self) -> [u8; 8] {
  |     ^^^ the visibility
  |
  = help: you likely meant to define an item, e.g., `pub fn foo() {}`

error: non-item in item list
 --> src/lib.rs:6:9
  |
5 | impl Thing {
  |            - item list starts here
6 |     pub contst fn octets(&self) -> [u8; 8] {
  |         ^^^^^^ non-item starts here
...
9 | }
  | - item list ends here
help: there is a keyword `const` with a similar name
  |
6 -     pub contst fn octets(&self) -> [u8; 8] {
6 +     pub const fn octets(&self) -> [u8; 8] {
  |

error: could not compile `playground` (lib) due to 2 previous errors

Rationale and extra context

No response

Other cases

pub struct Thing {
    octets: [u8; 8],
}

impl Thing {
    contst fn octets(&self) -> [u8; 8] {
        self.octets
    }
}

~~~~~~~~~~

   Compiling playground v0.0.1 (/playground)
error: expected one of `!` or `::`, found keyword `fn`
 --> src/lib.rs:6:12
  |
5 | impl Thing {
  |            - while parsing this item list starting here
6 |     contst fn octets(&self) -> [u8; 8] {
  |            ^^ expected one of `!` or `::`
...
9 | }
  | - the item list ends here
  |
help: there is a keyword `const` with a similar name
  |
6 -     contst fn octets(&self) -> [u8; 8] {
6 +     const fn octets(&self) -> [u8; 8] {
  |

error: could not compile `playground` (lib) due to 1 previous error

Rust Version

$ rustc --version --verbose
rustc 1.93.1 (01f6ddf75 2026-02-11)
binary: rustc
commit-hash: 01f6ddf7588f42ae2d7eb0a2f21d44e8e96674cf
commit-date: 2026-02-11
host: x86_64-unknown-linux-gnu
release: 1.93.1
LLVM version: 21.1.8

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions