Skip to content
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

Closed
jopamer opened this issue Nov 6, 2015 · 13 comments
Closed

[SR-2] Build configuration directives can not wrap switch cases #42628

jopamer opened this issue Nov 6, 2015 · 13 comments
Assignees
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers improvement parser Area → compiler: The legacy C++ parser

Comments

@jopamer
Copy link
Contributor

jopamer commented Nov 6, 2015

Previous ID SR-2
Radar rdar://problem/18841045
Original Reporter @jopamer
Type Improvement
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 5
Component/s Compiler
Labels Improvement, Parser
Assignee @rintaro
Priority Medium

Watchers: @shahmishal

md5: ac72c1f2b3e90602adfe008ab2456590

relates to:

  • SR-4196 Allow #if (conditional compilation) to guard switch cases

Issue Description:

This code should be accepted, but it is rejected now:

switch 10 {
  case 10:
    break
#if FOO
  case 20:
    break
#endif
}
$ swiftc /tmp/a.swift
/tmp/a.swift:5:3: error: 'case' label can only appear inside a 'switch' statement
  case 20:
  ^
@swift-ci
Copy link
Collaborator

swift-ci commented Dec 6, 2015

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.

@meadori
Copy link
Mannequin

meadori mannequin commented Dec 7, 2015

I signed up for this one on the mailing list.

@meadori
Copy link
Mannequin

meadori mannequin commented Dec 7, 2015

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?

@swift-ci
Copy link
Collaborator

Comment by Alexander Panin (JIRA)

Consider this as an improvement of the grammar for switch statements and for branch statements.

@meadori
Copy link
Mannequin

meadori mannequin commented Feb 22, 2016

I am still looking at this one. Should have an update this week.

@swift-ci
Copy link
Collaborator

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.

@meadori
Copy link
Mannequin

meadori mannequin commented Mar 3, 2016

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.

@meadori
Copy link
Mannequin

meadori mannequin commented Mar 3, 2016

Nevermind, I see there is already a patch in flight for [SR-826].

@hpux735
Copy link
Contributor

hpux735 commented Nov 9, 2016

This bug still exists. For whatever reason, SR-826 did not patch.

@robinkunde
Copy link
Contributor

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.

@rintaro
Copy link
Mannequin

rintaro mannequin commented May 10, 2017

@rintaro
Copy link
Mannequin

rintaro mannequin commented Apr 3, 2018

Implemented in Swift 4.1

@shahmishal
Copy link
Member

test

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added good first issue Good for newcomers feature A feature request or implementation labels Nov 11, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers improvement parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

6 participants