Skip to content

Comparing parameterized protocol type with nil requires runtime support #90511

Description

@Eskils

Description

Comparing an optional instance of a parameterized protocol type with nil, requires runtime support only available in recent versions of the standard library. This seems to be an incorrect assumption from the Swift compiler as guard let is allowed, as well as a switch statement.

Further investigation suggests the Swift compiler assumes this not to be allowed for all generic enums where the bound type is a parameterized protocol type.

Reproduction

protocol SomeTrait<T> {
    associatedtype T
}

struct SomeConstruct {
    var box: (any SomeTrait<String>)?
    
    func doSomething() {
        guard let _ = box else {
            return
        }
        
        guard box == nil else { // Error: Runtime support for parameterized protocol types is only available in macOS 13.0.0 or newer
            return
        }
        
        switch box {
        case .none:
            break
        case .some:
            break
        }
    }
}

Expected behavior

As guard let is allowed, and also a switch statement, I expect comparing with nil to also be allowed.

Environment

swift-driver version: 1.167 Apple Swift version 6.4 (swiftlang-6.4.0.20.104 clang-2100.3.20.102)
Target: arm64-apple-macosx26.0

Also occurs on:

swift-driver version: 1.127.14.1 Apple Swift version 6.2.1 (swiftlang-6.2.1.4.8 clang-1700.4.4.1)
Target: arm64-apple-macosx26.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage neededThis issue needs more specific labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions