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-9998] #if embedded in an if-else does not behave as expected #52401

Open
swift-ci opened this issue Feb 25, 2019 · 2 comments
Open

[SR-9998] #if embedded in an if-else does not behave as expected #52401

swift-ci opened this issue Feb 25, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9998
Radar None
Original Reporter jestyjest (JIRA User)
Type Bug
Environment

Xcode 10.1, building for simulator, Swift 4

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Parser
Assignee None
Priority Medium

md5: 2afc1773f4650f1c632b59b1bc75e22e

Issue Description:

// MARK: Compiles
if !true {
    print("this does not make sense")
} else if true {
    print("we double checked, and all is well")
} else {
    print("all is well")
}
 
// MARK: Does not compile

if !true {
    print("this does not make sense")
} // ----- ERROR: Expected expression
#if DEBUG
else if true {
    print("we double checked, and all is well")
}
#endif
else { // ----- ERROR: Closure expression is unused
    print("all is well")
}
 
// MARK: Workaround; compiles

if !true {
    print("this does not make sense")
} else {
    #if DEBUG
    if true {
        print("we double checked, and all is well")
    }
    #endif
    print("all is well")
}

The above reproduces an issue I'm having with preprocessor directives/conditional compilation

@belkadan
Copy link
Contributor

Swift's #if isn't the same as C's; it only works around an entire "unit" of the AST (usually a set of declarations or statements). However, it does work for switch cases, so it's possible we could make it work for else if as well. If not, we could at least improve the diagnostic.

cc @rintaro

@swift-ci
Copy link
Collaborator Author

Comment by Sam Warner (JIRA)

@belkadan ah right, that makes perfect sense, thanks for the explanation. I did wonder if I was unreasonably expecting C-like conditional compilation.

In terms of impact for our project, this is small (due to the workaround above), but an improved diagnostic/having this problem covered in the Swift docs would be very helpful.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

2 participants