Skip to content

unsafe const fn should recover-parse as const unsafe fn #115714

Closed
@saethlin

Description

@saethlin

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=125d16cb6ffba02dc50df65194611ec5

Code

struct Thing {}

impl Thing {
    unsafe const fn from_u32(val: u32) -> Self {
        Self {}
    }
    
    fn oof(self) -> u32 {
        0
    }
}

fn main() {
    Thing {}.oof();
}

Current output

error: expected one of `extern` or `fn`, found keyword `const`
  --> src/main.rs:4:12
   |
3  | impl Thing {
   |            - while parsing this item list starting here
4  |     unsafe const fn from_u32(val: u32) -> Self {
   |     -------^^^^^
   |     |      |
   |     |      expected one of `extern` or `fn`
   |     help: `const` must come before `unsafe`: `const unsafe`
...
11 | }
   | - the item list ends here
   |
   = note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`

error[[E0599]](https://doc.rust-lang.org/stable/error_codes/E0599.html): no method named `oof` found for struct `Thing` in the current scope
  --> src/main.rs:14:14
   |
1  | struct Thing {}
   | ------------ method `oof` not found for this struct
...
14 |     Thing {}.oof();
   |              ^^^ method not found in `Thing`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 2 previous errors

Desired output

The compiler should understand that I just got the keyword order wrong, and recover-parse appropriately, instead of losing track of all methods in the impl.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTT-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