-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Introduce then
statements
#67454
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
Introduce then
statements
#67454
Conversation
bb194a9
to
15f68f6
Compare
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.
Looks good to me. Just a few minor comments.
// Issue a warning when the expression is on a different line than | ||
// the 'then' keyword, but both have the same indentation. | ||
if (SourceMgr.getLineAndColumnInBuffer(thenLoc).second == | ||
SourceMgr.getLineAndColumnInBuffer(exprLoc).second) { | ||
diagnose(exprLoc, diag::unindented_code_after_then); | ||
diagnose(exprLoc, diag::indent_expression_to_silence); | ||
} |
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.
Hmm. Would be nice if we could do the same in swift-syntax but there currently isn’t any way to emit warnings from the parser (only from the lexer).
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.
Depending on where we land with the newline rule, we may not even need this logic in the first place
e21c520
to
16782d7
Compare
@hamishknight I am doing something similar that is going to need to be gated by an experimental feature in SwiftSyntax. Is it possible for you to split out the addition of the experimental feature support into a separate commit in this same PR. This will make it easier for people to cargo cult this work forward by just looking at a small commit that implements this rather than the large commit here. (I am not just doing this for myself, I am thinking down the line about other people). |
Or actually from talking @ahoppen this is going to take some time for you to do. I am going to see if I can cargo cult some of your work around the experimental feature since I need the same thing for reference bindings. |
@gottesmm Hey Michael, I already did this :) See #67826 + swiftlang/swift-syntax#2041. There's one more commit on the swift-syntax side that's needed to enable testing of experimental features (swiftlang/swift-syntax@b0414b1), I can land that separately if needed. |
@hamishknight can you land it separately in that case? My work is almost ready to go |
Sure thing: swiftlang/swift-syntax#2051 |
f4f1802
to
ebfe6aa
Compare
@swift-ci please build toolchain |
dfc038b
to
6ca310d
Compare
let x = if .random() { | ||
print("hello") | ||
then 0 | ||
// expected-error@-1 {{cannot find 'then' in scope}} |
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.
Should this be diagnosed as a disabled feature instead if then
here otherwise would be valid?
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.
This is a source breaking change, so we can't generally diagnose it as a disabled feature, we could apply a heuristic to generate that diagnostic, but I'm not convinced it's worth it.
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.
Maybe it's not worth it if we are going to enable this by default soon, but it would be great for user experience to hint that all they need is to enable a flag in situations when it's syntactically unambiguous that they meant then
statement.
These allow multi-statement `if`/`switch` expression branches that can produce a value at the end by saying `then <expr>`. This is gated behind `-enable-experimental-feature ThenStatements` pending evolution discussion.
@swift-ci please test |
@swift-ci please build toolchain |
These allow multi-statement
if
/switch
expression branches that can produce a value at the end by sayingthen <expr>
. This is gated behind-enable-experimental-feature ThenStatements
pending evolution discussion.