Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any does not support combined protocols with primary associated type without a typealias #63877

Open
rnapier opened this issue Feb 24, 2023 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values parameterized protocols Feature → protocol: protocols with primary associated types protocol compositions Feature → types: protocol composition types swift 5.9 type checker Area → compiler: Semantic analysis TypeResolver unexpected error Bug: Unexpected error

Comments

@rnapier
Copy link

rnapier commented Feb 24, 2023

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

@rnapier rnapier added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 24, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Feb 24, 2023

More self-contained:

protocol P<A> {
  associatedtype A
}
protocol Q {}

do {
  let _: any Q & P<Int>
}

Looks like type resolution is not expecting parameterized protocol types in a composition when the composition is an existential constraint.

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis TypeResolver parameterized protocols Feature → protocol: protocols with primary associated types protocol compositions Feature → types: protocol composition types swift 5.9 unexpected error Bug: Unexpected error existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values and removed triage needed This issue needs more specific labels labels Feb 24, 2023
@slavapestov
Copy link
Member

The restriction here is artificial, we should just remove it.

@slavapestov slavapestov self-assigned this Mar 7, 2023
@vmanot
Copy link

vmanot commented Dec 14, 2023

Curious about the status of this issue, just ran into it again today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values parameterized protocols Feature → protocol: protocols with primary associated types protocol compositions Feature → types: protocol composition types swift 5.9 type checker Area → compiler: Semantic analysis TypeResolver unexpected error Bug: Unexpected error
Projects
None yet
Development

No branches or pull requests

4 participants