-
Notifications
You must be signed in to change notification settings - Fork 439
Introduce then
statements
#2000
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
Conversation
Sources/SwiftSyntax/generated/syntaxNodes/SyntaxStmtNodes.swift
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation looks pretty good to me. I mostly have concerns with source breakage. I left a bunch of comments with whatever came to my mind.
func testThenStmt5() { | ||
assertParse( | ||
""" | ||
then (1, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m sure it’s been discussed somewhere but I missed it: What are the disambiguation rules here. This could be a function call as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The space right now IIUC. Ie. then(1, 2)
-> function call, then (1, 2)
-> then statement.
func testThenStmt7() { | ||
assertParse( | ||
""" | ||
then [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly here, before the feature, this would be a subscript call to then
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I don't expect writing functions and subscripts with an intermediate space is a common case tho.
func testThenStmt13() { | ||
assertParse( | ||
""" | ||
then .foo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here. Why isn’t this a member access on then
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the answer to basically all of these is "the whitespace". There's also:
then
.foo
and I'm not sure if that one is a statement or member access with the rules as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently that's a statement, I'm hoping that then
is a fairly uncommon name for a variable, but if not we'll probably need an extra rule that says the following expression must be on the same line for it to be treated as a statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also disambiguate with (then)
, but that's pretty awful
// This is a function call. | ||
assertParse( | ||
""" | ||
then {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you have then
followed by a closure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then ({})
was the answer there I believe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, producing a closure from a then
should a fairly uncommon thing to do anyways
case .leftParen, .leftSquare, .period: | ||
// These are handled based on whether there is trivia between the 'then' | ||
// and the token. If so, it's a 'then' statement. Otherwise it should | ||
// be treated as an expression, e.g `then(...)`, `then[...]`, `then.foo`. | ||
return !self.currentToken.trailingTriviaText.isEmpty || !next.leadingTriviaText.isEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT, the following would be interpreted as a ThenStmt
but I feel like it would be fairly common to write
then
.filter { ... }
.map { ... }
Thanks for the review! I'll respond Monday since it's getting late here :) |
2e588a3
to
bff1bb2
Compare
For now I've left the heuristic without the newline rule, I want to do some source compatibility testing to see how often that actually happens in code |
@ahoppen FYI the above force push diff is the correct diff of changes, I can separate out into a separate commit if you prefer tho |
Gahhhh Swift 5.7 doesn't support SPI enum cases :( It was fixed in Swift 5.8 |
CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SwiftSyntaxDoccIndex.swift
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline, I think it would be good to split this PR into a preliminary PR that introduces the experimental language feature infrastructure and then the actual PR that introduces the then statements.
3570163
to
9faa43d
Compare
Updated for feedback, and dropped the SPI from the syntax node for the time being as it's not supported. |
b6dcdb7
to
31b9bff
Compare
fc87d5d
to
f8060fb
Compare
These allow multi-statement `if`/`switch` expression branches that can produce a value at the end by saying `then <expr>`. This is gated behind an experimental feature option pending evolution discussion.
@swift-ci please test |
These allow multi-statement
if
/switch
expression branches that can produce a value at the end by sayingthen <expr>
. This is gated behind an experimental feature option pending evolution discussion.