Skip to content

Commit 8a2ffd5

Browse files
authored
Merge pull request #2772 from DougGregor/ifconfig-fixes
Minor fixes for SwiftIfConfig
2 parents d24bbd5 + 19445c0 commit 8a2ffd5

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ func evaluateIfConfig(
109109
if let prefixOp = condition.as(PrefixOperatorExprSyntax.self),
110110
prefixOp.operator.text == "!"
111111
{
112-
let (innerActive, innersyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig(
112+
let (innerActive, innerSyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig(
113113
condition: prefixOp.expression,
114114
configuration: configuration
115115
)
116116

117-
return (active: !innerActive, syntaxErrorsAllowed: innersyntaxErrorsAllowed, diagnostics: innerDiagnostics)
117+
return (active: !innerActive, syntaxErrorsAllowed: innerSyntaxErrorsAllowed, diagnostics: innerDiagnostics)
118118
}
119119

120120
// Logical '&&' and '||'.
@@ -334,13 +334,7 @@ func evaluateIfConfig(
334334
let segment = stringLiteral.segments.first,
335335
case .stringSegment(let stringSegment) = segment
336336
else {
337-
return recordError(
338-
.requiresUnlabeledArgument(
339-
name: "_compiler_version",
340-
role: "version",
341-
syntax: ExprSyntax(call)
342-
)
343-
)
337+
return doVersionComparisonCheck(configuration.compilerVersion)
344338
}
345339

346340
let versionString = stringSegment.content.text

Sources/SwiftIfConfig/IfConfigRegionState.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public enum IfConfigRegionState {
2525
case active
2626

2727
/// Evaluate the given `#if` condition using the given build configuration
28-
/// to determine its state and identify any problems encountered along the
29-
/// way.
28+
/// to determine its state, whether syntax errors in inactive conditions are
29+
/// permitted, and to identify any problems encountered along the way.
3030
public static func evaluating(
3131
_ condition: some ExprSyntaxProtocol,
3232
in configuration: some BuildConfiguration
33-
) -> (state: IfConfigRegionState, diagnostics: [Diagnostic]) {
33+
) -> (state: IfConfigRegionState, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
3434
// Apply operator folding for !/&&/||.
3535
var foldingDiagnostics: [Diagnostic] = []
3636
let foldedCondition = OperatorTable.logicalOperators.foldAll(condition) { error in
@@ -44,9 +44,9 @@ public enum IfConfigRegionState {
4444

4545
let diagnostics = foldingDiagnostics + evalDiagnostics
4646
switch (active, syntaxErrorsAllowed) {
47-
case (true, _): return (.active, diagnostics)
48-
case (false, false): return (.inactive, diagnostics)
49-
case (false, true): return (.unparsed, diagnostics)
47+
case (true, _): return (.active, syntaxErrorsAllowed, diagnostics)
48+
case (false, false): return (.inactive, syntaxErrorsAllowed, diagnostics)
49+
case (false, true): return (.unparsed, syntaxErrorsAllowed, diagnostics)
5050
}
5151
}
5252
}

Sources/SwiftIfConfig/VersionTuple+Parsing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension VersionTuple {
2727
components = []
2828

2929
// Version value are separated by periods.
30-
let componentStrings = versionString.split(separator: ".")
30+
let componentStrings = versionString.split(separator: ".", omittingEmptySubsequences: false)
3131

3232
/// Record a component after checking its value.
3333
func recordComponent(_ value: Int) throws {

Sources/SwiftIfConfig/VersionTuple.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct VersionTuple: Sendable {
3333
public init?(parsing string: String) {
3434
self.components = []
3535

36-
for componentText in string.split(separator: ".") {
36+
for componentText in string.split(separator: ".", omittingEmptySubsequences: false) {
3737
guard let component = Int(componentText) else {
3838
return nil
3939
}

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,25 @@ public class EvaluateTests: XCTestCase {
176176
assertIfConfig("swift(>=5.5)", .active)
177177
assertIfConfig("swift(<6)", .active)
178178
assertIfConfig("swift(>=6)", .unparsed)
179+
assertIfConfig(
180+
"swift(>=...)",
181+
.unparsed,
182+
diagnostics: [
183+
DiagnosticSpec(
184+
message: "'swift' version check has invalid version ''",
185+
line: 1,
186+
column: 9
187+
)
188+
]
189+
)
179190
assertIfConfig("compiler(>=5.8)", .active)
180191
assertIfConfig("compiler(>=5.9)", .active)
181192
assertIfConfig("compiler(>=5.10)", .unparsed)
182193
assertIfConfig(#"_compiler_version("5009.*.1")"#, .active)
183194
assertIfConfig(#"_compiler_version("5009.*.3.2.3")"#, .unparsed)
184195
assertIfConfig(#"_compiler_version("5010.*.0")"#, .unparsed)
196+
assertIfConfig("_compiler_version(>=5.8)", .active)
197+
assertIfConfig("_compiler_version(>=12.0)", .unparsed)
185198
assertIfConfig("compiler(>=5.10) && 3.14159", .unparsed)
186199
assertIfConfig(
187200
"compiler(>=5.10) || 3.14159",
@@ -206,6 +219,17 @@ public class EvaluateTests: XCTestCase {
206219
)
207220
]
208221
)
222+
assertIfConfig(
223+
#"_compiler_version("...")"#,
224+
.unparsed,
225+
diagnostics: [
226+
DiagnosticSpec(
227+
message: "found empty version component",
228+
line: 1,
229+
column: 20
230+
)
231+
]
232+
)
209233
}
210234

211235
func testCanImport() throws {
@@ -269,7 +293,7 @@ fileprivate func assertIfConfig(
269293
// Evaluate the condition to check the state.
270294
let actualDiagnostics: [Diagnostic]
271295
let actualState: IfConfigRegionState
272-
(actualState, actualDiagnostics) = IfConfigRegionState.evaluating(condition, in: configuration)
296+
(actualState, _, actualDiagnostics) = IfConfigRegionState.evaluating(condition, in: configuration)
273297
XCTAssertEqual(actualState, expectedState, file: file, line: line)
274298

275299
// Check the diagnostics.

0 commit comments

Comments
 (0)