Skip to content

Conversation

@DougGregor
Copy link
Member

Test PR to try out implicitly opening existentials by default

When calling a generic function with an argument of existential type,
implicitly "open" the existential type into a concrete archetype, which
can then be bound to the generic type. This extends the implicit
opening that is performed when accessing a member of an existential
type from the "self" parameter to all parameters. For example:

    func unsafeFirst<C: Collection>(_ c: C) -> C.Element { c.first! }

    func g(c: any Collection) {
      unsafeFirst(c)   // currently an error
                       // with this change, succeeds and produces an 'Any'
    }

This avoids many common sources of errors of the form

    protocol 'P' as a type cannot conform to the protocol itself

which come from calling generic functions with an existential, and
allows another way "out" if one has an existention and needs to treat
it generically.

This feature is behind a frontend flag
`-enable-experimental-opened-existential-types`.
An opaque type is only invariant with respect to the existential Self
when the constraints on the opaque type involve Self. Such constraints
are not expressible in the type-erased value, so treat them as
invariant. This loosens the restriction on using members of protocol
type that return an opaque type, such that (e.g.) the following is
still well-formed:

    protocol P { }
    protocol Q { }

    extension P {
      func getQ() -> some Q { ... }
    }

    func test(p: any P) {
      let q = p.getQ() // formerly an error, now returns an "any Q"
    }

However, this does not permit uses of members such as:

    extension P {
      func getCollection() -> some Collection<Self> { ... } // error
    }

because the type system cannot express the corresponding existential
type `any Collection<Self>`.
Ensure that we only open existentials in an argument when the corresponding
parameter's type is a generic parameter that is only used in covariant
positions, because either invariant or contravariant uses mean that we
won't be able to type-erase other uses of that parameter. This mirrors
the requirement placed when opening existentials as a member of protocol
type.
C++ function templates require specialization, which does not work with
opened existentials. Disable opening for them.
…entials

Implicitly disable implicit opening of existentials for SwiftOnoneSupport
because it generates different specializations.
As with many other places in the frontend, grab the constraint type
when from an existential type when we have one.
@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor DougGregor force-pushed the implicitly-open-existentials-default branch from 9fa30f5 to 2641f2d Compare February 11, 2022 01:57
@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor DougGregor closed this Mar 25, 2022
@DougGregor DougGregor deleted the implicitly-open-existentials-default branch March 25, 2022 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant