Skip to content

Crash occurs when diffing two LocalizedStringKeys with special interpolations #97

@daltonclaybrook

Description

@daltonclaybrook

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:

Array(Mirror(reflecting: children[0].value).children)[0].value as! (Any, Formatter?)

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

  • swift-custom-dump version: 1.0.0
  • Xcode Version 14.3.1 (14E300c)
  • Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
  • OS: macOS 13.4.1 (c) (22F770820d) and iOS 16.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions