Skip to content

[Parser] Add fix-it for missing type errors #9294

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

Merged
merged 1 commit into from
May 20, 2017

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented May 4, 2017

Resolves SR-4785.

We can't reliably decide in the parser if a type was forgotten or a wrong spelling was meant to be a type (e.g. let x: class MyClass). This patch applies a heuristic that the parameter was most likely forgotten if the next character is a closing bracket or a semantic separator (arrow, equal, comma, semicolon).
This catches the most common cases of function parameters and variable declarations that are immediately initialised.

We can't reliably decide in the parser if a type was forgotten or a
wrong type was meant to be a type (e.g. `let x: class MyClass`).
This patch applies a heuristic that the parameter was most likely
forgotten if the next character is a closing bracket or a semantic
separator.
This catches the most common cases of function parameters and variable
declarations that are immediately initialised.

This fixes SR-4785.
@ahoppen
Copy link
Member Author

ahoppen commented May 4, 2017

@swift-ci please smoke test

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

I missed this when you first posted it—it looks good to me! Thanks, Alex.

// If the next token is closing or separating, the type was likely forgotten
if (Tok.isAny(tok::r_paren, tok::r_brace, tok::r_square, tok::arrow,
tok::equal, tok::comma, tok::semi)) {
diag.fixItInsert(getEndOfPreviousLoc(), " <#type#>");
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't perfect because the space isn't always needed, but I can't think of a way to test whether it's needed without looking at the previous token, which is a bit tricky. I guess this is good enough!

@jrose-apple
Copy link
Contributor

Re-smoke-testing just in case, since it's been two weeks.

@swift-ci Please smoke test

@ahoppen ahoppen merged commit 1812830 into swiftlang:master May 20, 2017
@ahoppen
Copy link
Member Author

ahoppen commented May 20, 2017

Thanks for the review

jtbandes added a commit that referenced this pull request May 27, 2017
Fixes @practicalswift's crasher 28759 (#9907), which seems to have been introduced in #9294.
@ahoppen ahoppen deleted the SR-4785-fixit-missing-type branch June 26, 2017 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants