Skip to content

Enable constrained existential types in protocol compositions #63877

Closed
@rnapier

Description

@rnapier

Description

If a protocol with a primary associated type (such as RangeExpression) is combined using & with another protocol (such as Sendable), it cannot directly be wrapped with any. It can, however, be wrapped with an intermediate typealias.

Steps to reproduce

struct Period: Sendable {
    // Fails:
    // Non-protocol, non-class type 'RangeExpression<Int>' cannot be used within a protocol-constrained type
    var monthsBad: any RangeExpression<Int> & Sendable

    // Succeeds:
    typealias IntRangeSendable = RangeExpression<Int> & Sendable
    var monthsGood: any IntRangeSendable
}

Expected behavior

Both of these should be identical and compile. However, only the second is allowed. The following similar cases are also all allowed:

any CustomStringConvertible & Sendable // non-associated type
any Equatable & Sendable // associated type, but not primary
any RangeExpression & Sendable // primary associated type, but not specified

Environment
swift-driver version: 1.75.1 Apple Swift version 5.8 (swiftlang-5.8.0.117.11 clang-1403.0.22.8.60)
Target: arm64-apple-macosx13.0

Xcode 14.3
Build version 14E5197f

Target: macOS 13.2.1

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconstrained existentialsFeature → existentials: constrained existentials such as 'any Collection<Int>'existentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesgenericsFeature: generic declarations and typesprotocol compositionsFeature → types: protocol composition typesswift 5.9type checkerArea → compiler: Semantic analysistypesFeature: typesunexpected errorBug: Unexpected error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions