-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Infra] Remove outdated availability and Swift version guards #1711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Analyzed project deployment targets and removed unnecessary @available, #available, and #if swift guards throughout the codebase. - iOS deployment target standardized at 15.0. - macOS deployment target baseline at 11.0. - tvOS deployment target baseline at 15.0. - watchOS deployment target baseline at 7.0. Removed #if swift(>=X.X) blocks by migrating the code for the newer Swift version into the enclosing scope and deleting the #else blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @ncooke3, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request focuses on infrastructure improvements by systematically removing outdated availability and Swift version guards. By aligning the codebase with updated minimum deployment targets and a newer Swift version, the changes simplify the code, enhance readability, and ensure that the project consistently leverages modern platform features and Swift's concurrency model.
Highlights
- Code Modernization: Removed numerous
@available
and#available
checks throughout the codebase, simplifying the code by eliminating conditional compilation for older OS versions. - Swift Version Alignment: Eliminated
#if swift(>=X.X)
blocks, particularly those related to Swift 5.5 and concurrency features, by migrating the code for the newer Swift version into the enclosing scope. This standardizes the codebase on a more recent Swift version. - Deployment Target Simplification: Streamlined the codebase by removing code paths and availability guards for older OS versions, aligning with the updated minimum deployment targets: iOS 15.0, macOS 11.0, tvOS 15.0, and watchOS 7.0.
- Concurrency Adoption: Directly adopted Swift's
async/await
patterns in several areas where they were previously guarded by#available
checks, indicating full embrace of these modern concurrency features.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request successfully removes several outdated availability and Swift version guards based on the updated deployment targets. However, critical issues were identified where #available
checks were removed for APIs (.task
, .refreshable
, URLSession.shared.data(from:)
) that require higher OS versions than the stated deployment targets for macOS and watchOS, which will lead to runtime crashes on those platforms. Additionally, a Swift compiler version check was removed from around async functions, which will cause compilation failures on older Swift versions.
abtesting/Shared/ContentView.swift
Outdated
BasicList(data: data) | ||
.preferredColorScheme(appConfig.colorScheme) | ||
.foregroundColor(appConfig.colorScheme == .dark ? .orange : .primary) | ||
.task { await appConfig.updateFromRemoteConfigAsync() } | ||
.refreshable { await appConfig.updateFromRemoteConfigAsync() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The .task
and .refreshable
view modifiers were introduced in iOS 15, tvOS 15, macOS 12, and watchOS 8. The pull request description states that the macOS deployment target is 11.0 and the watchOS deployment target is 7.0. Removing the #available
check means this code will be compiled and potentially run on macOS 11 and watchOS 7, where these modifiers are not available, leading to a runtime crash.
if #available(iOS 15, tvOS 15, macOS 12, watchOS 8, *) {
BasicList(data: data)
.preferredColorScheme(appConfig.colorScheme)
.foregroundColor(appConfig.colorScheme == .dark ? .orange : .primary)
.task { await appConfig.updateFromRemoteConfigAsync() }
.refreshable { await appConfig.updateFromRemoteConfigAsync() }
} else {
BasicList(data: data)
.preferredColorScheme(appConfig.colorScheme)
.foregroundColor(appConfig.colorScheme == .dark ? .orange : .primary)
.onAppear { appConfig.updateFromRemoteConfig() }
}
#if compiler(>=5.5) && canImport(_Concurrency) | ||
Task { await | ||
post.didTapStarButtonAsync() | ||
} | ||
#else | ||
post.didTapStarButton() | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code included an #available(iOS 15, macOS 12, *)
check before calling the async version post.didTapStarButtonAsync()
. While Task { ... }
is available from macOS 10.15, the didTapStarButtonAsync
function itself might be using APIs that are only available from macOS 12 (similar to URLSession.shared.data(from:)
in Process.swift
). Given the macOS deployment target is 11.0, removing this #available
check could lead to a runtime crash on macOS 11 if didTapStarButtonAsync
relies on macOS 12+ APIs. It's safer to either keep the #available
check or confirm that didTapStarButtonAsync
is compatible with macOS 11.
if #available(iOS 15, macOS 12, *) {
#if compiler(>=5.5) && canImport(_Concurrency)
Task { await
post.didTapStarButtonAsync()
}
#else
post.didTapStarButton()
#endif
} else {
post.didTapStarButton()
}
print("Failed to create scaling filter.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
guard let observation = request.results?.first else { | ||
print("Failed to generate saliency map.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
|
||
scaleFilter.setValue(inputImage, forKey: kCIInputImageKey) | ||
scaleFilter.setValue(scale, forKey: kCIInputScaleKey) | ||
scaleFilter.setValue(aspectRatio, forKey: kCIInputAspectRatioKey) | ||
let inputImage = CIImage(cvPixelBuffer: observation.pixelBuffer) | ||
let scale = Double(ciImage.extent.height) / Double(inputImage.extent.height) | ||
let aspectRatio = Double(ciImage.extent.width) / Double(ciImage.extent.height) | ||
|
||
guard let scaledImage = scaleFilter.outputImage else { | ||
print("Failed to scale saliency map.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
guard let scaleFilter = CIFilter(name: "CILanczosScaleTransform") else { | ||
print("Failed to create scaling filter.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
|
||
let saliencyImage = context.createCGImage(scaledImage, from: scaledImage.extent) | ||
scaleFilter.setValue(inputImage, forKey: kCIInputImageKey) | ||
scaleFilter.setValue(scale, forKey: kCIInputScaleKey) | ||
scaleFilter.setValue(aspectRatio, forKey: kCIInputAspectRatioKey) | ||
|
||
guard let saliencyMap = saliencyImage else { | ||
print("Failed to convert saliency map to correct format.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
guard let scaledImage = scaleFilter.outputImage else { | ||
print("Failed to scale saliency map.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
|
||
let saliencyImage = context.createCGImage(scaledImage, from: scaledImage.extent) | ||
|
||
await updateSaliencyMapAsync(to: UIImage(cgImage: saliencyMap)) | ||
await updateStatusAsync(to: .success(.saliencyMap)) | ||
guard let saliencyMap = saliencyImage else { | ||
print("Failed to convert saliency map to correct format.") | ||
await updateStatusAsync(to: .failure(.saliencyMap)) | ||
return | ||
} | ||
|
||
@available(iOS 15, tvOS 15, *) | ||
func uploadSaliencyMapAsync(compressionQuality: CGFloat = 0.5) async { | ||
guard let jpg = saliencyMap?.jpegData(compressionQuality: compressionQuality) else { | ||
print("Could not convert saliency map into correct format.") | ||
await updateStatusAsync(to: .failure(.upload), updateUploadStatus: true) | ||
return | ||
} | ||
await updateSaliencyMapAsync(to: UIImage(cgImage: saliencyMap)) | ||
await updateStatusAsync(to: .success(.saliencyMap)) | ||
} | ||
|
||
let metadata = StorageMetadata() | ||
metadata.contentType = "image/jpeg" | ||
let reference = Storage.storage().reference().child("saliency_map.jpg") | ||
await updateStatusAsync(to: .running(.upload)) | ||
|
||
do { | ||
let response = try await reference.putDataAsync(jpg, metadata: metadata) | ||
print("Upload response metadata: \(response)") | ||
await updateStatusAsync(to: .success(.upload), updateUploadStatus: true) | ||
} catch { | ||
print("Error uploading saliency map: \(error).") | ||
await updateStatusAsync(to: .failure(.upload), updateUploadStatus: true) | ||
} | ||
func uploadSaliencyMapAsync(compressionQuality: CGFloat = 0.5) async { | ||
guard let jpg = saliencyMap?.jpegData(compressionQuality: compressionQuality) else { | ||
print("Could not convert saliency map into correct format.") | ||
await updateStatusAsync(to: .failure(.upload), updateUploadStatus: true) | ||
return | ||
} | ||
#endif | ||
|
||
let metadata = StorageMetadata() | ||
metadata.contentType = "image/jpeg" | ||
let reference = Storage.storage().reference().child("saliency_map.jpg") | ||
await updateStatusAsync(to: .running(.upload)) | ||
|
||
do { | ||
let response = try await reference.putDataAsync(jpg, metadata: metadata) | ||
print("Upload response metadata: \(response)") | ||
await updateStatusAsync(to: .success(.upload), updateUploadStatus: true) | ||
} catch { | ||
print("Error uploading saliency map: \(error).") | ||
await updateStatusAsync(to: .failure(.upload), updateUploadStatus: true) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The #if swift(>=5.5)
compiler directive was removed from around the async functions. These functions use async/await
features which require Swift 5.5 or later. Removing this guard will cause compilation errors on older Swift versions.
#if compiler(>=5.5) && canImport(_Concurrency)
@MainActor
func updateStatusAsync(to newStatus: ProcessStatus, updateUploadStatus: Bool = false) {
status = newStatus
if updateUploadStatus { uploadSucceeded = newStatus == .success(.upload) }
}
@MainActor func updateImageAsync(to newImage: UIImage?) {
image = newImage
}
@MainActor
func updateSaliencyMapAsync(to newSaliencyMap: UIImage?) {
saliencyMap = newSaliencyMap
}
func downloadImageAsync() async {
await updateStatusAsync(to: .running(.download))
guard let url = URL(string: site) else {
await updateStatusAsync(to: .failure(.download))
print("Failure obtaining URL.")
return
}
do {
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse,
(200 ... 299).contains(httpResponse.statusCode) else {
print("Did not receive acceptable response: \(response)")
await updateStatusAsync(to: .failure(.download))
return
}
guard let mimeType = httpResponse.mimeType, mimeType == "image/png",
let image = UIImage(data: data) else {
print("Could not create image from downloaded data.")
await updateStatusAsync(to: .failure(.download))
return
}
await updateStatusAsync(to: .success(.download))
await updateImageAsync(to: image)
} catch {
print("Error downloading image: \(error).")
await updateStatusAsync(to: .failure(.download))
}
}
func classifyImageAsync() async {
guard let uiImage = image, let ciImage = CIImage(image: uiImage) else {
print("Could not convert image into correct format.")
await updateStatusAsync(to: .failure(.classify))
return
}
let handler = VNImageRequestHandler(ciImage: ciImage, options: [:])
let request = VNClassifyImageRequest()
await updateStatusAsync(to: .running(.classify))
let trace = makeTrace(called: "Classification")
trace?.start()
try? handler.perform([request])
trace?.stop()
guard let observations = request.results else {
print("Failed to obtain classification results.")
await updateStatusAsync(to: .failure(.classify))
return
}
categories = observations
.filter { $0.hasMinimumRecall(0.01, forPrecision: precision) }
.map { ($0.identifier, $0.confidence) }
await updateStatusAsync(to: .success(.classify))
}
func generateSaliencyMapAsync() async {
guard let uiImage = image, let ciImage = CIImage(image: uiImage) else {
print("Could not convert image into correct format.")
await updateStatusAsync(to: .failure(.saliencyMap))
return
}
let handler = VNImageRequestHandler(ciImage: ciImage, options: [:])
let request = VNGenerateAttentionBasedSaliencyImageRequest()
await updateStatusAsync(to: .running(.saliencyMap))
let trace = makeTrace(called: "Saliency_Map")
trace?.start()
try? handler.perform([request])
trace?.stop()
guard let observation = request.results?.first else {
print("Failed to generate saliency map.")
await updateStatusAsync(to: .failure(.saliencyMap))
return
}
let inputImage = CIImage(cvPixelBuffer: observation.pixelBuffer)!
let scale = Double(ciImage.extent.height) / Double(inputImage.extent.height)
let aspectRatio = Double(ciImage.extent.width) / Double(ciImage.extent.height)
guard let scaleFilter = CIFilter(name: "CILanczosScaleTransform") else {
print("Failed to create scaling filter.")
await updateStatusAsync(to: .failure(.saliencyMap))
return
}
scaleFilter.setValue(inputImage, forKey: kCIInputImageKey)
scaleFilter.setValue(scale, forKey: kCIInputScaleKey)
scaleFilter.setValue(aspectRatio, forKey: kCIInputAspectRatioKey)
guard let scaledImage = scaleFilter.outputImage else {
print("Failed to scale saliency map.")
await updateStatusAsync(to: .failure(.saliencyMap))
return
}
let saliencyImage = context.createCGImage(scaledImage, from: scaledImage.extent)
guard let saliencyMap = saliencyImage else {
print("Failed to convert saliency map to correct format.")
await updateStatusAsync(to: .failure(.saliencyMap))
return
}
await updateSaliencyMapAsync(to: UIImage(cgImage: saliencyMap))
await updateStatusAsync(to: .success(.saliencyMap))
}
func uploadSaliencyMapAsync(compressionQuality: CGFloat = 0.5) async {
guard let jpg = saliencyMap?.jpegData(compressionQuality: compressionQuality) else {
print("Could not convert saliency map into correct format.")
await updateStatusAsync(to: .failure(.upload), updateUploadStatus: true)
return
}
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let reference = Storage.storage().reference().child("saliency_map.jpg")
await updateStatusAsync(to: .running(.upload))
do {
let response = try await reference.putDataAsync(jpg, metadata: metadata)
print("Upload response metadata: \(response)")
await updateStatusAsync(to: .success(.upload), updateUploadStatus: true)
} catch {
print("Error uploading saliency map: \(error).")
await updateStatusAsync(to: .failure(.upload), updateUploadStatus: true)
}
}
#endif
func downloadImageAsync() async { | ||
await updateStatusAsync(to: .running(.download)) | ||
|
||
@MainActor @available(iOS 15, tvOS 15, *) | ||
func updateSaliencyMapAsync(to newSaliencyMap: UIImage?) { | ||
saliencyMap = newSaliencyMap | ||
guard let url = URL(string: site) else { | ||
await updateStatusAsync(to: .failure(.download)) | ||
print("Failure obtaining URL.") | ||
return | ||
} | ||
|
||
@available(iOS 15, tvOS 15, *) func downloadImageAsync() async { | ||
await updateStatusAsync(to: .running(.download)) | ||
do { | ||
let (data, response) = try await URLSession.shared.data(from: url) | ||
|
||
guard let url = URL(string: site) else { | ||
guard let httpResponse = response as? HTTPURLResponse, | ||
(200 ... 299).contains(httpResponse.statusCode) else { | ||
print("Did not receive acceptable response: \(response)") | ||
await updateStatusAsync(to: .failure(.download)) | ||
print("Failure obtaining URL.") | ||
return | ||
} | ||
|
||
do { | ||
let (data, response) = try await URLSession.shared.data(from: url) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, | ||
(200 ... 299).contains(httpResponse.statusCode) else { | ||
print("Did not receive acceptable response: \(response)") | ||
await updateStatusAsync(to: .failure(.download)) | ||
return | ||
} | ||
|
||
guard let mimeType = httpResponse.mimeType, mimeType == "image/png", | ||
let image = UIImage(data: data) else { | ||
print("Could not create image from downloaded data.") | ||
await updateStatusAsync(to: .failure(.download)) | ||
return | ||
} | ||
|
||
await updateStatusAsync(to: .success(.download)) | ||
await updateImageAsync(to: image) | ||
|
||
} catch { | ||
print("Error downloading image: \(error).") | ||
guard let mimeType = httpResponse.mimeType, mimeType == "image/png", | ||
let image = UIImage(data: data) else { | ||
print("Could not create image from downloaded data.") | ||
await updateStatusAsync(to: .failure(.download)) | ||
} | ||
} | ||
|
||
@available(iOS 15, tvOS 15, *) func classifyImageAsync() async { | ||
guard let uiImage = image, let ciImage = CIImage(image: uiImage) else { | ||
print("Could not convert image into correct format.") | ||
await updateStatusAsync(to: .failure(.classify)) | ||
return | ||
} | ||
|
||
let handler = VNImageRequestHandler(ciImage: ciImage, options: [:]) | ||
let request = VNClassifyImageRequest() | ||
await updateStatusAsync(to: .running(.classify)) | ||
let trace = makeTrace(called: "Classification") | ||
trace?.start() | ||
try? handler.perform([request]) | ||
trace?.stop() | ||
await updateStatusAsync(to: .success(.download)) | ||
await updateImageAsync(to: image) | ||
|
||
guard let observations = request.results else { | ||
print("Failed to obtain classification results.") | ||
await updateStatusAsync(to: .failure(.classify)) | ||
return | ||
} | ||
} catch { | ||
print("Error downloading image: \(error).") | ||
await updateStatusAsync(to: .failure(.download)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The URLSession.shared.data(from:)
async method requires macOS 12+ and watchOS 8+. The pull request description states that the macOS deployment target is 11.0 and the watchOS deployment target is 7.0. Calling this method on these older OS versions will result in a runtime crash. This function should either be guarded by an @available
attribute or an #available
check inside the function, or the deployment targets should be raised.
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *) func downloadImageAsync() async {
await updateStatusAsync(to: .running(.download))
guard let url = URL(string: site) else {
await updateStatusAsync(to: .failure(.download))
print("Failure obtaining URL.")
return
}
do {
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse,
(200 ... 299).contains(httpResponse.statusCode) else {
print("Did not receive acceptable response: \(response)")
await updateStatusAsync(to: .failure(.download))
return
}
guard let mimeType = httpResponse.mimeType, mimeType == "image/png",
let image = UIImage(data: data) else {
print("Could not create image from downloaded data.")
await updateStatusAsync(to: .failure(.download))
return
}
await updateStatusAsync(to: .success(.download))
await updateImageAsync(to: image)
} catch {
print("Error downloading image: \(error).")
await updateStatusAsync(to: .failure(.download))
}
}
} else { | ||
process.downloadImage() | ||
} | ||
Task { await process.downloadImageAsync() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The downloadImageAsync()
function uses URLSession.shared.data(from:)
which requires macOS 12+ and watchOS 8+. The pull request description states that the macOS deployment target is 11.0 and the watchOS deployment target is 7.0. Calling this function on these older OS versions will result in a runtime crash. The #available
check should be restored here to prevent calling the async version on unsupported platforms/versions.
if #available(iOS 15, tvOS 15, macOS 12, watchOS 8, *) {
#if compiler(>=5.5) && canImport(_Concurrency)
Task { await process.downloadImageAsync() }
#else
process.downloadImage()
#endif
} else {
process.downloadImage()
}
Removed #if compiler(>=X.X) directives by keeping the code within the #if block and removing the #else and #endif blocks. This assumes the codebase is compiled with a modern Swift compiler where these conditional blocks are no longer necessary.
Analyzed project deployment targets and removed unnecessary @available, #available, and #if swift guards throughout the codebase.
Removed #if swift(>=X.X) blocks by migrating the code for the newer Swift version into the enclosing scope and deleting the #else blocks.