-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[SR-2] Build configuration directives can not wrap switch cases #42628
Comments
Comment by Alex Lew (JIRA) Looks like a problem with if / else too: if (4 < 5) {
print("True")
}
#if FOO
else {
print("False")
}
#endif doesn't compile. |
I signed up for this one on the mailing list. |
I don't see where compiler control statements are allowed by the grammar for switch statements. Is the grammar out of date? Am I looking at the wrong docs? |
Comment by Alexander Panin (JIRA) Consider this as an improvement of the grammar for switch statements and for branch statements. |
I am still looking at this one. Should have an update this week. |
Comment by Edward Patel (JIRA) Is SR-826 related to this? Conditional compilation over methods seem to do a simplified syntax check and report error when code has been disabled. |
It is related in that it is a very similar problem, but I suspect the patch to fix [SR-826] will be a little different and should be a separate patch. I will checkout that one out too after I push a patch for this issue. |
Nevermind, I see there is already a patch in flight for [SR-826]. |
This bug still exists. For whatever reason, SR-826 did not patch. |
I've spent some time looking at the parser code today, and I don't think this qualifies as a starter bug. The following is based on my limited understanding: Making this work will require non-trivial changes to the parser, and due to the rules around conditional compilation it might even require an evolution proposal. While conditional compilation directives use a syntax that's similar to C's preprocessor, they don't work the same way. Rather than modify the source code prior to parsing, the statements contained in the if/else clauses are parsed in the same step as the rest of the code and then added to the AST. That means the contents of those blocks currently need to stand on their own and can't extend surrounding statements. That's why you can't conditionally add an else block to an if statement. "else { print("False") }" is not a valid standalone statement. With the switch statement, there's an additional wrinkle. In the example given above, the compiler tries to evaluate the #if block in the context of the "10" case. Under current rules, the body of a case only ends when the next case keyword, default keyword, or closing brace of the switch statement are encountered. If there's code after a break, only a warning is emitted stating that the code after it will not be executed. If we wanted to allow conditionally adding cases to a switch statement, we'd first have to decide how to differentiate it from just extending a case body. |
PR: #9457 |
Implemented in Swift 4.1 |
test |
Additional Detail from JIRA
Watchers: @shahmishal
md5: ac72c1f2b3e90602adfe008ab2456590
relates to:
Issue Description:
This code should be accepted, but it is rejected now:
The text was updated successfully, but these errors were encountered: