Commit Graph

1082 Commits

Author SHA1 Message Date
Doug Gregor
7f9029071d [Constraint system] Adopt rewriteTarget for function builder transform.
When applying a function builder to a closure to produce the final,
type-checked closure, use the new rewriteTarget() so it’s performed on
a per-target basis. Use this to eliminate some duplicating in the handling
of return types.
2020-02-12 17:51:47 -08:00
Doug Gregor
efd357e7c7 [Constraint system] Factor out application to a SolutionApplicationTarget.
Pull out the core operation of rewriting a given SolutionApplicationTarget
into into a method on the ExprWalker that does the overall rewrite, so it
can be called multiple times within application.
2020-02-12 17:51:47 -08:00
Doug Gregor
3508dfb0ea [Constraint system] Use hash table lookup appropriately.
It’s pointlessly inefficient to do an O(n) scan through a hash table to match
keys!
2020-02-11 15:14:44 -08:00
Doug Gregor
f5b9517552 [Constraint system] Simplify and improve handling of node tyoes.
Rather than having separate dense maps for the type mappings of each
node type (expression, type location, variable declaration, pattern) that
a TypedNode can be, have a single such map. Sometimes we end up
setting a particular node's type more than once, so cope with that
by restoring the prior type.
2020-02-11 15:14:44 -08:00
Holly Borla
607c298493 [Diagnostics] Refactor diagnoseAmbigutiyWithFixes to use the differences between
solutions in order to figure out the source of ambiguity.
2020-02-11 14:52:41 -08:00
Doug Gregor
a1932f10e5 [Constraint solver] Sink remaining logic from typeCheckBinding.
Move the remaining logic of typeCheckBinding, which consists of applying
the solution to the pattern, down into the normal solution application
path. typeCheckBinding() is now a thin shell over the normal
typeCheckExpression() convenience function.
2020-02-10 22:01:37 -08:00
Doug Gregor
a2640b73ee [Constraint system] Finish allowing one to record pattern types. 2020-02-10 22:01:37 -08:00
Doug Gregor
caf2f62ea3 Merge pull request #29717 from DougGregor/init-via-solution-target
[Constraint system] Sink initialization logic into SolutionApplicationTarget
2020-02-08 20:38:09 -08:00
Doug Gregor
d70c055888 [Constraint system] Sink more init logic into SolutionApplicationTarget
More steps toward eliminating ExprTypeCheckListener.
2020-02-07 22:46:05 -08:00
Doug Gregor
614f618e5d [Constraint system] Allow recording pattern types in the system. 2020-02-07 21:32:13 -08:00
Doug Gregor
efd4152061 [Constraint solver] Factor constraint generation logic for solution targets
Move more constraint generation logic for an expression target into a
new generateConstraints() on a solution target, so we can further
generalize the main “solve” logic.
2020-02-07 20:56:32 -08:00
swift-ci
08dca4666e Merge pull request #29635 from DougGregor/simplify-solution-target 2020-02-04 02:36:15 -08:00
Doug Gregor
3981a68a76 Merge pull request #29626 from DougGregor/function-builders-single-expr-closures-return
[Constraint solver] Fix function builders with single-expression closures
2020-02-03 23:10:51 -08:00
Doug Gregor
bbc0a95723 [Constraint system] Simplify creation of contextual conversion constraint.
We were storing more state than necessary in the constraint system.
2020-02-03 22:38:24 -08:00
Doug Gregor
9569714334 Clarify method names in SolutionApplicationTarget. 2020-02-03 22:11:03 -08:00
Doug Gregor
36366bc3e4 [Constraint solver] One SolutionApplicationTarget instance to rule them all.
Capture the peculiarities of contextual types vs. types used to generate
conversion constraints, as well as the behavior of “optional some” patterns
as used by if let / while let, within SolutionApplicationTarget. This allows
us to use a single target throughout setup / solving / application, rather
than mapping between two similar-but-disjoint targets.
2020-02-03 22:02:21 -08:00
Doug Gregor
f8eee50310 [Constraint solver] Fix function builders with single-expression closures
Fix a few related issues involving the interaction with
single-expression closures:

* A single-expression closure can have a "return" in it; in such
cases, disable the function-builder transform.
* Have the function builder constraint generator look through the
"return" statement in a single-expression closure the same way as
solution application does

