Commit Graph

998 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
141c987bb9 [ConstraintSystem] Add accessor to type var to determine if it represents a closure type 2020-01-14 00:09:32 -08:00
Pavel Yaskevich
9257b29465 [ConstraintSystem] Clarify DefaultClosureType constraint and add its own simplify method 2020-01-14 00:09:32 -08:00
Pavel Yaskevich
9dc2dfde98 [ConstraintSystem] Allow single-statement closures be used with function builders
While resolving closure use contextual locator information to
determine whether given contextual type comes from a "function builder"
parameter and if so, use special `applyFunctionBuilder` to open
closure body instead of regular constraint generation.
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
66db166922 [ConstraintSystem] Introduce new DefaultClosureType constraint
This constraint connects type variable representing a closure
expression to its inferred type and behaves just like regular
`Defaultable` constraint expect for type inference where it's
used only if there are no contextual bindings available.
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
b7523d745c [ConstraintSystem] Introduce resolveClosure method to generate constraints for closure body
In preparation to change the way closures are handled in constraint system
let's introduce a special method which is responsible for generation of
constraints for closure body and connecting it to the rest of the
constraint system when contextual type becomes available.
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
92556e8f0d [ConstraintSystem] Introduce a mapping from closures to their inferred types
In preparation to change type-checking behavior of closures
we need to introduce a special mapping from closure expressions
to their inferred types (based on parameters/result and body).
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
465a948e67 [ConstraintSystem] Add a separate constraint generation for closure body
Separate closure from top-level constraint generation because
its body shouldn't be considered when it comes to parent/weight
computation, otherwise top expression in body is going to get
disconnected from the closure.
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
8f68a68c70 [ConstraintSystem] Augment getCalleeLocator with getType callback
This is necessary for ambiguity diagnostics because none of the
solutions are applied back to the constraint system which means
that `getCalleeLocator` needs an ability to query information
from different sources e.g. constraint system and/or solution.
2020-01-14 00:09:32 -08:00
Doug Gregor
5b320992ea [Constraint solver] Use a constraint system to apply all function builders.
When type checking the body of a function declaration that has a function
builder on it (e.g., `@ViewBuilder var body: some View { ... }`), create a
constraint system that is responsible for constraint generation and
application, sending function declarations through the same code paths
used by closures.
2020-01-09 14:38:45 -08:00
Doug Gregor
ac24491dae [Constraint system] Generalize function builder application APIs.
Teach the constraint system to handle matching a function builder to a
function as well as a closure.
2020-01-08 16:19:44 -08:00
Doug Gregor
fffe1291fa [Constraint system] Generalize record of transformed function builders. 2020-01-06 11:39:11 -08:00
Doug Gregor
dfda7aca7f [Constraint solver] Generalize "target" of solution application.
Chip away at the expression-centric nature of the constraint system by
generalizing the "target" of solution application from a single
expression to a a "SolutionApplicationTarget", which can be an
expression or a function body. The latter will be used for applying
function builders to an entire function body.
2020-01-06 11:31:06 -08:00
Doug Gregor
9be16b2185 [Type checker] Move conformance resolution during application into Solution.
While this doesn't completely use the solution's set of known conformances,
it moves the logic for handling the lookup into the right place.
2020-01-02 13:06:09 -08:00
Doug Gregor
bbcaf8c669 [Type checker] Introduce value witness constraints.
Introduce a new kind of constraint, the "value witness" constraint,
which captures a reference to a witness for a specific protocol
conformance. It otherwise acts like a more restricted form of a "value
member" constraint, where the specific member is known (as a
ValueDecl*) in advance.

The constraint is effectively dependent on the protocol
conformance itself; if that conformance fails, mark the type variables
in the resolved member type as "holes", so that the conformance
failure does not cascade.

Note that the resolved overload for this constraint always refers to
the requirement, rather than the witness, so we will end up recording
witness-method references in the AST rather than concrete references,
and leave it up to the optimizers to perform devirtualization. This is
demonstrated by the SIL changes needed in tests, and is part of the
wider resilience issue with conformances described by
rdar://problem/22708391.
2020-01-02 12:06:23 -08:00
Slava Pestov
af07642c8f Sema: Remove TypeCheckExprFlags::SkipMultiStmtClosures
Fold this into TypeCheckExprFlags::SubExpressionDiagnostics, which
is itself going away soon, hopefully!
2019-12-19 23:51:53 -05:00
Pavel Yaskevich
8bcc192591 [Diagnostics] Diagnose inability to infer (complex) closure return type 2019-12-19 12:16:30 -08:00
Robert Widmann
1534b3659d [NFC] Const-qualify SelectedOverload's members 2019-12-16 11:28:54 -08:00
Hamish Knight
f1e690c07e Merge pull request #28775 from hamishknight/dynamic-lookup-attr-req
Requestify hasDynamicMemberLookupAttribute for Sema too
2019-12-13 14:40:13 -08:00
Pavel Yaskevich
5ba2e59c63 Merge pull request #28752 from xedin/simplify-potential-binding
[ConstraintSystem] Record originator constraint for each type variable binding
2019-12-13 11:50:02 -08:00
Hamish Knight
36cecb4015 Requestify hasDynamicMemberLookupAttribute for Sema too
Previously we had a request for this in
IDETypeChecking, but this wasn't used for queries
made from Sema. Move the request into Sema, and
move `hasDynamicMemberLookupAttribute` onto
TypeBase.
2019-12-13 11:04:00 -08:00
Pavel Yaskevich
c4b74a3984 [ConstraintSystem] Remove getSourceKind() from PotentialBinding
There was a single place where it was used to determine whether
a binding comes from an argument conversion constraint. Since
constraint locator is part of the binding now it's possible to
use it instead and remove unused accessor.
2019-12-12 16:48:40 -08:00
Pavel Yaskevich
bb51a8f97d [ConstraintSystem] Use originating constraint as a source for a binding
Unify all of the fields of `PotentialBinding` which have to do with
location information into a single field with is either a constraint
(for regular bindings) or constraint locator (for "holes").

This helps to reduce the amount of space used by `PotentialBinding`
 as well as propagate more contextual information when binding gets
 attempted.
2019-12-12 12:41:44 -08:00
Brent Royal-Gordon
7543a89cc4 Merge pull request #27683 from brentdax/i-understood-that-reference
[NFC] Distinguish references to names from declarations of those names by type
2019-12-11 10:50:57 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Hamish Knight
58884002d8 [CS] Don't adjust opened types of @dynamicMemberLookup overloads
Rather than adjusting the opened types for choices
referring to `subscript(dynamicMember:)` down to
the result type, only use the result type to
bind the overload type variable. This avoids the
need for a couple of special cases in CSApply.

To facilitate this change, add
`ConstraintSystem::bindOverload`, which deals with
adding the necessary constraints to bind the
overload's type variable. The eventual goal here
is to remove `adjustTypeOfOverloadReference`, with
adjustments to the opened type being moved to
`getTypeOfMemberReference`, and the adding of
constraints being moved to `bindOverloadType`.
2019-12-10 18:02:14 -08:00
Hamish Knight
73098687b0 [CS] Pass SelectedOverload in a couple of places (#28649)
[CS] Pass SelectedOverload in a couple of places
2019-12-10 11:13:46 -08:00
Hamish Knight
5d7fb487eb [CS] Remove a couple of dead types 2019-12-09 15:27:57 -08:00
Holly Borla
1e013a02cf [ConstraintSystem] Move FailureDiagnostic::getFunctionArgApplyInfo
into `ConstraintSystem`.
2019-12-06 15:43:39 -08:00
Hamish Knight
a97328dcbf [CS] Use a MapVector to cache resolved overloads
Rather than maintaining a linked list of overload
choices, which must be linearly searched each time
we need to lookup an overload at a given callee
locator, use a MapVector which can be rolled back
at the end of a scope.

Remove ResolvedOverloadSetListItem in favor of
using SelectedOverload, which avoids the need to
convert between them when moving from
ConstraintSystem to Solution.
2019-12-05 14:47:52 -08:00
Pavel Yaskevich
ebdb5c7776 [ConstraintSystem] Improve handling of ambiguities related to fixes
Solutions passed to `diagnoseAmbiguityWithFixes` aren't filtered
so we need to remove all of the solutions with the score worse
than overall "best". Also logic has to account for some fixes being
"warnings".
2019-12-03 12:07:16 -08:00
Hamish Knight
a014f15e75 [CS] Enforce that withPathElement is called on an lvalue
Locator builders keep a pointer to their underlying
locator, so it's not generally sound to extend an
rvalue locator builder.

This commit enforces that withPathElement is called
on an lvalue, and adds a couple more overloads of
getConstraintLocator to make it more convenient to
extend locators with multiple elements.
2019-11-27 17:13:50 -08:00
Pavel Yaskevich
f7ee402d2f Merge pull request #28400 from xedin/rdar-57201781
[ConstraintSystem] Introduce notion of constraint system phase
2019-11-21 13:17:34 -08:00
Pavel Yaskevich
e3a4b4ffc9 [ConstraintSystem] Fix ConstraintGenerator to change phase to "constraint generation" and back 2019-11-21 00:08:58 -08:00
Pavel Yaskevich
addc2b06ab [ConstraintSystem] Introduce notion of constraint system phase
Some constraint transformations require knowledge about what state
constraint system is currently in e.g. `constraint generation`,
`solving` or `diagnostics` to make a decision whether simplication
is possible. Notable example is `keypath dynamic member lookup`
which requires a presence of `applicable fn` constraint to retrieve
some contextual information.

Currently presence or absence of solver state is used to determine
whether constraint system is in `constraint generation` or `solving`
phase, but it's incorrect in case of `diagnoseFailureForExpr` which
tries to simplify leftover "active" constraints before it can attempt
type-check based diagnostics.

To make this more robust let's introduce (maybe temporarily until
type-check based diagnostics are completely obsoleted) a proper
notion of "phase" to constraint system so it is always clear what
transitions are allowed and what state constraint system is
currently in.

Resolves: rdar://problem/57201781
2019-11-20 18:34:51 -08:00