Commit Graph

1102 Commits

Author SHA1 Message Date
Holly Borla
626bea2bc6 [ConstraintSystem] Return an existential type in getTypeOfMemberReference
when the member is a typealias to a protocol or composition.
2022-01-13 22:31:55 -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
Holly Borla
d971d489d6 [Type System] With explicit existential types, make sure existential
metatypes always wrap the constraint type directly, and the instance
type of an existential metatype is an existential type.
2022-01-13 19:30:44 -08:00
Holly Borla
6cee193fc0 [Type System] When explicit existential types are enabled, wrap Error
in ExistentialType for the type of error values.
2022-01-13 19:30:44 -08:00
Doug Gregor
99a9e95a93 Merge pull request #40747 from DougGregor/opaque-type-archetype-environment 2022-01-06 06:50:50 -08:00
Pavel Yaskevich
9f7d3fccad Merge pull request #40708 from xedin/disable-multi-stmt-inference-inside-result-builder-bodies
[ConstraintSystem] SE-0326: Temporarily prevent multi-statement closure inference in result builder contexts
2022-01-06 01:04:22 -08:00
Doug Gregor
859110cc97 Stop opening the "bound" generic signature of an opaque type declaration.
Creation of the "bound" generic signature isn't possible with interface
types or type variables, so open up the opaque interface signature
instead and separately bind the outer type parameters as appropriate.
2022-01-05 12:04:48 -08:00
Doug Gregor
791c9c7d13 Implement most of the semantic analysis for named opaque result types.
Address small gaps in several places to make named opaque result types
partially work:

* Augment name lookup to look into the generic parameters when inside the
result type, which is used both to create structure and add requirements
via a `where` clause.
* Resolve opaque generic type parameter references to
OpaqueTypeArchetypeType instances, as we do for the "some" types
* Customize some opaque-type-specific diagnostics and type printing to
refer to the opaque generic parameter names specifically
* Fix some minor issues with the constraint system not finding
already-opened opaque generic type parameters and with the handling of
the opaque result type candidate set.

The major limitation on opaque types, where we cannot add requirements
that aren't strictly protocol or superclass requirements on the
generic parameters, remains. Until then, named opaque result types are
no more expressive than structural opaque result types.
2022-01-04 21:14:04 -08:00
Doug Gregor
b97ef02d85 Opaque opaque types and compute substitutions in the constraint system
Opaque opaque types and record them within the "opened types" of the
constraint system, then use that information to compute the set of
substitutions needed for the opaque type declaration using the normal
mechanism of the constraint solver. Record these substitutions within
the underlying-to-opaque conversion.

Use the recorded substitutions in the underlying-to-opaque conversion
to set the underlying substitutions for the opaque type declaration
itself, rather than reconstructing the substitutions in an ad hoc manner
that does not account for structural opaque result types.
2021-12-26 20:33:58 -08:00
Pavel Yaskevich
efafe02a30 [ConstraintSystem] SE-0326: Temporarily prevent multi-statement closure inference in result builder contexts
Multi-statement closure inference doesn't play well with result builders
at the moment because it expects all of the information from the parent
statements to be inferred before solver starts working on the body of a
nested closure. Let's prevent inference for nested multi-statement closures
until result builders are ported to use conjunctions and solve the body
incrementally top-down instead of in one shot.
2021-12-25 17:09:19 -08:00
Robert Widmann
e5bfda7c6e Merge pull request #40587 from CodaFi/substitute-teacher
Initial Semantics for Variadic Generics
2021-12-20 11:25:25 -08:00
Pavel Yaskevich
34575e3a0d [TypeChecker] Support _openExistential with async functions 2021-12-17 16:37:49 -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
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
Doug Gregor
c3b6160af8 Generalize and cache the "closure effects" determined from the closure body.
Use this to enable better detection of async contexts when determining
whether to diagnose problems with concurrency.

