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

#if swift(...) nested within #if canImport tries to parse attributes #60876

Open
dnadoba opened this issue Aug 31, 2022 · 6 comments
Open

#if swift(...) nested within #if canImport tries to parse attributes #60876

dnadoba opened this issue Aug 31, 2022 · 6 comments
Labels
attributes Feature: Declaration and type attributes bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler directives Feature: compiler directives (#if, #warning, #error, etc.) compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser unexpected error Bug: Unexpected error

Comments

@dnadoba
Copy link
Member

dnadoba commented Aug 31, 2022

Describe the bug
This does not compile:

#if canImport(SomeModuleThatDoesNotExists)
struct Foo {}
#if swift(>=5.99)
extension Foo: @someUnkownAttribute {} // error: Expected type
#endif
#endif

But this compiles just fine

struct Foo {}
#if swift(>=5.99)
extension Foo: @someUnkownAttribute {} 
#endif

as well as this:

struct Foo {}

#if swift(>=5.99) && canImport(SomeModuleThatDoesNotExists)
extension Foo: @someUnkownAttribute {}
#endif

Steps To Reproduce
try to compile the above code

Expected behavior
All variations compile without errors

Environment (please fill out the following information)

  • OS: 13.0 Beta (22A5331e)
  • Xcode Version 14.0 beta 6 (14A5294g) but with Toolchain: Swift Development Snapshot 2022-08-18 (a)
  • also tested in docker with swiftlang/swift:nightly-main-focal

Additional context
Original issue found in the Swift 5.4 compiler with a code snipped similar to this:

#if canImport(Network)
class Foo {}

#if swift(>=5.5) && canImport(_Concurrency)
extension Foo: @unchecked Sendable {} // error: unknown attribute 'unchecked'
#endif
#endif

We now use this workaround which compiles just fine with Swift 5.4:

#if canImport(Network)
class Foo {}
#endif

#if swift(>=5.5) && canImport(_Concurrency) && canImport(Network)
extension Foo: @unchecked Sendable {}
#endif
@dnadoba dnadoba added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Aug 31, 2022
@hassila
Copy link

hassila commented Apr 17, 2023

Also encountered what seems to be the same issue with Swift 5.7:

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
   import CDarwinOperatingSystemStats
   import Darwin
   import Dispatch

#if swift(>=5.8)
@_documentation(visibility: internal)
#endif
   final class OperatingSystemStatsProducer {
       var nsPerMachTick: Double
       var nsPerSchedulerTick: Int

       let lock = NIOLock()
...
#endif

(Swift 5.8 seems ok - fail here: https://swiftpackageindex.com/builds/E19A3504-F6EA-49D3-B0C3-31A7624991ED)

In my case it is #if swift() nested in #if os() though, so just similar, but not identical.

hassila added a commit to ordo-one/package-benchmark that referenced this issue Apr 17, 2023
hassila added a commit to ordo-one/package-benchmark that referenced this issue Apr 17, 2023
Workaround apple/swift#60876 so we can build with Swift 5.7 again
@AnthonyLatsis AnthonyLatsis added parser Area → compiler: The legacy C++ parser compiler The Swift compiler in itself compiler directives Feature: compiler directives (#if, #warning, #error, etc.) unexpected error Bug: Unexpected error attributes Feature: Declaration and type attributes labels Apr 21, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Apr 21, 2023

@dnadoba Apparently this was fixed a while ago. Your examples from the additional context compile fine, and the other three are rejected, which I believe to be expected, since extension Foo: @someUnkownAttribute {} is not a valid syntactic production.

Tested with Swift version 5.9-dev (LLVM 91080e65df791d6, Swift c1d5118).

@AnthonyLatsis
Copy link
Collaborator

@hassila Swift 5.7 does not support #ifs around attributes. SE-0367 was implemented in 5.8, hence your observation.

@dnadoba
Copy link
Member Author

dnadoba commented Apr 21, 2023

@AnthonyLatsis I'm not sure I follow.
This compiles:

struct Foo {}
#if swift(>=5.99) && canImport(SomeModuleThatDoesNotExists)
extension Foo: @someUnkownAttribute {}
#endif

but this doesn't:

#if canImport(SomeModuleThatDoesNotExists)
struct Foo {}
#if swift(>=5.99)
extension Foo: @someUnkownAttribute {} // error: Expected type
#endif
#endif

The only difference is that we are nesting the #ifs.
(tested with swift-driver version: 1.80.1 Apple Swift version 5.9 (swiftlang-5.9.0.108.11 clang-1500.0.19.2))

Even if it isn't valid syntax, shouldn't it be consistently rejected?

@AnthonyLatsis
Copy link
Collaborator

Ah, sorry, -dump-parse was giving me a false impression. The first one really is accepted.

@AnthonyLatsis AnthonyLatsis reopened this Apr 21, 2023
@dnadoba
Copy link
Member Author

dnadoba commented Apr 21, 2023

rdar://108138906

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
attributes Feature: Declaration and type attributes bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler directives Feature: compiler directives (#if, #warning, #error, etc.) compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser unexpected error Bug: Unexpected error
Projects
None yet
Development

No branches or pull requests

3 participants