Protocol inheritance does not trigger @available compiler errors

The @available on Identifiable does not get tripped at compile time when it is inherited. This feels like a bug (maybe even is a known bug).

I have tried to minimize a reproducible case. This seems pretty close.

import XCTest

protocol ProtocolIdentifiable: Identifiable {}

func use<I: ProtocolIdentifiable>(_ item: I) {
  print(item.id)
}

struct Conformance: ProtocolIdentifiable {
  let id = UUID()
}

final class ProtocolIdentifiableTests: XCTestCase {
    func testExample() {
      use(Conformance())
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

Indeed, I can build and run the above code on iOS 12 albeit with an (expected) runtime crash.

#!/usr/bin/env zsh

set -e
set -x

XCODE_SCHEME="ProtocolIdentifiable"
XCODE_RESULT="ProtocolIdentifiable.xcresult"
DESTINATION="platform=iOS Simulator,name=iPad Pro (9.7-inch),OS=12.4"
rm -rf "$PWD/derivedData" "$PWD/.build" "$PWD/.swiftpm" "$PWD/$XCODE_RESULT"
xcodebuild -derivedDataPath "$PWD/derivedData" -scheme "$XCODE_SCHEME" -destination "$DESTINATION" build-for-testing
xcodebuild -derivedDataPath "$PWD/derivedData" -resultBundlePath "$XCODE_RESULT" -destination "$DESTINATION" -xctestrun derivedData/Build/Products/*.xctestrun test-without-building

If I however, change the code to:

import XCTest

func use<I: Identifiable>(_ item: I) {
  print(item.id)
}

struct Conformance: Identifiable {
  let id = UUID()
}

final class ProtocolIdentifiableTests: XCTestCase {
    func testExample() {
      use(Conformance())
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

Then build as above it stops during compilation with an error (as expected):

ProtocolIdentifiableTests.swift:3:13: error: 'Identifiable' is only available in iOS 13 or newer
func use<I: Identifiable>(_ item: I) {

Is this a bug or is this working as designed?

1 Like

This was a bug. I fixed it recently as part of the conformance availability work, but technically it was a separate issue. See Fixing two holes in declaration availability checking for details.

1 Like
Terms of Service

Privacy Policy

Cookie Policy