Part of SR-15131 / rdar://problem/82535088.
2021-12-13 21:32:28 -08:00
Pavel Yaskevich
0e6e058e7c [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-12-03 10:54:07 -08:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Anthony Latsis
9fc633e0b8 Merge pull request #39652 from AnthonyLatsis/no-context
TypeResolution: Abolish TypeResolutionStage::Contextual
2021-11-29 16:44:27 +03:00
Anthony Latsis
3c17d35f34 CS: Use TypeResolutionStage::Interface for applying generic arguments in 'openUnboundGenericType' 2021-11-19 16:44:07 +03:00
Anthony Latsis
e48f4830dd TypeResolution: Replace more 'forContextual' uses with 'resolveContextualType' 2021-11-19 16:44:03 +03:00
swift-ci
37ea254cc1 Merge pull request #40220 from DougGregor/predates-concurrency 2021-11-19 01:14:42 -08:00
Doug Gregor
f32572b4cd Apply @_predatesConcurrency adjustments to variables, subscripts. 2021-11-18 21:12:28 -08:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Pavel Yaskevich
106922545c Merge pull request #40074 from xedin/rdar-84879566
[ConstraintLocator] Allow simplification of constructor member with overloaded base
2021-11-08 10:13:49 -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
609841710f [ConstraintLocator] Allow simplification of constructor member with overloaded base
`init` calls to redeclared types would end up diagnosed as ambiguity,
so locator simplification needs to account for the fact that "base"
of constructor might be overloaded type reference.

Resolves: rdar://84879566
2021-11-05 16:44:07 -07:00
Pavel Yaskevich
4c415abc44 [ConstraintSystem] Add missing cases to getLoc(ASTNode)
Recently added `StmtCondition` and `CastLabelItem` wheren't
handled at all.
2021-11-05 13:36:10 -07:00
Hamish Knight
fb3796971e [CS] Simplify getAlternativeLiteralTypes
Caching the result here feels a little overkill as
it's only useful for one protocol, and the
`TypeChecker::getDefaultType` computation is
cached by the request evaluator.
2021-11-02 13:55:04 +00:00
Pavel Yaskevich
568c27f551 [ConstraintSystem] Convert Opened{Existential}Types to a map vector to avoid duplicates
Same reasons as with `DefaultConstraints` but also improves
code ergonomics around opened types.
2021-10-22 10:09:08 -07:00
Holly Borla
0601498ce7 Merge pull request #39831 from hborla/invalid-selector-property
[ConstraintSystem] Don't increase the score for unapplied function references if the expression is an argument to `#selector`.
2021-10-20 19:37:48 -07:00
Holly Borla
beaaee7af1 [ConstraintSystem] Don't increase the score for unapplied function references
if the expression is an argument to `#selector`.

For `#selector` arguments, functions and properties are syntactically distinct
with the getter/setter label, so the solver should not unconditionally prefer
properties to unapplied functions. A better fix for this is to port over the
`#selector` diagnostics from CSApply, and not attempt invalid disjunction choices
based on the selector kind on the valid code path.
2021-10-20 09:19:25 -07:00
Doug Gregor
913650dab1 Centralize Dispatch-related @_unsafe(Sendable|MainActor) hacks 2021-10-19 22:50:19 -07:00
Doug Gregor
1b8f562852 Adjust the referenced function type for @_unsafeSendable and @_unsafeMainActor.
When referencing a function that contains parameters with the hidden
`@_unsafeSendable` or `@_unsafeMainActor` attributes, adjust the
function type to make the types of those parameters `@Sendable` or
`@MainActor`, respectively, based on both the context the expression:

* `@Sendable` will be applied when we are in a context with strict
concurrency checking.
* `@MainActor` will be applied when we are in a context with strict
concurrency checking *or* the function is being directly applied so
that an argument is provided in the immediate expression.

The second part of the rule of `@MainActor` reflects the fact that
making the parameter `@MainActor` doesn't break existing code (because
there is a conversion to add a global actor to a function value), but
it does enable such code to synchronously use a `@MainActor`-qualified
API.

The main effect of this change is that, in a strict concurrency
context, the type of referencing an unapplied function involving
`@_unsafeSendable` or `@_unsafeMainActor` in a strict context will
make those parameters `@Sendable` or `@MainActor`, which ensures that
these constraints properly work with non-closure arguments. The former
solution only applied to closure literals, which left some holes in
Sendable checking.

Fixes rdar://77753021.
2021-10-19 22:50:17 -07:00
Hamish Knight
d3618ef73c Merge pull request #39302 from hamishknight/iuo-a-refactoring 2021-10-15 23:35:47 +01:00
Pavel Yaskevich
6de2e63251 Merge pull request #39774 from xedin/fix-no-assert-warnings
[Sema] Fix unused function/variable warnings in no-assertion configuration
2021-10-15 15:31:56 -07:00
Pavel Yaskevich
84fa8a35e6 [Sema] Fix unused function/variable warnings in no-assertion configuration 2021-10-15 13:22:44 -07:00
Pavel Yaskevich
ffca407138 [ConstraintSystem] NFC: Remove unnecessary castToExpr from diagnoseAmbiguity 2021-10-14 12:51:15 -07:00
Pavel Yaskevich
df44c4f4d7 [ConstraintSystem] Fix non-determinism in diagnoseAmbiguity
Simplification of member locator would produce a base expression,
this is what we want for diagnostics but not for comparisons in
`diagnoseAmbiguity` because base expression is located at a different
depth which would lead to incorrect results if both reference and base
expression are ambiguous e.g. `test[x].count` if both `[x]` and `count`
are ambiguous than simplification of `count` would produce `[x]` which
is incorrect.

This is the test-case (already in the suite) that exibits this behavior:

```
func test_ambiguity_with_placeholders(pairs: [(rank: Int, count: Int)]) -> Bool {
  return pairs[<#^ARG^#>].count == 2
}
```

Here subscript would either return a tuple or `ArraySlice` and
`count` is ambiguous because both have it.
2021-10-14 11:26:38 -07:00
Hamish Knight
287fa8e8de [CS] Refactor IUO handling
The current IUO design always forms a disjunction
at the overload reference, for both:

- An IUO property `T!`, forming `$T := T? or T`
- An IUO-returning function `() -> T!`, forming `$T := () -> T? or () -> T`

This is simple in concept, however it's suboptimal
for the latter case of IUO-returning functions for
a couple of reasons:

- The arguments cannot be matched independently of
  the disjunction
- There's some awkwardness when it comes e.g wrapping
  the overload type in an outer layer of optionality
  such as `(() -> T!)?`:
  - The binding logic has to "adjust" the correct
    reference type after forming the disjunction.
  - The applicable fn solving logic needs a special
    case to handle such functions.
- The CSApply logic needs various hacks such as
  ImplicitlyUnwrappedFunctionConversionExpr to
  make up for the fact that there's no function
  conversion for IUO functions, we can only force
  unwrap the function result.
  - This lead to various crashes in cases where
    we we'd fail to detect the expr and peephole
    the force unwrap.
  - This also lead to crashes where the solver
    would have a different view of the world than
    CSApply, as the former would consider an
    unwrapped IUO function to be of type `() -> T`
    whereas CSApply would correctly see the overload
    as being of type `() -> T?`.

To remedy these issues, IUO-returning functions no
longer have their disjunction formed at the overload
reference. Instead, a disjunction is formed when
matching result types for the applicable fn
constraint, using the callee locator to determine
if there's an IUO return to consider. CSApply then
consults the callee locator when finishing up
applies, and inserts the force unwraps as needed,
eliminating ImplicitlyUnwrappedFunctionConversionExpr.

This means that now all IUO disjunctions are of the
form `$T := T? or T`. This will hopefully allow a
further refactoring away from using disjunctions
and instead using type variable binding logic to
apply the correct unwrapping.

Fixes SR-10492.
2021-10-12 14:14:33 +01:00
Hamish Knight
f26f136569 [CS] Tweak dynamic member constraint generation
In order to fit with the new IUO model where
functions with IUO results have the disjunction
formed when matching result types, we need to
update this logic to form applicable fn
constraints for the implicit `x[dynamicMember:]`
subscript call.

This is done using a new ImplicitDynamicMemberSubscript
locator path element to allow easy identification of
what the right callee and argument list should be.
2021-10-12 14:14:32 +01:00
Pavel Yaskevich
99a9828a03 [ConstraintSystem] Add a new contextual purpose - CTP_CaseStmt 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
7197fac8f5 [ConstraintSystem] Add a new contextual purpose - CTP_ForEachSequence
A contextual purpose for a sequence expression associated with
`for-in` statement, that decays into a `ConformsTo` constraint
to a `Sequence` or `AsyncSequence` protocol.

Note that CTP_ForEachSequence is almost identical to CTP_ForEachStmt
but the meaning of latter is overloaded, so to avoid breaking solution
targets I have decided to add a new purpose for now.
2021-10-08 10:08:02 -07:00
Pavel Yaskevich
1c0a306c75 Merge pull request #37956 from xedin/implicit-swift-to-c-ptr-conversions
[TypeChecker] Implement limited set of conversions between Swift and C pointers
2021-10-06 14:35:22 -07:00
Frederick Kellison-Linn
b433724735 Remove default argument from getContextualType 2021-10-05 17:04:54 -04:00
Hamish Knight
d74f515a07 [CS] Handle placeholder in buildDisjunctionForOptionalVsUnderlying
If we encounter a placeholder type here, propagate
it to the type variable, as we don't know whether
it should be optional or non-optional, and we
would have already recorded a fix for it.

SR-15219
rdar://83352038
2021-09-24 15:51:10 +01:00
Hamish Knight
11617b1807 [CS] Move buildDisjunctionForOptionalVsUnderlying into .cpp 2021-09-24 15:51:09 +01:00
Holly Borla
1737303602 [ConstraintSystem] Increase the score when attempting overload choices
for unapplied references when the choice is a function declaration.

This will allow the solver to prune those overload choices when it
has already found a solultion with a property (all else equal in the
score). This is already done as an ambiguity tie-breaker in solution
ranking, but adding this bit to the score will prune a lot of search
space within the solver.
2021-09-22 13:33:44 -07:00
Pavel Yaskevich
25762c25ee [CSSimplify] Make sure that Swift -> C pointer conversion always waits for optionals
Wait for a value-to-optional promotion or optional-to-optional conversion
to happen before attempting Swift -> C pointer conversion.
2021-09-20 17:22:26 -07:00
Pavel Yaskevich
4f97ef0e75 [ConstraintSystem] Add a helper to determine whether locator is for argument to C/ObjC imported decl 2021-09-20 17:22:25 -07:00
Pavel Yaskevich
3a41f7af5f [ConstraintSystem] Add a new conversion - Swift to C pointers
Following pointer conversions are supported in argument positions (when referencing C/ObjC functions):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
2021-09-20 17:22:25 -07:00