Conversation
Summary of ChangesHello @tayloraswift, 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 introduces a substantial overhaul of how JSON numbers are represented, parsed, and encoded within the library. The primary goal is to improve the accuracy and robustness of numeric handling, particularly for floating-point values and non-finite numbers, by adopting a more flexible and efficient internal representation. This refactoring also aligns the project with the latest Swift 6.2 toolchain and enhances the overall testing coverage for numeric types. Highlights
Using Gemini Code AssistThe 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 by creating a comment using either
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 Limitations & Feedback Gemini Code Assist 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant refactoring of how numeric values are handled to ensure lossless round-tripping of floating-point numbers, which resolves issues #83 and #91. The JSON.Number type is changed from a struct to an enum to support non-finite numbers and values that don't fit into a 64-bit representation, using a string fallback. The parsing and encoding logic is updated accordingly, with new tests to verify the round-tripping behavior.
My review has identified a critical issue in the number parsing logic where an integer overflow can occur, potentially crashing the application. I've also suggested a simplification for the Swift settings configuration in Package.swift to improve readability and maintainability.
| case .minus: | ||
| // note: potential crash if `exponent.magnitude` is absurdly large | ||
| places += exponent.magnitude |
There was a problem hiding this comment.
As noted in the comment, places += exponent.magnitude can cause an integer overflow, which would lead to a crash in debug builds or incorrect parsing in release builds. You should use addingReportingOverflow to safely perform the addition and fall back to the string representation if an overflow occurs.
| case .minus: | |
| // note: potential crash if `exponent.magnitude` is absurdly large | |
| places += exponent.magnitude | |
| case .minus: | |
| let (sum, overflow) = places.addingReportingOverflow(exponent.magnitude) | |
| if overflow { break representable } | |
| places = sum |
| package.targets = package.targets.map { | ||
| switch $0.type { | ||
| case .plugin: return $0 | ||
| case .binary: return $0 | ||
| default: break | ||
| } | ||
| { | ||
| var settings: [SwiftSetting] = $0 ?? [] | ||
|
|
||
| settings.append(.enableUpcomingFeature("ExistentialAny")) | ||
| settings.append(.treatWarning("ExistentialAny", as: .error)) | ||
| settings.append(.treatWarning("MutableGlobalVariable", as: .error)) | ||
|
|
||
| $0 = settings | ||
| } (&$0.swiftSettings) | ||
| return $0 | ||
| } |
There was a problem hiding this comment.
This block of code for adding Swift settings is overly complex and uses an obscure pattern with an immediately-invoked closure and an inout parameter. This makes the code hard to read and maintain. A more straightforward approach using a guard statement and direct property assignment would be much clearer.
package.targets = package.targets.map {
guard $0.type != .plugin, $0.type != .binary else {
return $0
}
$0.swiftSettings = ($0.swiftSettings ?? []) + [
.enableUpcomingFeature("ExistentialAny"),
.treatWarning("ExistentialAny", as: .error),
.treatWarning("MutableGlobalVariable", as: .error),
]
return $0
}
fix #83
fix #91