Fixes rdar://problem/59045763, where we rejected some well-formed code
involving a single-expression closure with a "return" keyword.
2020-02-03 17:52:44 -08:00
Doug Gregor
9278fb8c22 [Constraint system] Eliminate TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType
We can compute this information based solely on the SolutionApplicationTarget,
so do it.
2020-02-02 23:41:24 -08:00
Doug Gregor
365511cfb4 [Constraint system] Remove TypeCheckExprFlags::ExpressionTypeMustBeOptional.
This result can be computed from the pattern of an initializer expression, so
do that instead.
2020-02-02 22:41:10 -08:00
Doug Gregor
52da2d6c4c [Constraint system] Extend SolutionApplicationTarget with an init pattern.
Initializer expressions are always type checked against a pattern, so also
include the pattern in the solution application target and use that to
compute the contextual type from the pattern type.
2020-02-02 22:31:27 -08:00
Doug Gregor
3b761dcae1 [Constraint system] Add a SolutionApplicationTarget-based typeCheckExpression
Rework most of typeCheckExpression() to use SolutionApplicationTarget,
folding more information into that data structure and sinking more
expression-checking behavior down into the more general solver and
solution-application code.
2020-02-01 23:46:13 -08:00
Doug Gregor
3190433d58 [Constraint system] Give SolutionApplicationTarget purpose.
A contextual type purpose, that is, so it captures more about what the
entity is that is being solved.
2020-01-31 22:03:13 -08:00
Doug Gregor
9fa03cd648 [Type checker] Eliminate TypeCheckExprFlags::ConvertTypeIsOnlyAHint.
This flag is recoverable from the contextual type purpose; don’t duplicate
the logic.
2020-01-31 21:11:26 -08:00
Doug Gregor
e0702d9f2e [Constraint solver] Generalize solve() for arbitrary solution targets.
Start cleaning up the main “solve” entry point for solving an expression
and applying the solution, so it handles arbitrary solution targets.
This is another small step that doesn’t do much on its own, but will help
with unifying the various places in the code base where we run the solver.
2020-01-30 22:01:12 -08:00
Doug Gregor
56aeac5d0a [Constraint system] Push SolutionApplicationTarget into the solveImpl
Baby steps toward generalizing the expression-centric solver core.
2020-01-30 22:01:12 -08:00
Doug Gregor
eb2862ec1e [Constraint system] Collapse applySolutionImpl() into applySolution().
Now that we have a unified notion of a SolutionApplicationTarget, use it
to collapse 3 applySolution-ish functions into one.
2020-01-30 22:01:12 -08:00
Doug Gregor
e3124dcb5f [Constraint system] Expand SolutionApplicationTarget for expressions.
Add the final conversion type and the flag indicating whether a given
expression is discarded to SolutionApplicationTarget, rather than 
separating the arguments to the solver implementation.
2020-01-30 22:01:12 -08:00
Doug Gregor
ad1ad43c6e Generalize SolutionApplicationTarget and use it more widely.
Make this type suitable for representing the result of solution application,
and do so.
2020-01-30 22:01:12 -08:00
Pavel Yaskevich
c662a91578 Merge pull request #29490 from xedin/remove-more-code-from-visitapplyexpr
[Diagnostics] Don't re-typecheck function expression associated with various calls
2020-01-28 00:32:05 -08:00
Pavel Yaskevich
dea79e09c6 [ConstraintSystem] Detect that function type has failures before applying arguments
If a type variable representing "function type" is a hole
or it could be bound to some concrete type with a help of
a fix, let's propagate holes to the "input" type. Doing so
provides more information to upcoming argument and result matching.
2020-01-27 16:53:33 -08:00
Doug Gregor
46a65009e3 [Constraint system] Eliminate global flag UnderlyingTypeForOpaqueReturnType
Make this information contextual (per type) rather than global (per
constraint system) so we don’t misapply it.
2020-01-27 15:44:54 -08:00
Doug Gregor
d752f414d3 [Constraint solver] Rework solveImpl(Expr*) to return SolutionResult.
SolutionResult better captures what can happen when solving a constraint
system for the given expression occurs than the ad hoc SolutionKind return
& small vector of Solution results. Also, try to make this logic less
convoluted.
2020-01-26 12:05:43 -08:00
Doug Gregor
5063cb5b79 [Constraint solver] Factor out adding a contextual conversion constraint. 2020-01-26 12:05:43 -08:00
Doug Gregor
f7744bd8b2 [Constraint solver] Make DeclContext explicit. NFC 2020-01-26 12:05:43 -08:00
Doug Gregor
27cd8d2afb [Constraint system] Save contextual types into each Solution
We’ll want to pull these back out for diagnostic purposes.
2020-01-24 10:17:59 -08:00
Doug Gregor
5669985665 Merge pull request #29419 from DougGregor/function-builder-if-available
[Function builders] Add support for "if #available".
2020-01-24 10:17:22 -08:00
Doug Gregor
7217cfa4a5 [Constraint system] Move statement condition constraint generation.
Move constraint generation for statement conditions onto the
constraint system; it doesn't really have any reason to be located
within the function builder transform.
2020-01-23 21:51:53 -08:00
Doug Gregor
83cb35139a [Constraint solver] Allow contextual types for multiple expressions.
Generalize the representation of contextual types so we can store
contextual types for more than one expression. Allow these to be
added/rolled back in solver scopes. Nothing uses this functionality
yet.
2020-01-23 13:56:36 -08:00
Doug Gregor
133439dcbb [Constraint solver] Request contextual type information per expression.
When requesting information about the contextual type of a constraint
system, do so using a given expression rather than treating it like
the global state that it is.
2020-01-23 11:46:17 -08:00
Pavel Yaskevich
1ed5794df5 Merge pull request #29322 from xedin/delay-closure-constraint-generation-is-some-cases
[ConstraintSystem] Delay closure body constraint generation in a coup…
2020-01-21 16:26:33 -08:00
Pavel Yaskevich
2b8c00215f [ConstraintSystem] Delay closure body constraint generation in a couple of specific cases
* If there is a disjunction associated with closure type e.g.
  coercion to some other type `_ = { $0 } as (Int32) -> Void`

