Commit Graph

200 Commits

Author SHA1 Message Date
Pavel Yaskevich
4b01c8fd11 [ConstraintSystem] Adjust impact of implicit Double <-> CGFloat conversions
- Prefer CGFloat -> Double over the other way around to avoid
  ambiguities;

- Every new conversion impacts the score by factor of number of
  previously applied conversions to make it possible to select
  solutions that require the least such conversions.

- Prefer concrete overloads with Double <-> CGFloat conversion
  over generic ones.
2021-03-17 00:18:14 -07:00
Pavel Yaskevich
73084e774d [CSStep] Make sure disjunction choices with value conversions are not considered "the best"
Augment `DisjunctionChoice::{shouldSkip, shouldStopAt}` to check
for presence of implicit value conversions, and if score indicates
that there is at least one of those was attempted - downgrade
importance of a last successful choice.
2021-03-17 00:18:09 -07:00
Anthony Latsis
8242d843cd NFC, Sema: Further propagate the locator through to doesStorageProduceLValue 2021-03-09 17:01:50 +03:00
Pavel Yaskevich
39b2ff92b3 [CSStep] Skip disabled overloads only if there are no fixes in diagnostic mode 2021-03-04 15:40:39 -08:00
Doug Gregor
9ccf206feb [Concurrency] Tune overloading to to allow sync overloads in async contexts.
The existing overloading rules strongly prefer async functions within
async contexts, and synchronous functions in synchronous contexts.
However, when there are other differences in the
signature, particularly parameters of function type that differ in
async vs. synchronous, the overloading rule would force the use of the
synchronous function even in cases where the synchronous function
would be better. An example:

    func f(_: (Int) -> Int) { }
    func f(_: (Int) async -> Int) async { }

    func g(_ x: Int) -> Int { -x }

    func h() async {
      f(g) // currently selects async f, want to select synchronous f
    }

Effect the semantics change by splitting the "sync/async mismatch"
score in the constraint system into an "async in sync mismatch" score
that is mostly disqualifying (because the call will always fail) and a
less-important score for "sync used in an async context", which also
includes conversion from a synchronous function to an asynchronous
one. This way, only synchronous functions are still considered within
a synchronous context, but we get more natural overloading behavior
within an asynchronous context. The end result is intended to be
equivalent to what one would get with reasync:

  func f(_: (Int) async -> Int) async { ... }

Addresses rdar://74289867.
2021-03-02 23:14:08 -08:00
Holly Borla
a7b5476710 [DisjunctionStep] Only check requirements early to skip disjunction
choices for arithmetic operators for now.
2021-01-14 21:07:39 -08:00
Holly Borla
f75eca9131 [ConstraintSystem] Allow early conformance checking for overload choices
if the best solution has a non-default literal type.
2021-01-14 14:34:25 -08:00
Holly Borla
a3957b987e [ConstraintSystem] Only do the work of partitioning the generic operator
overloads if generic operators are not going to be skipped.
2021-01-14 13:49:57 -08:00
Holly Borla
daec9c9eb7 [ConstraintSystem] Only attempt the refinement overload heuristic
for arithmetic operators.

Only sort overloads that are related, e.g. Sequence
overloads. Further, choose which generic overloads
to attempt first based on whether any known argument types
conform to one of the standard arithmetic protocols.
2021-01-07 14:13:30 -08:00
Holly Borla
e24ac86eaf [ConstraintSystem] Cache applied disjunction constraints in the
constraint system to use later in DisjunctionStep.
2021-01-06 13:36:38 -08:00
Holly Borla
0c01b62078 [ConstraintSystem] Account for duplicate archetype bindings
in IsDeclRefinementOfRequest.
2021-01-06 13:36:38 -08:00
Holly Borla
7783f6bb2b [ConstraintSystem] Rename IsDeclSubstitutableRequest to
IsDeclRefinementOfRequest to better reflect what it computes.
2021-01-06 13:36:38 -08:00
Holly Borla
62305d003f [ConstraintSystem] Use isDeclSubstitutable instead of compareDeclarations
to order generic operator overload choices.
2021-01-06 13:36:38 -08:00
Holly Borla
c62d898169 [ConstraintSystem] Only attempt generic overload pruning heuristics for
operators for now.
2021-01-06 13:36:38 -08:00
Holly Borla
0051c89b31 [ConstraintSystem] Account for scoring in the new generic overload
heuristics.
2021-01-06 13:36:38 -08:00
Holly Borla
dff5401390 [ConstraintSystem] Requestify isDeclSubstitutable from CSStep.cpp 2021-01-06 13:36:38 -08:00
Holly Borla
27b1394430 [ConstraintSystem] Within the generic disjunction choice partition,
attempt the most specific choices first. Then, if the solver finds
a solution with one choice, it can skip any subsequent choices that
can be unconditionally used in place of the successful chioce and produce
the same solution.
2021-01-06 13:36:38 -08:00
Holly Borla
50212a0ea4 [ConstraintSystem] If the solver has already found a solution with a
disjunction choice that does not introduce conversions, check to see
if known argument types satisfy generic operator conformance requirements
early, and skip the overload choice if any requirements fail.

This helps the solver avoid exploring way too much search space when
the right solution involves a generic operator, but the argument types
are known up front, such as `collection + collection + collection`.
2021-01-06 13:36:38 -08:00
Holly Borla
bda36cf30c [ConstraintSystem] Always log disjunction choices that are skipped and
the reason for skipping while solving in debug mode.
2021-01-05 12:47:46 -08:00
Pavel Yaskevich
8e4207e0d8 [ConstraintSystem] NFC: Simplify type var producer/step by referencing constraint system from bindings 2020-12-11 00:30:39 -08:00
Pavel Yaskevich
4e321320cf [CSStep] Don't retain multiple copies of the same bindings just for printing 2020-12-09 13:34:01 -08:00
Luciano Almeida
72b594b3fd [NFC][ConstraintGraph] Address fixme and switch CG::Component dependencies type to SmallVector<unsigned> 2020-11-09 19:49:29 -03:00
Holly Borla
a647f0fb5b Merge pull request #34399 from hborla/optimize-linked-operator-solving
[Constraint System] Implement heuristics for linked operator expressions in the solver proper.
2020-11-08 13:22:40 -08:00
Holly Borla
a8fcdb8580 [ConstraintSystem] Remove an unnecessary SIMD special case in
`DisjunctionStep::shortCircuitDisjunctionAt`.

This code is unnecessary because SIMD overloads are in their own
partition, so the short circuiting will happen automatically.
2020-10-29 19:49:47 -07:00
Holly Borla
b9e08b23fb [ConstraintSystem] Allow the solver to find other solutions after
successfully finding a solution by favoring operators already bound
elsewhere.

Favoring existing operator bindings often lets the solver find a solution
fast, but it's not necessarily the best solution.
2020-10-29 14:33:29 -07:00
Kavon Farvardin
93eb22bdb5 do not short-circuit overload resolution if chosen overload has async/sync mismatch
This fixes SR-13760 / rdar://70543421
2020-10-27 17:53:35 -07:00
Holly Borla
cb64d7f715 [ConstraintSystem] Fix build failures. 2020-10-15 13:30:46 -07:00
gregomni
34fa9c2786 Merge typevars of operators with shared decls after finding a solution to speed up further searching 2020-10-15 09:08:50 -07:00
gregomni
ab6d92b1cd Favor operators based on existing binds via both basename and type 2020-10-15 09:03:45 -07:00
Holly Borla
1e0038c3be [ConstraintSystem] Remove implementation of operator designated types
in the solver.
2020-10-14 16:05:54 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Robert Widmann
2bca013457 Move "isDebugMode" into ConstraintSystem
This eliminates the final source of mutation of the TypeCheckerFlags on the ASTContext.
2020-05-13 09:13:44 -07:00
Hamish Knight
87577d21b3 [CS] NFC: Make failedConstraint private 2020-04-20 19:08:25 -07:00
Hamish Knight
78072de623 [CS] Assert that we don't end up with unsolved constraints
Make sure we don't end up in a situation where we
have unsolved constraints left over and consider
the system fully solved.

This requires tweaking the type matching code for
dependent members such that a concrete base is
considered a failure rather than being left
unsolved. This should only happen when not in
diagnostic mode, as otherwise we use a hole.
2020-04-10 10:16:08 -07:00
Holly Borla
c1c6a884a4 [ConstraintSystem] Respect the constraint solver performance thresholds,
including time and allocated memory, in mergePartialSolutions.
2020-03-09 14:44:12 -07:00
swift-ci
25c6002331 Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-21 16:43:52 -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
Erik Eckstein
1b312a85bd Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-16 10:39:20 +01: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
swift-ci
5fc1d17bf9 Merge remote-tracking branch 'origin/master' into master-next 2019-11-13 09:10:56 -08:00
Robert Widmann
d890b8ad41 Remove some save-and-restores
An awful pattern we use throughout the compiler is to save and restore global flags just for little things.  In this case, it was just to turn on some extra options in AST printing for type variables. The kicker is that the ASTDumper doesn't even respect this flag. Add this as a PrintOption and remove the offending save-and-restores.

This doesn't quite get them all: we appear to have productized this pattern in the REPL.
2019-11-13 07:37:12 -08:00
Robert Widmann
f4d333d066 Sink a bunch of semantic options into TypeCheckerOptions
Sink
- DebugConstraintSolver
- DebugConstraintSolverAttempt
- DebugConstraintSolverOnLines
- DebugGenericSignatures
- DebugForbidTypecheckPrefix
- SolverMemoryThreshold
- SolverBindingThreshold
- SolverShrinkUnsolvedThreshold
- SolverDisableShrink
- EnableOperatorDesignatedTypes
- DisableConstraintSolverPerformanceHacks
- SolverEnableOperatorDesignatedTypes
2019-11-12 22:39:49 -08:00
Ben Langmuir
ec8b255d75 [master-next] Undo temporary change to fix merge conflict
Reverts 0cf3cfabbb
2019-11-05 13:34:12 -08:00
Holly Borla
4fd1377c81 [ConstraintSystem] With the new approach for holes, hole propagation happens
automatically.

This commit also renames `ConstraintSystem::recordHole/isHole` to
`recordPotentialHole` and `isPotentialHole` to make it clear that
we don't know for sure whether a type variable is a hole until it's
bound to unresolved.
2019-11-05 09:15:13 -08:00
Holly Borla
f2fdc8c114 [ConstraintSystem] Only apply the ExplicitlySpecifyGenericArguments fix
for generic parameters that are holes.
2019-11-05 09:15:13 -08:00
Holly Borla
e97314cb5c [ConstraintSystem] Never choose the potential binding for a hole
in the constraint system over a different binding or disjunction.

In other words, we will only choose to bind a hole to `Any` if there
are no other bindings and no disjunctions.
2019-11-05 09:15:13 -08:00
Holly Borla
3bc2269f4f [ConstraintSystem] Allow generic parameters and holes to default to
`Any` in `getPotentialBindings` rather than `ComponentStep::take`.
2019-11-05 09:15:13 -08:00
Holly Borla
561e527848 [ConstraintSystem] Extend the ExplicitlySpecifyGenericArguments fix to cover
all cases of missing generic parameters.

In `ComponentStep::take` when there are no bindings or disjunctions, use hole
propagation to default remaining free type variables that aren't for generic
parameters and continue solving. Rather than using a defaultable constraint for
holes, assign a fixed type directly when we have no bindings to try.
2019-11-05 09:15:13 -08:00
Doug Gregor
17ea39accd [Constraint solver] Simplify one-way constraints to Equal, not Bind.
One-way constraint expressions, which are the only things that
introduce one-way constraints at this point, want to look through
lvalue types to produce values. Rename OneWayBind to OneWayEqual, map
it down to an Equal constraint when it is simplified (to drop
lvalue-ness), and apply that coercion during constraint application.

Part of rdar://problem/50150793.
2019-08-16 14:13:21 -07:00
Doug Gregor
da267bf7ca [Constraint system] Switch TypeVariables to a SetVector.
There were a few places where we wanted fast testing to see whether a
particular type variable is currently of interest. Instead of building
local hash tables in those places, keep type variables in a SetVector
for efficient testing.
2019-08-16 14:13:15 -07:00