Commit Graph

1988 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
46ff410a23 [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-12-03 10:53:50 -08:00
Doug Gregor
91fb149faf Merge pull request #40388 from DougGregor/swift-6-stricter-concurrency-checking
Swift 6 stricter concurrency checking
2021-12-03 08:40:46 -08:00
Xi Ge
dde843c04d Merge pull request #40377 from nkcsgexi/const-propery-wrapper
sema: always record fix for mismatched constness.
2021-12-02 19:21:17 -08:00
Xi Ge
3377591b3c sema: always record fix for mismatched constness.
Also, this commit has added a test for using _const values in property wrapper.
2021-12-02 12:19:41 -08:00
Doug Gregor
30c5cf2ab3 In Swift 6, every context requires strict concurrency checking
This ensures, among other things, that `@_predatesConcurrency` doesn't
affect the types of entities anywhere in a module compiled in Swift 6, so
we get stricter checking throughout.
2021-12-02 11:44:10 -08:00
Doug Gregor
365f0afa9f Downgrade concurrency-related function type errors in existing code
When in "existing" Swift code that is Swift 5.x and has not adopted
concurrency, allow mismatches in function types that only involve
ABI-neutral concurrency changes (e.g., adding `@Sendable` or removing
a global actor) by downgrading the diagnostic to a warning. This
improves the story for incremental adoption of concurrency in an
existing code base.

As part of this, generalize the facility for downgrading an error to a
warning when performing diagnostics in the constraint solver, using the
new diagnostic behavior limits rather than duplicating diagnostics.
2021-12-02 10:33:01 -08:00
Xi Ge
4b0fc14a5e Merge pull request #40333 from nkcsgexi/enum-element-constness
sema: accept enum element reference as compile-time const value
2021-11-30 14:13:11 -08:00
Xi Ge
fdcb08ead2 sema: accept enum element reference as compile-time const value 2021-11-30 09:27:42 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Xi Ge
03c76bd32d sema: diagnose passing a non-constant value into a constant parameter 2021-11-22 11:52:57 -08:00
Hamish Knight
237338b504 Merge pull request #40224 from hamishknight/super-tuple-shuffle 2021-11-18 09:50:09 +00:00
Robert Widmann
1faf7edbd6 Merge pull request #40193 from CodaFi/sequential-access-patterns
Model Sequence Archetypes
2021-11-17 13:19:31 -08:00
Hamish Knight
4aaec65780 [CS] Warn on mismatched tuple labels for subtyping
This is something that we'd like to fix to bring
in line with tuple conversion, so start warning on
cases where it occurs.
2021-11-17 17:06:21 +00:00
Hamish Knight
da36a2cb88 [CS] Restore a type variable for compatibility with rdar://85263844
Despite being otherwise disconnected from the
constraint system, it's possible for it to affect
how we type-check tuple matches in certain cases.

This is due to the fact that:
- It can have a lower type variable ID than an
opened generic parameter type, so becomes the
representative when merged with it. And because it
has a different locator, this can influence
binding prioritization.
- Tuple subtyping is broken, as it's currently a
*weaker* relationship than conversion.

Therefore, temporarily restore this bit of logic
for language versions < 6. If possible, we should
try and fix tuple subtying in Swift 6 mode to not
accept label mismatches, so that it's not more
permissive than tuple conversion.

rdar://85263844
2021-11-17 17:06:19 +00:00
Robert Widmann
e7e11df927 Model Sequence Archetypes 2021-11-16 11:38:57 -08:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Pavel Yaskevich
3927f56dbd [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-11-15 16:42:04 -08:00
Pavel Yaskevich
c4b3f86a70 [Diagnostics] Don't assume that contextual type for optional chain is optional
While checking validity of the optional chain, let's not assume
that the contextual type associated with is optional because
enclosing expression could too be invalid.

Resolves: rdar://85166519
2021-11-09 09:42:45 -08:00
Pavel Yaskevich
0c70f5650f Merge pull request #39592 from Jumhyn/contextual-placeholder-followup
Remove default argument from getContextualType
2021-11-08 10:00:36 -08:00
Pavel Yaskevich
7f94bbf29b [AST] NFC: Drop Type from TypeBase::isCGFloatType
This makes naming consistent with other accessors e.g. `isDouble`
2021-11-03 18:09:14 -07:00
Pavel Yaskevich
9e3a0586c4 [CSSimplify] Detect and fix implicit Double <-> CGFloat conversion via optional chaining
Passing Double? to CGFloat? and vice versa is not supported but
there was one case which solver still allowed to get through -
optional chains, which is not fixed.
2021-11-03 14:42:13 -07:00
Pavel Yaskevich
f765390c62 [ConstraintSystem] Convert Fixes to a set vector to avoid duplicates 2021-10-22 10:09:09 -07:00