* If there is a disjunction associated with a collection which
  could provide more context to the constraint solver.
2020-01-21 12:06:32 -08:00
Doug Gregor
8283a67648 Revert "Revert "Reimplement function builders as statement transformations."" 2020-01-21 10:07:20 -08:00
Doug Gregor
86c13d3c74 Revert "Reimplement function builders as statement transformations." 2020-01-17 15:52:49 -08:00
Doug Gregor
3a51d5b9b2 [WIP] Reimplement function builders as statement transformations. 2020-01-16 13:18:33 -08:00
Pavel Yaskevich
8269522f86 Merge pull request #28837 from xedin/rdar-56340587
[ConstraintSystem] Delay constraint generation for single-statement closure body
2020-01-16 00:29:11 -08:00
Pavel Yaskevich
97d747002c [CSBindings] Avoid inferring duplicate bindings from supertypes
If there is `subtype` relationship between two type variables
binding inference algorithm attempts to infer supertype bindings
transitively, but it doesn't check whether some of the new types
are already included in the set, which leads to duplicate solutions.
2020-01-14 17:31:57 -08:00
Pavel Yaskevich
c6a5f59946 [ConstraintSystem] Attempt types linked via subtyping in reverse order (supertypes first)
If there is a subtype relationship between N types we have to make
sure that type chain is attempted in reverse order because we can't
infer bindings transitively through type variables and attempting
it in any other way would miss potential bindings across the chain.
2020-01-14 00:09:33 -08:00
Pavel Yaskevich
22e12e86a8 [ConstraintSystem] Switch to pair-wise type binding differentiation for ranking
Instead of trying to hold a "global" set of type variable differences
let's use pair-wise comparison instead because in presence of generic
overloads such would be more precise.

If there are N solutions for a single generic overload we currently
relied on "local" comparison to detect the difference, but it's not
always possible to split the system to do one. Which means higher
level comparisons have to account for "local" (per overload choice)
differences as well otherwise ranking would loose precision.
2020-01-14 00:09:33 -08:00
Pavel Yaskevich
cf2cad0cd5 [ConstraintSystem] Add more information retrieval callbacks to getCalleeLocator
`getCalleeLocator` has to be able to:

- Retrieve type for given expression via `getType`;
- Simplify given type via `simplifyType`; and
- Retrieve a selected overload choice for a given locator via `getOverloadFor`.
2020-01-14 00:09:33 -08:00
Pavel Yaskevich
59c749124d [CSBindings] Prefer closure type binding over disjunction
Even if contextual closure type has type variables inside
prefer it over disjunction because it provides a lot of
contextual information early which helps to solve the system
faster.
2020-01-14 00:09:32 -08:00