Commit Graph

718 Commits

Author SHA1 Message Date
Pavel Yaskevich
daf4c2f3b5 [CSSolver] Add skeleton of iterative solve
The idea so to split solving into non-recursive steps,
represented by `SolverStep`, each of the steps is resposible
for a unit of work e.g. attempting type variable or
disjunction bindings/choices.

Each step could produce more work via "follow-up" steps,
complete "partial" solution when it's done, or error which
terminates solver loop.
2018-09-15 20:56:46 -07:00
Slava Pestov
83cc9755ed Sema: Refactor matchFunctionTypes() to walk parameter lists directly
This lets us remove a variety of ParenType-preserving hacks.
2018-09-10 12:30:47 -07:00
Pavel Yaskevich
e4252a5fc7 [ConstraintSystem] Add flags to TypeBinding 2018-09-05 12:32:40 -07:00
Pavel Yaskevich
2acfff0deb [ConstraintSystem] Unify type var and disjunction choice producers
Introduce a base `BindingProducer` type which can produce bindings
for both type variables and disjunctions.
2018-09-05 00:12:55 -07:00
Pavel Yaskevich
edfabc6715 [ConstraintSystem] NFC: Refactor solveForDisjunction to use TypeBinding
* Extract logic for attempting individual choices into `solveForDisjunctionChoice`
* Remove all of the unnecessary information from `DisjunctionChoice`
* Convert auxiliary logic to operate on `TypeBinding` instead of `DisjunctionChoice`
2018-09-04 22:27:54 -07:00
Pavel Yaskevich
a9bb68cea7 [ConstraintSystem] Extend TypeBinding to look more like a base class
Add all of the important information to the `TypeBinding` which covers
both type variable and disjunction choices.
2018-09-04 22:27:46 -07:00
Pavel Yaskevich
e5b63fa16c [ConstraintSystem] NFC: Unify type variable and disjunction choice representation
Introduce `TypeBinding` interface with a single `attempt` method
which is useful to encapsulate specific binding logic used for
attempting disjunction choices and type variable bindings under
a single interface. This makes it easier to unify top-level logic
responsible for binding enumeration.

It should also make it possible to introduce unified binding producer
interface in the future.
2018-09-03 23:27:41 -07:00
Pavel Yaskevich
d560b36c5c [ConstraintSystem] NFC: Move allowFreeTypeVariableBindings into SolverState
Currently `allowFreeTypeVariableBindings` flag has to be passed all
the way down from top-level `solve` call to `finalize` that forms
(partial and complete) solutions. Instead of doing that, let's just
make it a part of the solver state, which is already present
throughout whole solver run.
2018-08-31 19:51:11 -07:00
Pavel Yaskevich
f5aaa15df7 [CSSolver] NFC: Refactor tryTypeVariableBindings to use generator
Encapsulate most of the logic related to new binding generation
and iteration into `TypeVarBindingGenerator`. `tryTypeVariableBindings`
is only responsible for attempting bindings and recording results.

One more step towards iterative solver.
2018-08-31 00:53:03 -07:00
Pavel Yaskevich
2ae8e8d0cb [CSBindings] Cleanup tryTypeVariableBindings
* Move logic to ensure that r-value type var would get r-value type to `PotentialBindings`;
* Strip uncessary parens directly when creating `PotentialBinding`;
* Check if the binding is viable before inclusion in the set, instead of filtering it later;
* Assert that bindings don't have types with errors included in the set.
2018-08-30 00:08:26 -07:00
Slava Pestov
542ed54c79 Merge pull request #19042 from slavapestov/match-call-arguments
Refactor matchCallArguments() to take arrays of AnyFunctionType::Param instead of argument tuples
2018-08-29 17:16:05 -07:00
Pavel Yaskevich
db69b3e486 Merge pull request #19055 from xedin/disjunction-as-generator
[ConstraintSystem] NFC: Refactor `solveForDisjunctionChoices` to use iterator
2018-08-29 16:34:53 -07:00
Pavel Yaskevich
0dbd12922f Merge pull request #18995 from xedin/add-assignment-diagnostic
[Diagnostics] NFC: Refactor assignment diagnostics into `AssignmentFailure`
2018-08-29 16:34:26 -07:00
Pavel Yaskevich
012315fcaf [ConstraintSystem] NFC: Refactor solveForDisjunctionChoices to use iterator
Instead of passing a set of choices, locator and other flags to
`solveForDisjunctionChoices` directly, let's wrap all that information
into "disjunction" iterator which returns `DisjunctionChoice`s
directly.
2018-08-29 13:34:24 -07:00
Pavel Yaskevich
3be5c203e3 [ConstraintSystem] NFC: Move isExplicitConversion to Constraint
Since this logic is tightly coupled to constraint, it makes sense
to move just there, also it's easier to re-use it elsewhere since
it doesn't have to be `private` anymore.
2018-08-29 00:01:36 -07:00
Slava Pestov
83c32da93c Sema: Refactor constraints::matchCallArguments() to take parameters and not input tuples 2018-08-28 22:36:02 -07:00
Slava Pestov
383e93b428 Sema: Call matchCallArguments() directly instead of matchTypes()
Now that function types cannot have a naked type variable as
their input type it's no longer possible to have an unsolved
ArgumentTupleConversion constraint, so we can bypass most of
the logic in matchTypes() and call matchCallArguments() instead.
2018-08-28 14:40:56 -07:00
Slava Pestov
a002aae495 Sema: Remove matchFunctionParamTypes()
Now that function types cannot have a naked type variable as
their input type we should never end up down this code path
with an associated declaration and argument labels, so it's
OK to just call matchTypes() on the input types instead.
2018-08-28 14:40:56 -07:00
Slava Pestov
65edce7591 Sema: Add FunctionInput and FunctionResult constraints
These are temporary.

