Skip to content

Regression creating/comparing JSON encoded data #66466

Description

@NachoSoto

Description
Running my test suite, I realized a bunch of tests doing Data comparisons started failing with Xcode 15. I managed to find an example. It's possible that something simpler would also reproduce, but this is the simplest I was able to get it.

Steps to reproduce

import Foundation
import XCTest

struct Model {
    struct SubModel: Encodable {
        var data: [Int]

        init(count: Int) {
            self.data = []
            self.data.reserveCapacity(count)

            for _ in 0..<count {
                self.data.append(Int.random(in: 0..<99999999))
            }
        }
    }

    internal enum Enum: Int, Encodable {
        case notRequested = 0
        case verified = 1
        case failed = 2

    }

    let submodel: SubModel
    let count: Int
    let result: Enum = .verified

    init(count: Int) {
        self.submodel = .init(count: count)
        self.count = count
    }
}

extension Model: Encodable {
    private enum CodingKeys: String, CodingKey {
        case submodel
        case count
        case result
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try self.submodel.encode(to: encoder)
        try container.encode(self.count, forKey: .count)
        try container.encode(self.result, forKey: .result)
    }
}

let jsonEncoder = JSONEncoder()

extension Encodable {
    var asData: Data {
        get throws {
            return try jsonEncoder.encode(self)
        }
    }
}

private let model = Model(count: 1000000)

final class JSONRegressionTests: XCTestCase {

    func testComparison() throws {
        try XCTAssertEqual(model.asData, model.asData)
    }
}

Expected behavior
Test passes, like it does on Xcode 14.x / iOS 16.

Actual behavior
Screenshot 2023-06-08 at 13 29 55

Environment

  • Swift compiler version info:
swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
Target: arm64-apple-macosx13.0
  • Xcode version info:
Xcode 15.0
Build version 15A5160n
  • Deployment target: iOS 17.0

Additional context
Weirdly sometimes after a clean build, the test passes, but subsequent runs fail, maybe pointing to some sort of miscompilation?

I can't really tell what the problem is. As you can see on the error though, the actual Data has the same hash and length (this is the error when comparing the values using Nimble instead of XCTest which provides the hash as well):

expected to equal <Data<hash=17221091,length=1364>>, got <Data<hash=17221091,length=1364>>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions