swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.66k stars 10.38k forks source link

[SR-3331] Declaration attributes do not work within compilation directives #45919

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-3331
Radar None
Original Reporter wattson12 (JIRA User)
Type Bug

Attachment: Download

Environment Xcode Version 8.1 (8B62) Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1) Target: x86_64-apple-macosx10.9
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, LanguageFeatureRequest | |Assignee | None | |Priority | Medium | md5: 83d297322aea89dcb9bbe5587fb84a02

Issue Description:

I'm trying to use the @available attribute to mark some code as unavailable based on build settings (the use case here is feature toggling)

When the attribute is part of a complication directive `#if ... #endif` the code breaks with the error "expected declaration" shown on the #endif line

Playground attached but full code here as well:

import UIKit

#if DEBUG
    @available(*, unavailable)
#endif

class Demo { }
CodaFi commented 7 years ago

Conditional compilation expressions are a function of the compiler, and not a preprocessor like in C or C++. They only apply at statement or declaration boundaries.

swift-ci commented 2 years ago

Comment by Martin Redington (JIRA)

I was about to open an issue for this, but found this one when searching for existing reports. My use case is

{{public protocol Disposable {
/// Dispose resource.
func dispose()
}

#if swift(>=5.4)
@resultBuilder
#else
@_functionBuilder
#endif
public struct DisposableBuilder {
public static func buildBlock(_ disposables: Disposable...) -> [Disposable] {
return disposables
}
}
}}

It is very annoying to have to duplicate (at least some of) the struct declaration within each branch of the compiler directive.