Skip to content

Crash with existential any in function context #70988

@GeorgeElsham

Description

@GeorgeElsham

Description

I'm unsure what causes this bug, and I would expect it to correctly compile as valid code.

Reproduction

protocol A<T> {
    associatedtype T
}
protocol B {}
typealias C<T> = A<T> & B

struct Foo<In, Out> {
    init(fn: (any C<In>) -> Out) {
        //
    }
}

let f = Foo<Int, Void> { _ in }

Stack dump

error: compile command failed due to signal 4 (use -v to see invocation)
error: fatalError

Expected behavior

The given code should compile.

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

Workaround 1

Replace Out in function with Void. Optionally remove the Out generic on Foo. Breaks the original intentions.

protocol A<T> {
    associatedtype T
}
protocol B {}
typealias C<T> = A<T> & B

struct Foo<In, Out> {
    init(fn: (any C<In>) -> Void) { // <- here
        //
    }
}

let f = Foo<Int, Void> { _ in }

Workaround 2

Remove protocol composition. Breaks the original intentions.

protocol A<T> {
    associatedtype T
}
typealias C<T> = A<T>

struct Foo<In, Out> {
    init(fn: (any C<In>) -> Out) {
        //
    }
}

let f = Foo<Int, Void> { _ in }

Workaround 3

Remove the use of any existential. Breaks the original intentions.

protocol A<T> {
    associatedtype T
}
protocol B {}
typealias C<T> = A<T> & B

struct Foo<In, Out> {
    init<D: C<In>>(fn: (D) -> Out) {
        //
    }
}

struct Bar {}
extension Bar: A {
    typealias T = Int
}
extension Bar: B {}

let f = Foo<Int, Void> { (_: Bar) in }

Workaround 4

Move the function to a new type. Looks like the init may be the issue? Although inconvenient, it does not break the original intentions.

protocol A<T> {
    associatedtype T
}
protocol B {}
typealias C<T> = A<T> & B

struct Bar<In, Out> {
    let fn: (any C<In>) -> Out
}

struct Foo<In, Out> {
    init(fn: Bar<In, Out>) {
        //
    }
}

let f = Foo<Int, Void>(fn: Bar { _ in })

Metadata

Metadata

Assignees

No one assigned

    Labels

    SILGenArea → compiler: The SIL generation stagebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareexistentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesfunction typesFeature → types: function typesparameterized protocolsFeature → protocol: protocols with primary associated typesprotocol compositionsFeature → types: protocol composition typesswift 6.0typesFeature: types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions