Skip to content

Commit f35c9e8

Browse files
committed
[Build configuration] Drop optionality from protocol requirement result types
The optional return was used to mean "don't know", but was always treated as false. Instead, make all of the result types non-optional, and allow these operations to throw to indicate failure. While here, drop the "syntax" parameters to all of these functions. We shouldn't be working with syntax inside the build configuration.
1 parent 431fe8a commit f35c9e8

File tree

3 files changed

+78
-117
lines changed

3 files changed

+78
-117
lines changed

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
import SwiftSyntax
1312

1413
/// Describes the ordering of a sequence of bytes that make up a word of
1514
/// storage for a particular architecture.
@@ -43,18 +42,17 @@ public enum CanImportVersion {
4342
/// Providing complete build configuration information effectively requires
4443
/// a Swift compiler, because (for example) determining whether a module can
4544
/// be imported is a complicated task only implemented in the Swift compiler.
46-
/// Therefore, many of the queries return `Bool?`, where `nil` indicates
47-
/// that the answer is not known. Clients that don't have a lot of context
48-
/// (such as an IDE that does not have access to the compiler command line)
49-
/// can return `nil`.
45+
/// Therefore, queries are permitted to throw an error to report when they
46+
/// cannot answer a query, in which case this error will be reported to
47+
/// the caller.
5048
public protocol BuildConfiguration {
5149
/// Determine whether a given custom build condition has been set.
5250
///
5351
/// Custom build conditions can be set by the `-D` command line option to
5452
/// the Swift compiler. For example, `-DDEBUG` sets the custom condition
5553
/// named `DEBUG`, which could be checked with, e.g.,
5654
///
57-
/// ```
55+
/// ```swift
5856
/// #if DEBUG
5957
/// // ...
6058
/// #endif
@@ -63,53 +61,49 @@ public protocol BuildConfiguration {
6361
/// - Parameters:
6462
/// - name: The name of the custom build condition being checked (e.g.,
6563
/// `DEBUG`.
66-
/// - syntax: The syntax node for the name of the custom build
67-
/// configuration.
6864
/// - Returns: Whether the custom condition is set.
69-
func isCustomConditionSet(name: String, syntax: ExprSyntax) -> Bool?
65+
func isCustomConditionSet(name: String) throws -> Bool
7066

7167
/// Determine whether the given feature is enabled.
7268
///
7369
/// Features are determined by the Swift compiler, language mode, and other
7470
/// options such as `--enable-upcoming-feature`, and can be checked with
7571
/// the `hasFeature` syntax, e.g.,
7672
///
77-
/// ```
73+
/// ```swift
7874
/// #if hasFeature(VariadicGenerics)
7975
/// // ...
8076
/// #endif
8177
/// ```
8278
///
8379
/// - Parameters:
8480
/// - name: The name of the feature being checked.
85-
/// - syntax: The syntax node for the `hasFeature(<name>)`.
8681
/// - Returns: Whether the requested feature is available.
87-
func hasFeature(name: String, syntax: ExprSyntax) -> Bool?
82+
func hasFeature(name: String) throws -> Bool
8883

8984
/// Determine whether the given attribute is available.
9085
///
9186
/// Attributes are determined by the Swift compiler. They can be checked
9287
/// with `hasAttribute` syntax, e.g.,
9388
///
94-
/// ```
89+
/// ```swift
9590
/// #if hasAttribute(available)
9691
/// // ...
9792
/// #endif
9893
/// ```
9994
///
10095
/// - Parameters:
10196
/// - name: The name of the attribute being queried.
102-
/// - syntax: The syntax node for the `hasAttribute(<name>)`.
10397
/// - Returns: Whether the requested attribute is supported.
104-
func hasAttribute(name: String, syntax: ExprSyntax) -> Bool?
98+
func hasAttribute(name: String) throws -> Bool
10599

106100
/// Determine whether a module with the given import path can be imported,
107101
/// with additional version information.
108102
///
109103
/// The availability of a module for import can be checked with `canImport`,
110104
/// e.g.,
111105
///
112-
/// ```
106+
/// ```swift
113107
/// #if canImport(UIKit)
114108
/// // ...
115109
/// #endif
@@ -126,142 +120,143 @@ public protocol BuildConfiguration {
126120
/// - version: The version restriction on the imported module. For the
127121
/// normal `canImport(<import-path>)` syntax, this will always be
128122
/// `CanImportVersion.unversioned`.
129-
/// - syntax: The syntax node for the `canImport` expression.
130123
/// - Returns: Whether the module can be imported.
131-
func canImport(importPath: [String], version: CanImportVersion, syntax: ExprSyntax) -> Bool?
124+
func canImport(importPath: [String], version: CanImportVersion) throws -> Bool
132125

133126
/// Determine whether the given name is the active target OS (e.g., Linux, iOS).
134127
///
135128
/// The target operating system can be queried with `os(<name>)`, e.g.,
136129
///
137-
/// ```
130+
/// ```swift
138131
/// #if os(Linux)
139132
/// // Linux-specific implementation
140133
/// #endif
134+
/// ```
141135
///
142136
/// - Parameters:
143137
/// - name: The name of the operating system being queried, such as `Linux`,
144138
/// `Windows`, `macOS`, etc.
145-
/// - syntax: The syntax node for the `os(<name>)` expression.
146139
/// - Returns: Whether the given operating system name is the target operating
147140
/// system, i.e., the operating system for which code is being generated.
148-
func isActiveTargetOS(name: String, syntax: ExprSyntax) -> Bool?
141+
func isActiveTargetOS(name: String) throws -> Bool
149142

150143
/// Determine whether the given name is the active target architecture
151144
/// (e.g., x86_64, arm64).
152145
///
153146
/// The target processor architecture can be queried with `arch(<name>)`, e.g.,
154147
///
155-
/// ```
148+
/// ```swift
156149
/// #if arch(x86_64)
157150
/// // 64-bit x86 Intel-specific code
158151
/// #endif
152+
/// ```
159153
///
160154
/// - Parameters:
161155
/// - name: The name of the target architecture to check.
162-
/// - syntax: The syntax node for the `arch(<name>)` expression.
163156
/// - Returns: Whether the given processor architecture is the target
164157
/// architecture.
165-
func isActiveTargetArchitecture(name: String, syntax: ExprSyntax) -> Bool?
158+
func isActiveTargetArchitecture(name: String) throws -> Bool
166159

167160
/// Determine whether the given name is the active target environment (e.g., simulator)
168161
///
169162
/// The target environment can be queried with `targetEnvironment(<name>)`,
170163
/// e.g.,
171164
///
172-
/// ```
165+
/// ```swift
173166
/// #if targetEnvironment(simulator)
174167
/// // Simulator-specific code
175168
/// #endif
169+
/// ```
176170
///
177171
/// - Parameters:
178172
/// - name: The name of the target environment to check.
179-
/// - syntax: The syntax node for the `targetEnvironment(<name>)`
180-
/// expression.
181173
/// - Returns: Whether the target platform is for a specific environment,
182174
/// such as a simulator or emulator.
183-
func isActiveTargetEnvironment(name: String, syntax: ExprSyntax) -> Bool?
175+
func isActiveTargetEnvironment(name: String) throws -> Bool
184176

185177
/// Determine whether the given name is the active target runtime (e.g., _ObjC vs. _Native)
186178
///
187179
/// The target runtime can only be queried by an experimental syntax
188180
/// `_runtime(<name>)`, e.g.,
189181
///
182+
/// ```swift
190183
/// #if _runtime(_ObjC)
191184
/// // Code that depends on Swift being built for use with the Objective-C
192185
/// // runtime, e.g., on Apple platforms.
193186
/// #endif
187+
/// ```
194188
///
195189
/// The only other runtime is "none", when Swift isn't tying into any other
196190
/// specific runtime.
197191
///
198192
/// - Parameters:
199193
/// - name: The name of the runtime.
200-
/// - syntax: The syntax node for the `_runtime(<name>)` expression.
201194
/// - Returns: Whether the target runtime matches the given name.
202-
func isActiveTargetRuntime(name: String, syntax: ExprSyntax) -> Bool?
195+
func isActiveTargetRuntime(name: String) throws -> Bool
203196

204197
/// Determine whether the given name is the active target pointer authentication scheme (e.g., arm64e).
205198
///
206199
/// The target pointer authentication scheme describes how pointers are
207200
/// signed, as a security mitigation. This scheme can only be queried by
208201
/// an experimental syntax `_ptrath(<name>)`, e.g.,
209202
///
210-
/// ```
203+
/// ```swift
211204
/// #if _ptrauth(arm64e)
212205
/// // Special logic for arm64e pointer signing
213206
/// #endif
214-
///
207+
/// ```
215208
/// - Parameters:
216209
/// - name: The name of the pointer authentication scheme to check.
217-
/// - syntax: The syntax node for the `_ptrauth(<name>)` expression.
218210
/// - Returns: Whether the code generated for the target will use the given
219211
/// pointer authentication scheme.
220-
func isActiveTargetPointerAuthentication(name: String, syntax: ExprSyntax) -> Bool?
212+
func isActiveTargetPointerAuthentication(name: String) throws -> Bool
221213

222214
/// The bit width of a data pointer for the target architecture.
223215
///
224216
/// The target's pointer bit with (which also corresponds to the number of
225217
/// bits in `Int`/`UInt`) can only be queried with the experimental syntax
226218
/// `_pointerBitWidth(_<bitwidth>)`, e.g.,
227219
///
228-
/// ```
220+
/// ```swift
229221
/// #if _pointerBitWidth(32)
230222
/// // 32-bit system
231223
/// #endif
232-
var targetPointerBitWidth: Int? { get }
224+
/// ```
225+
var targetPointerBitWidth: Int { get }
233226

234227
/// The endianness of the target architecture.
235228
///
236229
/// The target's endianness can onyl be queried with the experimental syntax
237230
/// `_endian(<name>)`, where `<name>` can be either "big" or "little", e.g.,
238231
///
232+
/// ```swift
239233
/// #if _endian(little)
240234
/// // Swap some bytes around for network byte order
241235
/// #endif
242-
var endianness: Endianness? { get }
236+
/// ```
237+
var endianness: Endianness { get }
243238

244239
/// The effective language version, which can be set by the user (e.g., 5.0).
245240
///
246241
/// The language version can be queried with the `swift` directive that checks
247242
/// how the supported language version compares, as described by
248243
/// [SE-0212](https://github.com/apple/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
249244
///
250-
/// ```
245+
/// ```swift
251246
/// #if swift(>=5.5)
252247
/// // Hooray, we can use tasks!
253248
/// ```
254-
var languageVersion: VersionTuple? { get }
249+
var languageVersion: VersionTuple { get }
255250

256251
/// The version of the compiler (e.g., 5.9).
257252
///
258253
/// The compiler version can be queried with the `compiler` directive that
259254
/// checks the specific version of the compiler being used to process the
260255
/// code, e.g.,
261256
///
262-
/// ```
257+
/// ```swift
263258
/// #if compiler(>=5.7)
264259
/// // Hoorway, we can implicitly open existentials!
265260
/// #endif
266-
var compilerVersion: VersionTuple? { get }
261+
var compilerVersion: VersionTuple { get }
267262
}

0 commit comments

Comments
 (0)