Skip to content

Limit conditions for inferencing a possible FuncDecl #2149

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 2 commits into from
Sep 11, 2023

Conversation

mininny
Copy link
Contributor

@mininny mininny commented Sep 4, 2023

Follow-up from #2109

class MyClass {
  word1 // Parsed as FunctionDecl
}

Currently, the parser consumes the word1 token as an incomplete function declaration since most declarations within a MemberDeclList are functions.

However, without more information around the token like func or () which would indicate that it is a possible FuncDecl, it could also be other declarations, like let, var, or even something like typealias.

This PR peeks the tokens after the identifier and checks if it has ( or < which would indicate that it's a function.

Any freestanding identifiers as shown in the example above would be parsed as MissingDeclSyntax, which spits out unexpected 'word1' in MyClass.

This would affect existing test cases that rely on the previous behavior and change the resulting diagnostics.


I've validated that this change is compatible with most test cases, but I'm not sure how(if I'm allowed to) to fix existing test cases, and add test cases(because many of them are translations from swift repo)

Also, I think a better diagnostic in the example above is something like: expected keyword before `word1`. did you mean to add `func` or `var`?, but I'm not yet sure how I can feed the parsed information from Parser to Diagnostics.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @mininny. The changes look good to me and I quite like the unexpected 'word1' diagnostic because at this point you can just view word1 as garbage text inside the member decl list.

And you can definitely change the existing tests. If the diagnostics improve, it’s totally fine to change their expected behavior.

@@ -281,9 +281,11 @@ extension Parser {
)
}

let isProbablyFuncDecl = self.at(.identifier, .wildcard) || self.at(anyIn: Operator.self) != nil
let isPossibleFuncIdentifier = self.at(.identifier, .wildcard)
let isPossibleFuncParen = self.peek(isAt: .identifier, .leftParen) || self.peek(isAt: .identifier, .leftAngle)
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to shorten this to

Suggested change
let isPossibleFuncParen = self.peek(isAt: .identifier, .leftParen) || self.peek(isAt: .identifier, .leftAngle)
let isPossibleFuncParen = self.peek(isAt: .identifier, .leftParen, .leftAngle)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I shortened it. However, while testing it out, I found that in

class MyClass { 
    foo<Int>
}

< is parsed as a binaryOperator rather than a left angle. I think this might need some follow-up item that should be looked at.

@mininny mininny force-pushed the limit-parser-func-decl-inference branch from 057218f to e6b8bd9 Compare September 6, 2023 14:25
@mininny mininny marked this pull request as ready for review September 6, 2023 14:26
@mininny
Copy link
Contributor Author

mininny commented Sep 6, 2023

Thanks for the PR @mininny. The changes look good to me and I quite like the unexpected 'word1' diagnostic because at this point you can just view word1 as garbage text inside the member decl list.

And you can definitely change the existing tests. If the diagnostics improve, it’s totally fine to change their expected behavior.

Thanks for the review and comment! I've added some tests and marked the PR as ready for review.

I have another question; when adding tests, is it okay to add tests to SwiftParserTests/translated? I see that it's translated from the original swift repo.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM

@ahoppen
Copy link
Member

ahoppen commented Sep 6, 2023

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Sep 6, 2023

It looks like your changes aren’t formatted. Could you run format.py?

@mininny mininny changed the title WIP: Limit conditions for inferencing a possible FuncDecl Limit conditions for inferencing a possible FuncDecl Sep 9, 2023
@mininny
Copy link
Contributor Author

mininny commented Sep 9, 2023

It looks like your changes aren’t formatted. Could you run format.py?

Yep, I ran the the format.py 👍

@ahoppen
Copy link
Member

ahoppen commented Sep 9, 2023

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Sep 11, 2023

@swift-ci Please test Windows

@ahoppen ahoppen enabled auto-merge September 11, 2023 19:51
@ahoppen ahoppen merged commit 45ba983 into swiftlang:main Sep 11, 2023
@mininny mininny deleted the limit-parser-func-decl-inference branch September 11, 2023 23:49
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