Skip to content

Compiler ignores availability in synthesized Equatable conformance #66270

Open
@sjavora

Description

@sjavora

Description

Consider the following struct:

struct Foo<T> {
    let value: T
}

@available(iOS 13, *)
extension Foo: Equatable where T: Equatable {}

Now consider another struct that uses it and is itself unconditionally Equatable:

struct UsingFoo: Equatable {
    let foo: Foo<Int>
}

This compiles. Yet when I write out the conformance by hand, I get a (correct) error:

struct UsingFoo: Equatable {

    let foo: Foo<Int>

    static func == (lhs: Self, rhs: Self) -> Bool {
        lhs.foo == rhs.foo // error: '__derived_struct_equals' is only available in iOS 13 or newer
    }
}

We found this issue through a crash on iOS 14 with this SwiftUI type:

import SwiftUI

public struct IDPreference: Equatable {
    let id: AnyHashable
    let bounds: Anchor<CGRect>
}

Using this on iOS 14 (Anchor's Equatable conformance only exists from iOS 15) causes a crash:

dyld: lazy symbol binding failed: Symbol not found: _$s7SwiftUI6AnchorVAASQRzlE2eeoiySbACyxG_AEtFZ
  Referenced from: /Users/simonjavora/Library/Developer/CoreSimulator/Devices/7ABFF77E-E6F9-49D9-8B21-719FCCAFC3F9/data/Containers/Bundle/Application/7A61D167-84A5-446D-B404-780918F34531/Kiwi.com.app/Kiwi.com
  Expected in: /System/Library/Frameworks/SwiftUI.framework/SwiftUI

dyld: Symbol not found: _$s7SwiftUI6AnchorVAASQRzlE2eeoiySbACyxG_AEtFZ
  Referenced from: /Users/simonjavora/Library/Developer/CoreSimulator/Devices/7ABFF77E-E6F9-49D9-8B21-719FCCAFC3F9/data/Containers/Bundle/Application/7A61D167-84A5-446D-B404-780918F34531/Kiwi.com.app/Kiwi.com
  Expected in: /System/Library/Frameworks/SwiftUI.framework/SwiftUI
Screenshot 2023-06-01 at 14 34 12

Expected behavior

The compiler should output a diagnostic.

Environment

  • Swift compiler version info: swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100), Target: arm64-apple-macosx13.0
  • Xcode version info: Xcode 14.3.1, Build version 14E300b
  • Deployment target: iOS 16

Metadata

Metadata

Assignees

No one assigned

    Labels

    availabilityThe @available attribute and availability checking in generalbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesderived conformancesFeature → protocol → conformances: derived conformances aka synthesized conformancesrun-time crashBug → crash: Swift code crashed during executiontype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions