Commit Graph

2010 Commits

Author SHA1 Message Date
Pavel Yaskevich
8cfdb9999c Merge pull request #41436 from xedin/allow-specialization-from-default-expr
[TypeChecker] Allow inference from default expressions in certain scenarios (under a flag)
2022-02-24 08:57:42 -08:00
Pavel Yaskevich
bf820c5dab [CSDiagnostics] Look through l-value conversions when mismatch is in subscript setter
`repairFailures` needs a special case when l-value conversion is
associated with a result type of subscript setter because otherwise
it falls through and treats result type as if it's an argument type,
which leads to crashes.

Resolves: rdar://84580119
2022-02-21 16:32:25 -08:00
Pavel Yaskevich
82b955b93c [CSDiagnostics] Detect and diagnose contextual mismatches with default value 2022-02-21 09:59:54 -08:00
Pavel Yaskevich
aa286b7bf4 [CSSimplify] Check whether parameter refers to generic parameters before attempting inference from default 2022-02-21 09:59:54 -08:00
Pavel Yaskevich
aea76ca358 [TypeChecker] Extend type inference from default expressions to handle nested generic parameters
Adds support for parameter types like `[T?]` or `[(T, U?)]`,
and relaxes restriction on same-type generic parameters.

A same-type requirement is acceptable if it only includes
in-scope generic parameters and concrete types i.e. `T.X == Int`
if accepted if `T` is referenced only by a parameter default
expression is being applied to.
2022-02-21 09:59:54 -08:00
Pavel Yaskevich
a6e84b3d05 [ConstraintSystem] Add call site support for type inference from default expressions 2022-02-21 09:59:53 -08: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
76d1ba75f8 Only open existentials for generic arguments on the called declaration 2022-02-18 11:22:57 -08:00
Doug Gregor
9a7b9c6216 Don't open existentials when calling C++ function templates
C++ function templates require specialization, which does not work with
opened existentials. Disable opening for them.
2022-02-18 11:22:57 -08:00
Doug Gregor
33be0beb12 Ensure that we only open existential arguments to functions/subscripts. 2022-02-18 11:22:57 -08:00
Doug Gregor
deab0b9bd7 Don't open existential arguments to type(of:). 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
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
Becca Royal-Gordon
e04ec5a7d9 Weaken some type checks for @preconcurrency decls
In an async context, trying to pass a non-`@Sendable` function to an `@Sendable` parameter or trying to assign a `@MainActor` method to a non-`@MainActor`-typed variable were hard errors. We now think that this a mistake for `@preconcurrency` APIs in Swift 5 mode, as it hinders retroactive adoption of `@Sendable` and `@MainActor` by libraries.

This PR weakens these errors to warnings *only* when the decl which contains the attribute in its type signature is `@preconcurrency` and *only* when in Swift 5 mode (with or without -warn-concurrency). For non-`@preconcurrency` decls, it is still an error.

Fixes <rdar://88703266>.
2022-02-15 13:35:55 -08:00
Hamish Knight
04b37ae1d0 [CS] Don't apply compatibility logic in Swift 6 mode
This was only intended to preserve compatibility
for cases where the constraint system may have
previously accepted invalid code. Drop it in Swift 6
mode such that invalid collection coercions become
errors.
2022-02-10 19:06:49 +00:00
Hamish Knight
63c094c28b [CS] Don't apply compatibility logic to collection literals
We currently permit (but warn on) coercions of
collections with unrelated element types in
certain cases. This is done in an effort to preserve
compatibility with pre-5.3 compilers that may have
allowed such code to compile due to dropping
constraints while solving.

This is limited to collection coercions as we
emit somewhat reasonable code for them that
performs a force cast of the elements at runtime.
However, it turns out that in the case of collection
literals, we peephole the element conversions in
CSApply, leading to codegen crashes. As such,
narrow down the condition of this compatibility
logic to not apply to collection literals, as this
never would have compiled properly.

rdar://88334481
2022-02-10 19:06:48 +00:00
Pavel Yaskevich
f1e602f98d Merge pull request #41189 from xedin/trailing-closures-with-callAsFunction
[ConstraintSystem] Match trailing closures to implicit `.callAsFunction` when necessary
2022-02-08 17:44:55 -08:00
Pavel Yaskevich
dee174acca [CSSimplify] Simplify new argument/trailing closure splitting code for .callAsFunction injection 2022-02-04 11:20:32 -08:00
Pavel Yaskevich
49b181136b [AST] Augment ArgumentList::createImplicit to accept first trailing closure index 2022-02-03 19:41:00 -08:00
Pavel Yaskevich
1c258ca05f [CSSimplify] Allow .callAsFunction injection after constructor in diagnostic mode
If argument matching fails in diagnostic mode, it can only mean
that it couldn't find any viable fixes to attempt, so let's try
injecting `.callAsFunction` when appropriate to help diagnose
possible mismatches related to ambiguous syntax.
2022-02-03 15:36:19 -08:00
Pavel Yaskevich
01a4468b32 [ConstraintSystem] Extract creation/recording of implicit .callAsFunction roots 2022-02-03 15:36:18 -08:00
Pavel Yaskevich
9e2137d66f [CSSimplify] Record implicit .callAsFunction root expression
Makes it possible to find the root during solution application.
2022-02-03 15:35:57 -08:00
Pavel Yaskevich
c20621643b [CSSimplify] Attempt to split trailing closures into an implicit .callAsFunction
In situations like `T(...) { ... }` where `T` is a callable type,
it's not immediately clear whether trailing closures are associated
with `.init` of `T` or implicit `.callAsFunction`. So if constructor
didn't match trailing closures, let's attempt to split them off and
form an implicit `.callAsFunction` call with them as a fallback.
2022-02-03 15:33:38 -08:00
Pavel Yaskevich
4acd8caf12 [CSSimplify] Delay simplication of callable type construction constraint
Cannot do that during constraint generation because trailing closures
require special handling.
2022-02-03 15:33:38 -08:00
Pavel Yaskevich
12497b7145 [CSSimplify] Allow callers to provide argument list to matchCallArguments
This would make it much easier to implement trailing closure splitting
for during callable type construction.
2022-02-03 15:33:28 -08:00
Slava Pestov
aa67c8bf8a Parametrized => parameterized 2022-02-03 13:27:24 -05:00
Anthony Latsis
b3ee4b0718 AST, Sema: Use the opened archetype's generic signature to determine existential member availability 2022-02-02 02:09:59 +03:00
Anthony Latsis
3206f5ab34 [NFC] Preemptively relocate ProtocolDecl::isAvailableInExistential() and co. 2022-02-01 20:55:46 +03:00
Slava Pestov
e7e536705e AST: Introduce ParametrizedProtocolType 2022-01-26 00:11:38 -05:00
Doug Gregor
d484e12285 Merge pull request #40938 from DougGregor/opaque-witness-table-fixes
Opaque type fixes for interesting generic environments
2022-01-20 17:17:21 -08:00
Holly Borla
76e6156857 [Sema] Construct OpenedArchetypeType with a canonical existential type.
Opened archetypes can be created in the constraint system, and the
existential type it wraps can contain type variables. This can happen
when the existential type is inferred through a typealias inside a
generic type, and a member reference whose base is the opened existential
gets bound before binding the generic arguments of the parent type.

However, simplifying opened archetypes to replace type variables is
not yet supported, which leads to type variables escaping the constraint
system. We can support cases where the underlying existential type doesn't
depend on the type variables by canonicalizing it when opening the
existential. Cases where the underlying type requires resolved generic
arguments are still unsupported for now.
2022-01-19 22:53:52 -08:00
Doug Gregor
209167ae30 Eliminate spurious uses of ArchetypeType::getRoot(). 2022-01-19 09:54:34 -08:00
Doug Gregor
452eccab83 Remove NestedArchetypeType.
Nested archetypes are represented by their base archetype kinds (primary,
opened, or opaque type) with an interface type that is a nested type,
as represented by a DependentMemberType. This provides a more uniform
representation of archetypes throughout the frontend.
2022-01-14 21:28:21 -08:00
Holly Borla
6060de6be9 [AST] Teach ExistentialType::get to only produce ExistentialType when
explicit existential types are enabled.
2022-01-13 19:31:37 -08:00
Holly Borla
5dced8e5f9 [ConstraintSystem] Fix a few constraint system corner cases with explicit
existential types.
2022-01-13 19:30:44 -08:00
Doug Gregor
f5e40fed29 Eliminate another unnecessary use of an archetype's generic environment 2022-01-05 12:27:13 -08:00
Doug Gregor
d49a20e9dd Eliminate use of the opaque type's generic environment from type matching.
Introduce a new primitive operation on opaque type archetypes that
canonicalizes a type within the environment of the opaque type
archetype without building the environment itself. Use it when matching
opaque archetype types in the solver.
2022-01-05 10:16:33 -08:00
Robert Widmann
d44d8ec043 Allow Converting Pack Types to Tuples
Insert an implicit conversion from pack types to tuples with equivalent parallel structure. That means

1) The tuple must have the same arity
2) The tuple may not have any argument labels
3) The tuple may not have any variadic or inout components
4) The tuple must have the same element types as the pack
2021-12-16 08:51:38 -08:00
Robert Widmann
0471e2c58e Implement Type Sequence Parameter Opening
The heart of this patchset: An inference scheme for variadic generic functions and associated pack expansions.

A traditional variadic function looks like

func foo<T>(_ xs: T...) {}

Along with the corresponding function type

<T>(T [variadic]) -> Void

which the constraint system only has to resolve one two for. Hence it opens <T> as a type variable and uses each argument to the function to try to solve for <T>. This approach cannot work for variadic generics as each argument would try to bind to the same <T> and the constraint system would lose coherency in the one case we need it: when the arguments all have different types.

Instead, notice when we encounter expansion types:

func print<T...>(_ xs: T...)
print("Macs say Hello in", 42, "different languages")

We open this type as

print : ($t0...) -> ($t0...)

Now for the brand new stuff: We need to create and bind a pack to $t0, which will trigger the expansion we need for CSApply to see a coherent view of the world. This means we need to examine the argument list and construct the pack type <$t1, $t2, $t3, ...> - one type variable per argument - and bind it to $t0. There's also the catch that each argument that references the opened type $t0 needs to have the same parallel structure, including its arity. The algorithm is thus:

For input type F<... ($t0...), ..., ($tn..) ...> and an apply site F(a0, ..., an) we walk the type `F` and record an entry in a mapping for each opened variadic generic parameter. Now, for each argument ai in (a0, ..., an), we create a fresh type variable corresponding to the argument ai, and record an additional entry in the parameter pack type elements corresponding to that argument.

Concretely, suppose we have

func print2<T..., U...>(first: T..., second: U...) {}
print2(first: "", 42, (), second: [42])

We open print2 as

print2 : ($t0..., $t1...) -> Void

And construct a mapping

  $t0 => <$t2, $t3, $t4> // eventually <String, Int, Void>
  $t1 => <$t5> // eventually [Int]

We then yield the entries of this map back to the solver, which constructs bind constraints where each => exists in the diagram above. The pack type thus is immediately substituted into the corresponding pack expansion type which produces a fully-expanded pack type of the correct arity that the solver can actually use to make forward progress.

Applying the solution is as simple as pulling out the pack type and coercing arguments element-wise into a pack expression.
2021-12-16 01:16:45 -08:00
Doug Gregor
2b5bcc1062 Merge pull request #40544 from DougGregor/sr-15131-closure-effects
Extend "uses concurrency features" checks for closures currently being type checked
2021-12-14 11:02:03 -08:00
Doug Gregor
cc7904cf52 Use the closure type during type checking for establishing use of new features
When evaluating whether code is within a closure that uses concurrency
features, use the type of the closure as it's known during type checking,
so that contextual information (e.g., it's passed to a `@Sendable` or
`async` parameter of function type) can affect the result. This
corrects the definition for doing strict checking within a minimal
context for the end result of the type-check, rather than it's initial
state, catching more issues.

Fixes SR-15131 / rdar://problem/82535088.
2021-12-14 07:15:49 -08:00
Holly Borla
69be7b17fc Merge pull request #40282 from hborla/existential-any
[SE-0335] Introduce existential `any`
2021-12-10 08:56:03 -08:00
Holly Borla
445a856652 [Type System] Introduce a dedicated type to represent existential types.
The new type, called ExistentialType, is not yet used in type resolution.
Later, existential types written with `any` will resolve to this type, and
bare protocol names will resolve to this type depending on context.
2021-12-09 23:14:50 -08:00
LucianoAlmeida
85246bcc55 [test] Add regression test for SR-15562 2021-12-09 00:00:35 -03:00
LucianoAlmeida
b250723f58 [Sema] Do not consider warning fixes for non-augmenting fix recording logic 2021-12-08 23:54:05 -03:00
zoecarver
fc3b3a1d71 [cxx-interop] Implement foreign reference types.
This is an expiremental feature to allow an attribute, `import_as_ref`, to import a C++ record as a non-reference-counted reference type in Swift.
2021-12-08 15:35:18 +00:00
Pavel Yaskevich
0e6e058e7c [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-12-03 10:54:07 -08:00