Skip to content

Commit 2d6b282

Browse files
committed
[Version parsing] Fix precondition failure with no characters in between .'s
Uncovered during compiler testing.
1 parent 73cfd08 commit 2d6b282

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ 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)
@@ -206,6 +217,17 @@ public class EvaluateTests: XCTestCase {
206217
)
207218
]
208219
)
220+
assertIfConfig(
221+
#"_compiler_version("...")"#,
222+
.unparsed,
223+
diagnostics: [
224+
DiagnosticSpec(
225+
message: "found empty version component",
226+
line: 1,
227+
column: 20
228+
)
229+
]
230+
)
209231
}
210232

211233
func testCanImport() throws {

0 commit comments

Comments
 (0)