Commit Graph

663 Commits

Author SHA1 Message Date
Pavel Yaskevich
f765390c62 [ConstraintSystem] Convert Fixes to a set vector to avoid duplicates 2021-10-22 10:09:09 -07:00
Pavel Yaskevich
d74e8c9407 [ConstraintSystem] Convert ConversionRestrictions to a map vector to avoid duplicates 2021-10-22 10:09:08 -07:00
Pavel Yaskevich
880374eaf3 [ConstraintSystem] Convert DisjunctionChoices to a map vector & assert on duplicates 2021-10-22 10:09:08 -07:00
Pavel Yaskevich
19c253bec2 [ConstraintSystem] Convert argumentMatchingChoices to a map vector to avoid duplicates 2021-10-22 10:09:08 -07:00
Pavel Yaskevich
e95e43d019 [ConstraintSystem] Convert ImplicitValueConversions to a map vector to avoid duplicates 2021-10-22 10:09:08 -07: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
Pavel Yaskevich
cfcc85268a [ConstraintSystem] Convert DefaultConstraints to a set vector to avoid duplicates
It's always been the case that partial solutions introduce
some storage duplication when applied back to the constraint
system to form a more complete solution with outer context,
 but the constraint systems used to be small before
introduction of result builders (and now multi-statement
inference), which make the duplication more visible.
2021-10-22 10:09:08 -07:00
Doug Gregor
0c9f9f7a43 Use a SmallSetVector to eliminate duplicates 2021-10-20 23:15:21 -07:00
Doug Gregor
45824befd2 Infer 'isolated' closure parameters from context.
When a closure is provided with a contextual type that has isolated
parameters, infer that the corresponding closure parameter is "isolated".

Fixes rdar://83732479.
2021-10-20 21:51:42 -07:00
Hamish Knight
d3618ef73c Merge pull request #39302 from hamishknight/iuo-a-refactoring 2021-10-15 23:35:47 +01:00
Pavel Yaskevich
d8ebc558b7 [ConstraintSystem] Add possibility to mark conjunction element producers as exhausted 2021-10-14 11:20:54 -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
b8e4c676c6 [CS] Remove function component constraints
FunctionInput relies on being able to represent
parameter lists as tuples, which won't be possible
once parameter flags are stripped from tuple types.
FunctionResult is reasonable, but is currently
unused.
2021-10-12 09:51:45 +01:00
Luciano Almeida
ca8b660a1c Merge pull request #39648 from LucianoPAlmeida/SR-15281-cast
[SR-15281] [Sema] Couple of contextual mismatch and runtime cast diagnostic fixes
2021-10-10 08:03:24 -03:00
LucianoAlmeida
5424797e79 [Sema] Make constraint restriction to store TypeBase * instead of Type 2021-10-09 14:26:21 -03:00
Pavel Yaskevich
67a721485f [ConstraintSystem] Compute variables referenced by conjunction elements incrementally
Attempting to pre-compute a set of referenced type variables
upfront is incorrect because parameter(s) and/or result type
could be bound before conjunction is attempted. Let's compute
a set of referenced variables before each element gets attempted.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
8867a8d407 [CSApply] Delay solution application to multi-statement closure bodies
Let's delay solution application to multi-statement closure bodies,
because declarations found in the body of such closures may depend
on its `ExtInfo` flags e.g. no-escape is set based on parameter if
closure is used in a call.
2021-10-08 10:08:03 -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
55bae6150d [Constraint] Allow closure body elements to carry contextual information 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
d74cb79ea3 [ConstraintSystem/Closures] Add support for repeat .. while statement 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
28d1bacdce [ConstraintSystem] Implement conjunction step
Iterate over all of the elements one-by-one and make sure that
each results in a single solution, otherwise fail the conjunction step.

Once all of the elements are handled either stop or,
if conjunction step has been performed in isolation,
return all of the outer constraints back to the system
and attempt to solve for outer context - that should
produce one or more solutions for conjunction to be
considered successfully solved.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
441f8079be [ConstraintSystem] Expose type variables referenced by conjunction element 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
0324a4cafc [ConstraintSystem] Implement new required methods in ConjunctionElementProducer 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
73dffb3125 [ConstraintSystem] Don't require a type variable per closure body element
Since each of the body elements is already going to have a type associated
with it there is no need to create yet another type variable.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
27275f6214 [CSStep] Add an implementation of ConjunctionStep
It behavies similar to `DisjunctionStep` but attempts all of
its elements unless there is an inference failure.
2021-10-08 10:08:00 -07:00
Pavel Yaskevich
2377f0507c [ConstraintSystem] Add a conjunction element producer
In preporation to add `ConjunctionStep`, let's first
introduce a `ConjunctionElement` and `ConjunctionElementProducer`.
2021-10-08 10:08:00 -07:00
Pavel Yaskevich
eb8eabfba7 [ConstraintSystem] Add a skeleton of ClosureBodyElement constraint 2021-10-08 10:08:00 -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
Frederick Kellison-Linn
f6b6f0cd2b Lift replaceInferableTypes call into getContextualType 2021-10-04 23:36:33 -04:00
Alex Hoppen
4521c99d56 Merge pull request #38782 from ahoppen/pr/sourcekit-cancellation
[SourceKit] Support cancellation of requests while an AST is being built
2021-09-28 20:08:14 +02:00
Hamish Knight
11617b1807 [CS] Move buildDisjunctionForOptionalVsUnderlying into .cpp 2021-09-24 15:51:09 +01:00
Alex Hoppen
7a80034e35 [SourceKit] Support cancellation of requests while an AST is being built
This commit refactors the way ASTs are being built in SourceKit and how `SwiftASTConsumer`s are served by the built ASTs. `SwiftASTManager.h` should give an overview of the new design.

This commit does not change the cancellation paradigm in SourceKit (yet). That is, subsequent requests with the same `OncePerASTToken` still cancel previous requests with the same token. But while previously, we were only able to cancel requests that haven’t started an AST build yet, we can now also cancel the AST build of the to-be-cancelled requests.

With this change in place, we can start looking into explicit cancellation of requests or other cancellation paradigms.
2021-09-23 11:53:45 +02: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
Alex Hoppen
998a7871f8 [Sema] Add cancellation flag to typechecker options
Once this flag is set, the type checker will bail out with a “expression to complex” error.
2021-09-22 12:01:00 +02:00
Pavel Yaskevich
f368e5a7b6 [ConstraintSystem] NFC: Extract Swift -> C pointer conversion simplification into a separate method 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
Hamish Knight
ddff9c4c9d [CS] Store argument list mappings on solutions
Roll back argument list mappings in the constraint
system at the end of solver scopes, and copy
argument list mappings into solutions.
2021-09-06 12:54:49 +01:00
Hamish Knight
632bf41768 [CS] Make ArgumentLists a private member
Move associateArgumentList onto ConstraintSystem
and make ArgumentLists private in preparation for
having it roll back at the end of solver scopes.
2021-09-06 12:54:49 +01:00
Hamish Knight
dc60996f89 Merge pull request #38836 from hamishknight/toil-and-tuple 2021-09-04 19:30:04 +01:00
Holly Borla
09f2ca7f25 Merge pull request #39146 from hborla/wrapped-param-diagnostics
[ConstraintSystem] Fix a misleading argument mismatch error when using wrapped parameters.
2021-09-02 22:10:21 -07:00
Holly Borla
138c1170ea [CSGen] Declare generateWrappedPropertyTypeConstraints as a method
on ConstraintSystem.
2021-09-02 17:37:44 -07:00
Pavel Yaskevich
52307608e4 [ConstraintSystem] Get pattern from pattern binding for uninitialized variable targets
Instead of passing pattern directly, let's fetch it from the pattern
binding and remove extra argument from `SolutionApplicationTarget::forUninitializedVar`.
2021-09-02 11:41:43 -07:00
Hamish Knight
3e28bbbd2c Update for review feedback
- Remove OriginalArguments in favor of storing the
pre-rewritten argument list, which simplifies things
nicely
- Adopt llvm::indexed_accessor_iterator
2021-09-01 18:40:29 +01:00
Hamish Knight
8c2b88abc0 [CS] Adopt ArgumentList
- Explicitly limit favoring logic to only handle
unary args, this seems to have always been the
case, but needs to be handled explicitly now that
argument lists aren't exprs
- Update the ConstraintLocator simplification to
handle argument lists
- Store a mapping of locators to argument lists
in the constraint system
- Abstract more logic into a getArgumentLocator
method which retrieves an argument-to-param locator
from an argument anchor expr
2021-09-01 18:40:25 +01:00
Pavel Yaskevich
c1269883b8 [ConstraintSystem] Allow IUO types to be unrelated while forming a disjunction
Detect situations when type of a declaration hasn't been resolved yet
(one-way constraints would use a type variable to represent a type of IUO pattern),
and use additional type variable and a constraint to represent an
object type of a future optional type.

Resolves: SR-14893
Resolves: rdar://80271666
2021-08-30 11:59:43 -07:00
Pavel Yaskevich
83cc91869b Merge pull request #39031 from xedin/fix-uninit-var-pattern-binding
[ConstraintSystem] Store pattern binding associated with uninitialize…
2021-08-25 09:49:30 -07:00
Pavel Yaskevich
609ca72302 [ConstraintSystem] Store pattern binding associated with uninitialized variable
This is a follow-up to the uninitialized variable generalization.
Pattern binding declaration has to be stored together with the
variable because all of the entries in `SolutionApplicationTarget`
form a union.
2021-08-24 15:31:36 -07:00
Pavel Yaskevich
57c4b6a336 [ResultBuilders] Fix handling of multiple pattern bindings
`SolutionApplicationTargetsKey` was constructing pattern binding
entries with incorrect `kind`, which led to crashes for pattern
bindings with multiple initialized entries.
2021-08-24 12:37:12 -07:00
Pavel Yaskevich
d64c19ec37 [ConstraintSystem] Solution application target for uninitialized pattern binding 2021-08-17 14:23:57 -07:00