Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
saethlin opened this issue Sep 9, 2023 · 0 comments · Fixed by #117282
Closed

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

saethlin opened this issue Sep 9, 2023 · 0 comments · Fixed by #117282
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@saethlin
Copy link
Member

saethlin commented Sep 9, 2023

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

@saethlin saethlin added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 9, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 9, 2023
@fmease fmease added A-parser Area: The lexing & parsing of Rust source code to an AST D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Sep 29, 2023
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Nov 8, 2023
…eader, r=TaKO8Ki

Recover from incorrectly ordered/duplicated function keywords

Fixes rust-lang#115714
@bors bors closed this as completed in c828371 Nov 8, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 8, 2023
Rollup merge of rust-lang#117282 - clubby789:recover-wrong-function-header, r=TaKO8Ki

Recover from incorrectly ordered/duplicated function keywords

Fixes rust-lang#115714
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants