Skip to content

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

Open
@dnadoba

Description

@dnadoba

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    #ifFeature → compiler control statements: Conditional compilation blocksattributesFeature: Declaration and type attributesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcompiler control statementsFeature: compiler control statementsparserArea → compiler: The legacy C++ parserswift 6.0unexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions