Skip to content
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

Recover upon mistyped error on typo'd const in const param def #112029

Merged
merged 1 commit into from
May 29, 2023

Conversation

jieyouxu
Copy link
Member

And add machine-applicable fix for the typo'd const keyword.

Before

error: expected one of `,`, `:`, `=`, or `>`, found `N`
 --> src/lib.rs:1:18
  |
1 | pub fn bar<Const N: u8>() {}
  |                  ^ expected one of `,`, `:`, `=`, or `>`

After This PR

error: `const` keyword was mistyped as `Const`
 --> test.rs:1:8
  |
1 | fn bar<Const N: u8>() {}
  |        ^^^^^
  |
help: use the `const` keyword
  |
1 | fn bar<const N: u8>() {}
  |        ~~~~~

Fixes #111941.

@rustbot
Copy link
Collaborator

rustbot commented May 28, 2023

r? @cjgillot

(rustbot has picked a reviewer for you, use r? to override)

@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 May 28, 2023
@jieyouxu jieyouxu force-pushed the typo-const-in-const-param-def branch from 84a5120 to be66fca Compare May 28, 2023 06:36
Comment on lines 239 to 258
if let Some(last) = params.last()
&& last.ident.as_str().to_ascii_lowercase() == kw::Const.as_str()
{
// We might have a typo'd `Const` that was parsed as a type parameter.
let span = last.ident.span;
let mut err = self.struct_span_err(
span,
format!("`const` keyword was mistyped as `{}`", last.ident.as_str()),
);
err.span_suggestion_verbose(
span,
"use the `const` keyword",
kw::Const.as_str(),
Applicability::MachineApplicable,
);
err.emit();
return Err(err);
} else {
self.expect_gt()?;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this suggestion happen inside parse_generic_params or parse_ty_param?
Can it be made an actual recovery = if self.may_recover(), find a way to return Ok with a sensible AST?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I got the recovery as GenericParam working

Copy link
Member Author

Choose a reason for hiding this comment

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

Had to attrs.clone() a few places, not sure if I can avoid it

@jieyouxu jieyouxu force-pushed the typo-const-in-const-param-def branch from 302052c to e98650c Compare May 28, 2023 07:59
@jieyouxu jieyouxu changed the title Show mistyped error on typo'd const in const param def Recover upon mistyped error on typo'd const in const param def May 28, 2023
@jieyouxu jieyouxu force-pushed the typo-const-in-const-param-def branch from e98650c to 41f5a30 Compare May 28, 2023 08:55
@cjgillot
Copy link
Contributor

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented May 28, 2023

📌 Commit 41f5a30 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 28, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 29, 2023
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#112029 (Recover upon mistyped error on typo'd `const` in const param def)
 - rust-lang#112037 (Add details about `unsafe_op_in_unsafe_fn` to E0133)
 - rust-lang#112039 (compiler: update solaris/illumos to enable tsan support.)
 - rust-lang#112042 (Migrate GUI colors test to original CSS color format)
 - rust-lang#112045 (Followup to rust-lang#111973)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 45ca2f7 into rust-lang:master May 29, 2023
@rustbot rustbot added this to the 1.72.0 milestone May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

missing recover for typo'd const in const parameter definition
4 participants