FunctionInput is conditional on fixing some ranking behavior to not
depend on type variables having argument tuples bound to them.
Hopefully we can replace TVO_PreferSubtypeBinding with a better
mechanism that compares overload types instead.

FunctionResult is used in CSDiag's contextual type diagnostics.
This is also on the chopping block.
2018-08-28 14:37:57 -07:00
Pavel Yaskevich
bba7112506 [Diagnostics] NFC: Refactor assignment diagnostics into AssignmentFailure
Merge logic from `diagnoseAssignmentFailure` and `diagnoseSubElementFailure`
into new `AssignmentFailure`, together with their support functions, which
decouples `CSDiagnostics` from `CSDiag` and scrubs latter from some functionality.
2018-08-27 00:35:44 -07:00
Greg Titus
c95cfc6f69 Merge pull request #18950 from gregomni/rvalue-as-lvalue
[Diagnostics] Adding assignments directly to CS and diagnosing from there.
2018-08-26 11:12:57 -07:00
Pavel Yaskevich
ea62075766 [ConstraintSystem] NFC: Move diagnoseAmbiguity methods from CSDiag to ConstraintSystem` 2018-08-24 23:18:49 -07:00
gregomni
821f63fe98 Make assignments and assignment failure diagnoses directly in the CS.
More specific diagnoses for assigning to read-only keypaths.
'computeAssignDestType' is dead code now.
ConstraintFix shouldRecordFix()
2018-08-24 20:39:03 -07:00
Pavel Yaskevich
16fc3117cc [ConstraintSystem] Diagnose ambiguities related to solutions with fixes
If all of the solutions in the set have a single fix, which points
to the common anchor, attempt to diagnose the failure as an
ambiguity with a list of candidates and their related problems as notes.

Having richer message like that helps to understand why something is
ambiguous e.g. if there are two overloads, one requires conformance
to some protocol and another has a same-type requirement on some type,
but neither matched exactly, having both candidates in the diagnostic
message with associated errors, instead of simplify pointing to related
declarations, helps tremendously.
2018-08-24 11:20:49 -07:00
Pavel Yaskevich
cc436e313a [Diagnostics] Decouple FailureDiagnostic from Solution 2018-08-21 23:45:17 -07:00
gregomni
4d862d50cb In tryTypeVariableBindings, if T? fails, try T for all binding kinds (previously just allowed for Subtypes). This allows us to always find MissingOptionalUnwrapFailures, so that all the unwrap fixit code can be moved into CSDiagnostics and made static. 2018-08-14 09:14:59 -07:00
Greg Titus
8f41ee7750 Merge pull request #18324 from gregomni/opty2
[Sema] More unwrap fixits
2018-08-14 09:12:35 -07:00
Pavel Yaskevich
ebb9f86bee [ConstraintSystem] NFC: Remove obsolete Fix struct
Also clean up `ConstraintSystem` interface from the storage
supporing `Fix` such as missing types, decl names, conformances etc.
2018-08-13 18:10:40 -07:00
Pavel Yaskevich
e631a37ef6 [ConstraintSystem] Replace Fix with ConstraintFix throughout solver 2018-08-13 18:10:27 -07:00
gregomni
36cf24fb9e When we diagnose an unwrap, if the expression being unwrapped is a sole reference to a local let/var, and the local's type is inferred, offer fixits on the initializer as well. In this position we can also reasonably offer 'guard let'. Since we're identifying the implicitly typed initializer now, we can give a specific explanation of how IUOs get turned into optional types with the new IUO implementation.
Pass constraint system down into offering force unwrap fixits so that we can identify the type of the last member chosen for an optional chain. If there's a chain and the last member's return type isn't optional, then it's cleaner to offer to change that last '?' into a '!' to perform the force, rather than parenthesize the whole expression and force the result.
2018-08-12 12:57:20 -07:00
Pavel Yaskevich
a564042fdc [Diagnostics] Port missing explicit conversion to new abstraction 2018-08-09 17:01:54 -07:00
Pavel Yaskevich
0bd682ef82 Merge pull request #18512 from xedin/refactor-fix-diagnostics-into-class
[ConstraintSystem] Add new FailureDiagnostic abstraction
2018-08-06 18:35:22 -07:00
Mark Lacey
6ccc2acb06 [ConstraintSystem] Remove disjunction number as we're not currently
using it.

It was added to use for selecting the order we visit disjunctions, but
this order turned out to not be great (and I don't think there are
simple variations on the creation order, like reverse order), that
will work well either.
2018-08-06 16:00:26 -07:00
Pavel Yaskevich
776485878f [ConstraintSystem] NFC: Move new diagnostics into separate files - Diagnostics.(h|cpp) 2018-08-06 13:08:47 -07:00
Pavel Yaskevich
bda60b4ea2 [Diagnostics] NFC: Reword FailureDiagnostic::diagnose comment 2018-08-04 17:10:16 -07:00
Pavel Yaskevich
dee221c199 [Diagnostics] Add emitDiagnostic method to FailureDiagnostic
Instead of having to obtain type-checker instance through
constraint system, let's have `emitDiagnostic` do that and
forward all of the argument for easy access.
2018-08-04 17:01:46 -07:00
Pavel Yaskevich
d201e552e5 [Diagnostics] Move re-labeling diagnostic to use new abstraction 2018-08-04 16:52:45 -07:00
Pavel Yaskevich
5750b07daa [ConstraintSystem] Add new FailureDiagnostic abstraction
Aim of this new diagnostic abstraction is to encapsulate each
individual failure into its own class with some shared base.
Good example of this - diagnostics related to generic requirement
failures, where each kind (conformance, same-type etc.) has
some specific logic e.g. diagnostic message but the all share
the same traits like affected declaration/requirement.
2018-08-04 11:25:54 -07:00
Pavel Yaskevich
0f9f4e99ce [ConstraintSystem] NFC: Move getOverloadChoice{IfAvailable} to Solution
Since information comes from `Solution` anyway, it's good to have
that logic localized, but it's also useful for diagnostics based on
fixes attached to solutions.
2018-08-03 20:50:54 -07:00
Pavel Yaskevich
f15e17a629 [Sema] NFC: reword "only concrete types can conform to protocols" diagnostic 2018-08-03 14:27:00 -07:00
Pavel Yaskevich
cfc5b97dc2 [ConstraintSystem] Move score kind for Fix before Unavailable
Any solution which doesn't require source changes should be considered
better than any other solution which does.

Otherwise solutions with fixes are always considered better than
solutions with unavailable overloads, which isn't correct especially
considering that different types of errors are now diagnosed via
forming solutions with fixes.
2018-08-02 16:40:58 -07:00
Pavel Yaskevich
29e34e66e7 [ConstraintSystem] Diagnose missing conformance requirements via "fixes"
If fixes are allowed let solver record missing protocol conformance
requirements and assume that `conformsTo` constraint is successfully
solved, this helps to diagnose such errors without involving
heavy-weight expression based diagnostics.

Resolves: rdar://problem/40537858
2018-08-02 16:40:34 -07:00
Mark Lacey
94f14d4a46 Merge pull request #18386 from rudkx/simplify-state-for-disjunction-choice
[ConstraintSystem] Split the main loop attempting disjunction choices…
2018-07-31 09:55:48 -07:00
Mark Lacey
e84b372586 [ConstraintSystem] Split the main loop attempting disjunction choices out.
Make it a separate function that takes an ArrayRef<Constraint *> so
that we can call it with different collections of constraints.
2018-07-30 23:15:41 -07:00
swift-ci
e472e84691 Merge pull request #18371 from DougGregor/constraint-solver-expr-too-complex-fix 2018-07-30 22:33:45 -07:00
Mark Lacey
128a7ffa5d Merge pull request #18359 from rudkx/simplify-state-for-disjunction-choice
[ConstraintSystem] Hold slightly less state in DisjunctionChoice.
2018-07-30 18:31:20 -07:00
Doug Gregor
50eec8cb61 [Constraint solver] Check "expression too complex" more consistently.
Also, cache the result; once we've hit a too-complex expression, we
always have a too-complex expression in that constraint system.
2018-07-30 16:43:18 -07:00
Mark Lacey
7abf4b6206 [ConstraintSystem] Hold slightly less state in DisjunctionChoice.
We only care about whether the disjunction was created for an explicit
conversion.
2018-07-30 13:29:25 -07:00
Pavel Yaskevich
2ddeef7bcf [ConstraintSystem] Remove ExprCleaner and its uses 2018-07-28 20:28:41 -07:00
Pavel Yaskevich
f9c6ed54ed [Sema] NFC: Refactor getUnopenedTypeOfReference to accept callback for type 2018-07-28 20:28:41 -07:00