Skip to content

Compiler diagnostic suggests wrong keyword order #94879

New issue

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

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

Already on GitHub? Sign in to your account

Closed
PoignardAzur opened this issue Mar 12, 2022 · 2 comments · Fixed by #96629
Closed

Compiler diagnostic suggests wrong keyword order #94879

PoignardAzur opened this issue Mar 12, 2022 · 2 comments · Fixed by #96629
Assignees
Labels
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.

Comments

@PoignardAzur
Copy link
Contributor

PoignardAzur commented Mar 12, 2022

If I write this code:

#[cfg(FALSE)]
extern const fn hello() {}

The compiler tells me:

error: expected `fn`, found keyword `const`
 --> src/lib.rs:2:8
  | ...
  = note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`

That's a great message, very helpful... except the order is wrong. If I write:

#[cfg(FALSE)]
default pub const async unsafe extern fn hello() {}

The compiler gives me a weird error message.

The correct order is pub, default, const, async, unsafe, extern; in other words, pub before everything else.

Indeed, the following code compiles without complaint:

#[cfg(FALSE)]
pub default const async unsafe extern fn hello() {}

(as well as other variants with pub(crate), crate, etc)

The diagnostic suggestion should be fixed.

@PoignardAzur PoignardAzur 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 Mar 12, 2022
@PoignardAzur
Copy link
Contributor Author

Side note: it's interesting that the error suggestion is triggered in some cases but not in others. Maybe the diagnostics should be standardized over the codebase.

From quick experimentation:

  • If any of const async unsafe extern are in the wrong order, rustc gives the above message.
  • If pub is in the wrong position, eg const async pub unsafe rustc gives another message:
    visibility `pub` must come before `const  async`: `pub const  async`
    
  • If default is in the wrong position, rustc gives an unhelpful error message:
    error: expected `fn`, found `default`
    
    or
    error: `default` is not followed by an item
    

Might be worth a separate issue.

@ken-matsui
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants