Commit Graph

830 Commits

Author SHA1 Message Date
Michael Gottesman
c62f31f5dc Inject llvm::SmallBitVector into namespace swift;
I also eliminated all llvm:: before SmallBitVector in the code base.
2018-09-21 09:49:25 -07:00
Mark Lacey
4fa2f15e4f [ConstraintSystem] Remove Component, which is no longer used. 2018-09-20 17:23:46 -07:00
Mark Lacey
c6c66f6873 [ConstraintSystem] Change the order in which we visit disjunctions.
Attempt to visit disjunctions that are associated with applies where
we have at least some useful information about the types of all of the
arguments before visiting other disjunctions.

Two tests here got faster, and one slightly slower. One of the
faster tests is actually moving from test/ to the slow/ directory in
validation-test because despite going from 16s to less than 1s, it was
still borderline for what we consider the slow threshold, so I made
the test more complex. The one that got a little slower is
rdar22022980, which I also made more complex so that it is clearly
"slow" by the way we are testing it.

slower:
  rdar22022980.swift

faster:
  rdar33688063.swift
  expression_too_complex_4.swift
2018-09-20 13:15:59 -07:00
Pavel Yaskevich
f9452afa4f [CSStep] NFC: Unify type binding attempt logging
Instead of printing `trying` for type variable and `assuming`
for disjunction choices, unify it so `BindingStep` logs
`attempting {type variable, disjunction choice}`.
2018-09-19 18:37:36 -07:00
Pavel Yaskevich
82d07ad88d [ConstraintSystem] NFC: TypeBinding abstraction is no longer necessary 2018-09-17 15:28:37 -07:00
Pavel Yaskevich
de34d9fa6a [CSStep] Extract common type-var/disjunction functionality into BindingStep
Unify `take` implementation and API for type variable
and disjunction via `BindingStep` which accepts a producer.
2018-09-17 15:16:04 -07:00
Pavel Yaskevich
f1a53c3f59 [ConstraintSystem] Remove unnecessary forward declarations of the steps
Also move `DisjunctionStep` from being a friend of `ConstraintSystem`
because it's not strictly necessary.
2018-09-15 20:56:47 -07:00
Pavel Yaskevich
2eb0e824e4 [ConstraintSystem] Remove recursive solver algorithm and related logic
`solveRec` is now gone as well as all related logic,
such as attempting type variable and disjunction choices recursively.
2018-09-15 20:56:47 -07:00
Pavel Yaskevich
baceb68f85 [ConstraintSystem] Rename solveIteratively to solve and start using it
Remove solution filtering from new iterative `solve` method, and
replace all usages of `solveRec` with it.
2018-09-15 20:56:47 -07:00
Pavel Yaskevich
0d3c804c18 [DisjunctionStep] Start remembering choices when requested
Extract choice attempting logic into `DisjunctionStep::attemptChoice`
and start recording taken choices in constraint system when
requested by disjunction.
2018-09-15 20:56:47 -07:00
Pavel Yaskevich
c9bf47cb39 [CSStep] Add Disjunction step
`DisjunctionStep` attempts all of the viable choices,
at least one of the choices has to succeed for disjunction
to be considered a success.

`DisjunctionStep` attempts each choice and generates followup
`SplitterStep` to see if applying choice to constraint system
resulted in any simplification.
2018-09-15 20:56:46 -07:00
Pavel Yaskevich
60581da15e [ConstraintSystem] Add Splitter, Component and binding steps
* `SplitterStep` is responsible for running connected components
  algorithm to determine how many independent sub-systems there are.
  Once that's done it would create one `ComponentStep` per such
  sub-system, and move to try to solve each and then merge partial
  solutions produced by components into complete solution(s).

* `ComponentStep` represents a set of type variables and related
  constraints which could be solved independently. It's further
  simplified into "binding" steps which attempt type variable and
  disjunction choices.

* "Binding" steps such as `TypeVariableStep` and `DisjunctionStep`
  are responsible for trying type binding choices in attempt to
  simplify the system and produce a solution. After attempting each
  choice they introduce a new "split" step to compute more work.
2018-09-15 20:56:46 -07:00
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