Skip to content

importing Foundation allows casting non-Equatable Errors to Equatable; comparing instances yields false positive results #85111

@lukaskollmer

Description

@lukaskollmer

Description

if a program imports Foundation, for some reason, all Error enums get a runtime conformance to Equatable (which verifiably does not exist at compile-time), allowing instances of the enum to be compared against each other.

however: these comparisons are incorrect; they seem to only look at the enum case, but completely ignore any associated values, leading to completely different values comparing as equal, even though these values would not compare equal when compared outside the context of th Error enum.

Reproduction

import Foundation

final class C: Sendable {} // non-equatable type

enum TestError: Error {
    case a, b(Int), c(C)
}

let c1 = C()
let c2 = C()

let err1 = TestError.c(c1)
let err1Eq = err1 as any Equatable
precondition(err1Eq.isEqual(TestError.c(c1)))
precondition(err1Eq.isEqual(TestError.c(c2))) // will pass, but shouldn't

let err2 = TestError.b(0)
let err2Eq = err2 as any Equatable
precondition(err2Eq.isEqual(TestError.b(0)))
precondition(err2Eq.isEqual(TestError.b(1))) // will pass, but shouldn't


// util
extension Equatable {
    func isEqual(_ other: Any) -> Bool {
        if let other = other as? Self {
            self == other
        } else {
            false
        }
    }
}

Expected behavior

either:

  • the presence/absence of Foundation should not impact a type's runtime protocol conformances, and Error enums would not be Equatable if Foundation is imported (and instead the default rules would apply, i.e. the enum would (not) be Equatable based on its member types), or
  • this runtime conformance should properly compare the enum's associated values, and behave the same way a default synthesized conformance would.

Environment

swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx15.0

Additional information

No response

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions