Skip to content

Commit 2f24394

Browse files
committed
Downgrade the error about a _compiler_version("1.N.2") to a warning
The error about only a `*` being meaningful in the second position is a warning in the compiler, so match that behavior.
1 parent d5e3717 commit 2f24394

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

Sources/SwiftIfConfig/IfConfigError.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ extension IfConfigError: DiagnosticMessage {
118118

119119
var severity: SwiftDiagnostics.DiagnosticSeverity {
120120
switch self {
121-
case .ignoredTrailingComponents, .likelySimulatorPlatform: return .warning
121+
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents, .likelySimulatorPlatform:
122+
return .warning
122123
default: return .error
123124
}
124125
}

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,11 @@ func evaluateIfConfig(
354354
let versionString = stringSegment.content.text
355355
let expectedVersion: VersionTuple
356356
do {
357-
expectedVersion = try VersionTuple(parsingCompilerBuildVersion: versionString, argExpr)
357+
expectedVersion = try VersionTuple.parseCompilerBuildVersion(
358+
versionString,
359+
argExpr,
360+
extraDiagnostics: &extraDiagnostics
361+
)
358362
} catch {
359363
return recordError(error, at: stringSegment.content)
360364
}

Sources/SwiftIfConfig/VersionTuple+Parsing.swift

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SwiftDiagnostics
1314
import SwiftSyntax
1415

1516
extension VersionTuple {
@@ -20,11 +21,35 @@ extension VersionTuple {
2021
/// we are parsing.
2122
/// - versionSyntax: The syntax node that contains the version string, used
2223
/// only for diagnostic purposes.
23-
init(
24-
parsingCompilerBuildVersion versionString: String,
24+
static func parseCompilerBuildVersion(
25+
_ versionString: String,
2526
_ versionSyntax: ExprSyntax
26-
) throws {
27-
components = []
27+
) -> (version: VersionTuple?, diagnostics: [Diagnostic]) {
28+
var extraDiagnostics: [Diagnostic] = []
29+
let version: VersionTuple?
30+
do {
31+
version = try parseCompilerBuildVersion(versionString, versionSyntax, extraDiagnostics: &extraDiagnostics)
32+
} catch {
33+
version = nil
34+
extraDiagnostics.append(contentsOf: error.asDiagnostics(at: versionSyntax))
35+
}
36+
37+
return (version, extraDiagnostics)
38+
}
39+
40+
/// Parse a compiler build version of the form "5007.*.1.2.3*", which is
41+
/// used by an older if configuration form `_compiler_version("...")`.
42+
/// - Parameters:
43+
/// - versionString: The version string for the compiler build version that
44+
/// we are parsing.
45+
/// - versionSyntax: The syntax node that contains the version string, used
46+
/// only for diagnostic purposes.
47+
static func parseCompilerBuildVersion(
48+
_ versionString: String,
49+
_ versionSyntax: ExprSyntax,
50+
extraDiagnostics: inout [Diagnostic]
51+
) throws -> VersionTuple {
52+
var components: [Int] = []
2853

2954
// Version value are separated by periods.
3055
let componentStrings = versionString.split(separator: ".", omittingEmptySubsequences: false)
@@ -49,7 +74,9 @@ extension VersionTuple {
4974
// The second component is always "*", and is never used for comparison.
5075
if index == 1 {
5176
if componentString != "*" {
52-
throw IfConfigError.compilerVersionSecondComponentNotWildcard(syntax: versionSyntax)
77+
extraDiagnostics.append(
78+
IfConfigError.compilerVersionSecondComponentNotWildcard(syntax: versionSyntax).asDiagnostic
79+
)
5380
}
5481
try recordComponent(0)
5582
continue
@@ -102,5 +129,7 @@ extension VersionTuple {
102129
}
103130
components[0] = components[0] / 1000
104131
}
132+
133+
return VersionTuple(components: components)
105134
}
106135
}

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ public class EvaluateTests: XCTestCase {
230230
)
231231
]
232232
)
233+
234+
assertIfConfig(
235+
#"_compiler_version("5.7.100")"#,
236+
.active,
237+
diagnostics: [
238+
DiagnosticSpec(
239+
message: "the second version component is not used for comparison in legacy compiler versions",
240+
line: 1,
241+
column: 19,
242+
severity: .warning
243+
)
244+
]
245+
)
233246
}
234247

235248
func testCanImport() throws {

0 commit comments

Comments
 (0)