Commit Graph

39 Commits

Author SHA1 Message Date
Pavel Yaskevich
0d33af78ca [CSOptimizer] Add support for opened existential arguments
Check whether it would be possible to match a parameter type
by opening an existential type of the candidate argument.

Resolves: rdar://158159462
2025-08-15 00:14:51 -07:00
Slava Pestov
373d08e572 Add regression test for rdar://148357926
I believe this was fixed by https://github.com/swiftlang/swift/pull/81181.
2025-05-13 11:16:41 -04:00
Anthony Latsis
07a6612e5f test: Add regression test for rdar://91922018 2025-02-09 05:16:45 +00:00
Anthony Latsis
75953d2b51 OpenedExistentials: Do not attempt to erase existential metatypes with invariant Self
The non-metatype case was never supported. The same should hold for the
existential metatype case, which used to miscompile and now crashes
because the invariant reference is deemed OK but the erasure expectedly
fails to handle it:

```swift
class C<T> {}
protocol P {
  associatedtype A

  func f() -> any P & C<A>
  func fMeta() -> any (P & C<A>).Type
}

do {
  let p: any P
  let _ = p.f() // error
  let _ = p.fMeta() // crash
}
```
2025-01-12 17:47:52 +00:00
Anthony Latsis
56d943a77e [NFC] Sema: Tidy up canOpenExistentialCallArgument 2024-12-25 02:25:28 +00:00
Anthony Latsis
08ccfadfa9 Sema: Handle the edge case of in-out metatype parameter in canOpenExistentialCallArgument 2024-12-20 15:49:35 +00:00
Anthony Latsis
9bad13300d test: Actually check if we opened an existential call argument 2024-12-20 15:49:35 +00:00
Slava Pestov
851a829063 Sema: Consolidate logic for opening existentials in OpenedExistentials.cpp 2024-09-04 14:57:38 -04:00
Slava Pestov
1b1963e904 Sema: Remove 'inferred result type requires explicit coercion' diagnostic 2023-11-27 14:05:36 -05:00
Angela Laar
a92deb070d [GenericSignature] Exclude GenericParam Constraints
Only preserve primary associated types during type erasure if the
generic context does not contain outer generic prameters.

i.e.

  Given `func foo {  ... any P <Int> ... }` getNonDependentUpperBounds()
  should produce any P<Int>

  However, given `func foo<T> { ... any P<T> ... }` getNonDependentUpperBounds()
  should produce any P

