pointfreeco / swift-custom-dump

A collection of tools for debugging, diffing, and testing your application's data structures.
MIT License
800 stars 89 forks source link

Crash occurs when diffing two `LocalizedStringKeys` with special interpolations #97

Open daltonclaybrook opened 1 year ago

daltonclaybrook commented 1 year ago

Describe the bug Passing a LocalizedStringKey constructed with basic string interpolation works correctly. For example, the following code works as you would expect:

let name = "Dalton"
let control: LocalizedStringKey = "Static string"
let success: LocalizedStringKey = "Hello, \(name)!"

// This prints:
// - "Static string"
// + "Hello, Dalton!"
print(diff(control, success) ?? "No diff")

However, passing a LocalizedStringKey that uses any of the other variations of string interpolation supported by LocalizedStringKey results in a crash:

let control: LocalizedStringKey = "Static string"
let crash1: LocalizedStringKey = "Date: \(Date(), style: .date)"
let crash2: LocalizedStringKey = "Date range: \(Date.distantPast...Date.distantFuture)"
let crash3: LocalizedStringKey = "Time: \(Duration.seconds(10), format: .time(pattern: .minuteSecond))"

print(diff(control, crash1) ?? "No diff") // crash!
print(diff(control, crash2) ?? "No diff") // crash!
print(diff(control, crash3) ?? "No diff") // crash!

The crash (SIGABRT) occurs on this line: https://github.com/pointfreeco/swift-custom-dump/blob/c2012e1eadd30b232186a9045da0fc5635969566/Sources/CustomDump/Conformances/SwiftUI.swift#L59

The console output:

Could not cast value of type 'SwiftUI.LocalizedStringKey.FormatArgument.Token' (0x1fea477e8) to 'NSFormatter' (0x1fd708ed8).
2023-09-02 19:11:54.584602-0500 dump-bug[32884:11397570] Could not cast value of type 'SwiftUI.LocalizedStringKey.FormatArgument.Token' (0x1fea477e8) to 'NSFormatter' (0x1fd708ed8).

Expected behavior I expect that the above code would not result in a crash and would instead print an appropriate diff between the provided strings.

Environment