Skip to content

Commit 985b56f

Browse files
authored
[PackageModel] Toolchain: A few fixes for features supported by Swift compiler (#8761)
### Motivation: - Fix "supported features" parsing with older toolchains. `-print-supported-features` flag used to output "enabled_in" version as an integer (major only). - Handle absence of optional features section. `JSON.get` would throw if the key couldn't be found, let's handle that gracefully for an optional "optional features" section. ### Modifications: - Update `Toolchain.swiftCompilerSupportedFeatures` to use `try?` when querying JSON for an optional "optional" key. - Update `Toolchain.swiftCompilerSupportedFeatures` to attempt to retrieve "enabled_in" key as a String first and if that fails - as an integer. ### Result: `swift package migrate` can now support older toolchains that have "enabled_in" printed as an integer and have no "optional features" section in their `-print-supported-features` output.
1 parent c1af525 commit 985b56f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/PackageModel/Toolchain+SupportedFeatures.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ extension Toolchain {
102102

103103
let features: JSON = try parsedSupportedFeatures.get("features")
104104

105-
let optional: [SwiftCompilerFeature] = try (features.get("optional") as [JSON]?)?.map { (json: JSON) in
105+
let optionalFeatures = (try? features.getArray("optional")) ?? []
106+
107+
let optional: [SwiftCompilerFeature] = try optionalFeatures.map { json in
106108
let name: String = try json.get("name")
107109
let categories: [String]? = try json.getArrayIfAvailable("categories")
108110
let migratable: Bool? = json.get("migratable")
@@ -114,13 +116,17 @@ extension Toolchain {
114116
categories: categories ?? [name],
115117
flagName: flagName
116118
)
117-
} ?? []
119+
}
118120

119121
let upcoming: [SwiftCompilerFeature] = try features.getArray("upcoming").map {
120122
let name: String = try $0.get("name")
121123
let categories: [String]? = try $0.getArrayIfAvailable("categories")
122124
let migratable: Bool? = $0.get("migratable")
123-
let enabledIn: String = try $0.get("enabled_in")
125+
let enabledIn = if let version = try? $0.get(String.self, forKey: "enabled_in") {
126+
version
127+
} else {
128+
try String($0.get(Int.self, forKey: "enabled_in"))
129+
}
124130

125131
guard let mode = SwiftLanguageVersion(string: enabledIn) else {
126132
throw InternalError("Unknown swift language mode: \(enabledIn)")

0 commit comments

Comments
 (0)