Fixes rdar://110262754
2023-06-22 14:15:41 -07:00
Angela Laar
a9f1096839 [Generic Signature] Unify generic upper bound functions
Opened existentials should be erased to the dependent upper bound
if the dependent member can be reduced to a concrete type. This
allows the generic signature to support parameterized protocol types
and bound generic class types by producing a more specific constraint
instead of just a plain protocol or class.
2023-05-17 15:33:50 -07:00
Angela Laar
5c818b3fd4 [Constraint System] Fix covariant erasure for constrained existentials
Constrained existentials should be type erased to an upper bound that is dependent on other type parameters.
2023-05-15 16:17:59 -07:00
Doug Gregor
761b0aa98c Allow existential opening for parameters of optional type. 2022-09-27 13:08:36 -07:00
John Holdsworth
d72b9a4d81 Cater for opening var existential followup. 2022-07-15 12:29:41 -07:00
Holly Borla
6e8a581b42 [LangOptions] Remove the option to enable/disable implicit existential
opening.
2022-06-27 18:03:20 -07:00
Pavel Yaskevich
8eba8907a2 [CSSimplify] Don't suppress existential opening if as any ... is in parens 2022-05-26 15:34:40 -07:00
Pavel Yaskevich
6edab6a9a6 [Diagnostics] Prevent fix-it for explicit existential erasure from suppressing opening at use site
If erased result is passed as an argument to a call that requires
implicit opening, the fix-it should use parens to avoid suppressing
the opening at that argument position.
2022-05-26 15:34:07 -07:00
Pavel Yaskevich
f2f7082e7e [Diagnostics] Attach note about erased result type to the call instead of declaration
Since the inference is a call site specific it makes sense to attach
a candidate note to the call itself instead of the declaration used.
2022-05-25 16:07:06 -07:00
Pavel Yaskevich
773ea6b10c [TypeChecker] Require a coercion if result of protocol member access would loose information
Accessing members on the protocol could result in existential opening and subsequence
result erasure, which requires explicit coercion if there is any loss of generic requirements.
2022-05-25 16:07:06 -07:00
Pavel Yaskevich
73b16ba4ea [CSFix] Refine coercion check for dependent member chains
For types like `<Base>.B.C` we need to check that neither
`B` nor `C` have any additional requirements because they
cannot be expressed in the existential type.
2022-05-25 16:07:06 -07:00
Pavel Yaskevich
f08eec4a17 [CSSimplify] Start checking whether result type of a call needs a coercion
If there is going to be any loss of generic requirements in the result,
a result of a call has to use explicit coercion.
2022-05-25 16:07:05 -07:00
Josh Soref
8b89b4c995 Spelling constraints (#42548)
* spelling: availability

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conditional

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: deferred

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: disambiguate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: failed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interferes

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: magnify

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: occurrences

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overload

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: redeclarations

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: simplification

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: string

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-22 09:43:26 -07:00
Slava Pestov
1bf248e374 Enable parameterized protocols by default, and add new staging flag for parameterized existentials 2022-04-06 17:30:10 -04:00
Doug Gregor
50b113e222 Don't open an existential call argument if its type information is needed earlier.
To ensure that we do not accept code that would require an existential call argument
to be evaluated prior to the call, don't open existential call
arguments if the generic parameter that would capture the opened
existential type is used in any prior parameter.

Note that code generation currently moves the opening of the
existential call argument outside of the call, which is a compiler bug.
This change ensures that we don't accept code would *require* this
out-of-order evaluation and adds a test so we know when we fix this.
2022-03-23 16:34:32 -07:00
Doug Gregor
50451d2583 Type-erase contravariant uses of opened existentials in subsequent parameters.
When we open an existential argument in a call to a generic function,
type-erase contravariant uses of that opened existential in subsequent
parameters. This primarily impacts closure parameters, where we want
the closure to be provided with an existential parameter type rather
than permit the parameter to have opened existential type. This
prevents the opened existential type from being directly exposed in
the type system.

Note that we do not need to perform this erasure when the argument is
a reference to a generic function, because there it is suitable to
infer that the generic arguments are the opened archetypes. This
subsumes the use case for `_openExistential`.
2022-03-23 15:22:06 -07:00
Holly Borla
12459cff80 [Diagnostics] Print 'any' in diagnostic arguments. 2022-03-05 14:26:45 -08:00
Slava Pestov
7dfc2a20fb Parse: New syntax for primary associated types 2022-03-03 00:08:00 -05:00
Anthony Latsis
d36aecca49 Diag: Make 'could_not_use_member_on_existential' less assertive 2022-02-23 10:24:51 +03:00
Doug Gregor
4141e20ad6 Revert "Allow existential opening for parameters of optional type."
This reverts commit 6e7fff7e65283ae9b25116fa6b75ba92fd2f2a58. The
asymmetry between opening for optional parameters and optional
arguments is surprising enough that we're currently opting not to
allow them.
2022-02-18 11:22:57 -08:00
Doug Gregor
e1f68b26c5 Allow existential opening for parameters of optional type. 2022-02-18 11:22:57 -08:00
Doug Gregor
e30c8a5c09 Allow opening existentials for inout parameters. 2022-02-18 11:22:57 -08:00
Doug Gregor
17ce779dda Cannot open existentials passed to variadic parameters. 2022-02-18 11:22:57 -08:00
Doug Gregor
76d1ba75f8 Only open existentials for generic arguments on the called declaration 2022-02-18 11:22:57 -08:00
Doug Gregor
1273fa4185 Support opening of arguments of existential metatype type. 2022-02-18 11:22:57 -08:00
Doug Gregor
51abd3d56f Prevent opening existentials when the corresponding parameter is variant
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.
2022-02-18 11:22:57 -08:00
Doug Gregor
6c03882cc4 Fix test availability for opaque result types 2022-02-18 11:22:56 -08:00
Doug Gregor
fa6c3c15d8 Opaque types are only invariant when they involve same-type constraints.
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>`.
2022-02-18 11:22:56 -08:00
Doug Gregor
4ec6dc1e4a Erase opaque types involving opened existential types to their upper bound. 2022-02-18 11:22:56 -08:00
Doug Gregor
1e1b3427c3 Experimental support for implicitly opening existential arguments.
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`.
2022-02-18 11:22:56 -08:00