Skip to content

Rework how the disallowed qualifier in function type diagnostics are generated #142302

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JonathanBrouwer
Copy link
Contributor

@JonathanBrouwer JonathanBrouwer commented Jun 10, 2025

This pull request fixes two independent issues:

  1. When qualifiers of a function type ptr are in the wrong order and one of them is async/const (not permitted on function types), the diagnostic suggests removing the incorrect qualifier. Fixes invalid token removal suggested on unsafe const fn() type #142268, which is an issue created by Trim extra whitespace in fn ptr suggestion span #133151. This is fixed by moving the check into parse_fn_front_matter, where better span information is available to generate the right suggestions.
  2. When qualifiers of a function type ptr are in the wrong order and one of them is async/const (not permitted on function types), cargo fix crashes because "cannot replace slice of data that was already replaced". This is fixed by not generating a suggestion for the "wrong order" diagnostic if the "disallowed qualifier" diagnostic is triggered.

There is a commit with failing tests so the test diff is clearer
r? @jdonszelmann

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 10, 2025
@JonathanBrouwer JonathanBrouwer changed the title Invalid const token Rework how the disallowed qualifier lints are generated Jun 10, 2025
@JonathanBrouwer JonathanBrouwer changed the title Rework how the disallowed qualifier lints are generated Rework how the disallowed qualifier in function type lints are generated Jun 10, 2025
@JonathanBrouwer JonathanBrouwer changed the title Rework how the disallowed qualifier in function type lints are generated Rework how the disallowed qualifier in function type diagnostics are generated Jun 10, 2025
LL + pub type W1 = unsafe fn();
|

error: expected one of `extern` or `fn`, found keyword `const`
Copy link
Contributor

@jdonszelmann jdonszelmann Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two errors in some sense say a very similar thing, so I feel like ideally they're not emitted separately and as one diagnostic that explains both problems

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though that might be a separate issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you just want the "expected one of extern or fn, found keyword const" to go, and only have the "an fn pointer type cannot be const"?
That should be relatively easy to do

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter, with a "allowed keywords are ...."

@jdonszelmann
Copy link
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 12, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

invalid token removal suggested on unsafe const fn() type
3 participants