Commit Graph

663 Commits

Author SHA1 Message Date
Pavel Yaskevich
afec25271e [ConstraintSystem] Extract PotentialBinding and its auxiliary classes into a separate header
Create a new namespace - `swift::constraints::inference` and associate
`PotentialBinding` with it. This way it would be possible for constraint
graph to operate on `PotentialBinding(s)` in the future.
2021-01-15 15:03:24 -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
Pavel Yaskevich
7617b7418e [CSBindings] Re-introduce joined bindings instead of updating in place
Make `PotentialBinding` immutable and re-introduce products of
type join instead.
2021-01-12 11:09:14 -08:00
Pavel Yaskevich
03b6b41774 [CSBindings] NFC: Don't pass constraint system as an argument to infer* methods
`PotentialBindings` already reference the constraint system they
belong to, so there is no need to pass it as an argument to inference
methods.
2021-01-12 00:53:46 -08:00
Pavel Yaskevich
aa887fd66a [ConstraintSystem] Turn binding storage into a SetVector
Currently potential bindings are stored in a vector (`SmallVector`)
and every call has to pass additional set of unique types to
inference methods to unqiue the bindings. Instead let's merge
these two together and use `SetVector` for binding storage,
which would also be great for incremental mode that can't
pass additional sets around.
2021-01-12 00:52:55 -08:00
Pavel Yaskevich
db4f790c5d [ConstraintSystem] Make it possible to use PotentialBinding in set/map 2021-01-11 12:53:26 -08:00
Pavel Yaskevich
2a6f279b45 [ConstraintSystem] NFC: Remove obsolete used/checked conformances cache 2021-01-08 12:55:49 -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
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
Pavel Yaskevich
8356ab430b [ConstraintSystem] NFC: Avoid use of designated initializers to make Windows happy 2021-01-05 13:25:57 -08:00
Pavel Yaskevich
b79b875a3d [ConstraintSystem] Make it possible to reference PotentialBinding(s) in unit tests 2021-01-05 11:51:33 -08:00
Pavel Yaskevich
2393a7c9a4 [CSBindings] Represent literal requirements as a struct instead of a tuple
Doing so streamlines access to the information associated with literal
protocol requirements and allows to add more helpers.

Also cache default type in the struct itself for easy access.
2021-01-04 10:32:08 -08:00
Pavel Yaskevich
e676e05f13 [CSBindings] Transform literal requirements into bindings when type variable is attempted 2021-01-04 09:20:14 -08:00
Pavel Yaskevich
731810e184 [CSBindings] Check literal coverage as new bindings/requirements are discovered
One more step towards incrementality of binding inference. Instead of
trying to determine literal protocol coverage during finalization
of the bindings, let's do that as soon as new bindings and/or literal
protocol requirements are discovered.
2021-01-04 09:20:14 -08:00
Pavel Yaskevich
41623abe49 [CSBindings] Compute literal information on demand 2021-01-04 09:20:14 -08:00
Pavel Yaskevich
650c54b822 [CSBindings] Split literals from other protocol requirements
Literal protocol requirements are handled differently from other
protocols, they require additional contextual information (such
as coverage, direct/transitive distinction), and participate in
binding inference (could be turned into default bindings).
2021-01-04 09:20:14 -08:00
Pavel Yaskevich
e301b19b83 [ConstraintSystem] Avoid using a separate set to compute viable defaults 2020-12-15 13:30:52 -08:00
Pavel Yaskevich
9649b76042 [CSBindings] Don't generate bindings for defaults
Let's keep defaults separate from direct and transitive bindings,
that would make it easier to handle them in incremental model.

Instead of generating bindings for defaults and adding to the main
set, let's allow producer to choose what to do with them once type
variable has been picked for attempting.
2020-12-15 13:17:41 -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
22b34e8f36 [CSBindings] Let producer record a binding for a hole
As a step towards making binding inference more incremental, let's
make producer responsible for adding hole type binding instead of
doing so in `finalize`.
2020-12-10 15:39:38 -08:00
Pavel Yaskevich
50cc0f040f [ConstraintSystem] NFC: Extract requiresOptionalAdjustment so it could be used for default bindings 2020-12-09 13:34:30 -08:00
Pavel Yaskevich
ef673c74f1 [ConstraintSystem] Handle binding nullability in producer instead of collector
Wrapping bindings into optional type based on presence of
an `ExpressibleByNilLiteral` conformance requirement should
be done after type variable has been selected for attempting.
Otherwise such upfront work would be wasteful since it doesn't
affect binding ranking in any way.
2020-12-09 13:34:01 -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
Pavel Yaskevich
2619e2d8ac [ConstraintSystem] Track adjacent type variables while inferring bindings
Replaces `InvolvesTypeVariables` flag with a set of adjacent type
variables found during binding inference.

This approach is more suitable for incremental binding computation
because it allows to maintain a list of type variables that affect
ranking and check whether something has been resolved without having
to re-evaluate constraints associated with the given type variable.
2020-12-08 13:22:30 -08:00
Pavel Yaskevich
88efaacd79 Merge pull request #34988 from xedin/on-demand-fully-bound
[CSBindings] Instead of using a flag track constraints which delay type variable
2020-12-08 10:03:18 -08:00
Pavel Yaskevich
c1d178a9e0 [ConstraintSystem] NFC: Rename isFullyBound -> isDelayed and clarify associated comment 2020-12-08 00:54:09 -08:00
Pavel Yaskevich
54406b49de [ConstraintSystem] Track constraints that caused bindings to be "fully bound"
- Keep track of all constraints which caused a particular type variable to
  be de-prioritized.

- Convert a property to a method which would determine whether
  given set of potential bindings is "fully bound" and has to
  be delayed until certain constraints are simplified.
2020-12-04 21:50:04 -08:00
Frederick Kellison-Linn
a53b1e4f3f [Sema] Always look through optionals for unresolved member lookup 2020-12-04 12:11:10 -05:00
Pavel Yaskevich
96415b237e [CSBinding] Attempt to join any existing and viable bindings with new binding
Instead of attempting to join only the last recorded supertype
binding, let's attempt to join any previously recorded supertype
binding with an incoming one to make sure that set contains only
the most viable types.
2020-11-19 18:06:35 -08:00
Nathan Hawes
1eb05b7760 Merge pull request #34733 from nathawes/migrate-unresolved-completion
[CodeCompletion][Sema][Parse] Migrate unresolved member completion to the solver-based completion implementation
2020-11-19 01:26:43 -08:00
Nathan Hawes
86ddd52204 [Sema] Extract out expression-contains-completion-loc check into a method on ConstraintSystem (NFC) 2020-11-19 09:32:38 +11:00
Pavel Yaskevich
094ca8077d Merge pull request #34737 from xedin/potential-bindings-refactor
[ConstraintSystem] Infer some of the bindings attributes on demand
2020-11-16 13:28:21 -08:00
Pavel Yaskevich
afa11989c9 [ConstraintSystem] Determine whether bindings are potentially incomplete on demand 2020-11-13 13:45:58 -08:00
Pavel Yaskevich
6a7cc79f83 [ConstraintSystem] Determine whether type variable is a hole on demand 2020-11-11 15:31:24 -08:00
Pavel Yaskevich
4ed9794547 [ConstraintSystem] Compute whether bindings are bound only by existentials on demand 2020-11-11 14:19:49 -08:00
Pavel Yaskevich
7f228b0e31 [ConstraintSystem] Compute # of defaultable bindings on demand 2020-11-11 12:35:53 -08:00
Slava Pestov
0ff82ca051 Sema: Check conformance availability when ranking solutions
This completes the work on <rdar://problem/35158274>, and also
adds a test case for <rdar://problem/50627401>.
2020-11-10 17:52:51 -05: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
Doug Gregor
33cfbbdd72 [Concurrency] Don't diagnose missing "self." in async let autoclosures. 2020-11-04 23:18:51 -08: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
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00
swift-ci
b654008443 Merge pull request #34337 from nathawes/allow-missing-args-when-type-checking-for-completion 2020-10-19 15:26:50 -07:00
Pavel Yaskevich
59e8043cb9 Merge pull request #34278 from xedin/transitive-protocol-inference
[CSBindings] Implement transtive protocol requirement inference
2020-10-19 13:29:40 -07:00
Nathan Hawes
15f5222bbd [CodeCompletion][Sema] Allow missing args when solving if the completion location indicates the user may intend to write them later.
func foo(a: Int, b: Int) {}
func foo(a: String) {}

// Int and String should both be valid, despite the missing argument for the
// first overload since the second arg may just have not been written yet.
foo(a: <complete here>

func bar(a: (Int) -> ()) {}
func bar(a: (String, Int) -> ()) {}

// $0 being of type String should be valid, rather than just Int, since $1 may
// just have not been written yet.
bar { $0.<complete here> }
2020-10-19 12:16:19 -07:00
Luciano Almeida
907b06901f Merge pull request #34330 from LucianoPAlmeida/SR-13732-crash-invalid
[SR-13732] [Sema] Fix crash on simplifyFix constraint for tuple mismatch
2020-10-16 21:18:22 -03:00
Luciano Almeida
883d583a2f [CSSimplify] Make sure visit and record holes recursivelly for dependent member type 2020-10-16 18:32:20 -03:00
Pavel Yaskevich
5dc9919aff [ConstraintSystem] NFC: Make SemaTest a friend of ConstraintSystem
This is necessary in order to have access to private members of
a `ConstraintSystem` for testing purposes, such as logic related
to potential binding computation.
2020-10-15 16:27:35 -07:00
Pavel Yaskevich
d011bf3d7d [CSBindings] Implement transtive protocol requirement inference
Implements iterative protocol requirement inference through
subtype, conversion and equivalence relationships.

This algorithm doesn't depend on a type variable finalization
order (which is currently the order of type variable introduction).

If a given type variable doesn't yet have its transitive protocol
requirements inferred, algorithm would use iterative depth-first
walk through its supertypes and equivalences and incrementally
infer transitive protocols for each type variable involved,
transferring new information down the chain e.g.

  T1   T3
   \   /
    T4    T5
     \    /
       T2

Here `T1`, `T3` are supertypes of `T4`, `T4` and `T5`
are supertypes of `T2`.

Let's assume that algorithm starts at `T2` and none of the involved
type variables have their protocol requirements inferred yet.

First, it would consider supertypes of `T2` which are `T4` and `T5`,
since `T5` is the last in the chain algorithm would transfer its
direct protocol requirements to `T2`. `T4` has supertypes `T1` and
`T3` - they transfer their direct protocol requirements to `T4`
and `T4` transfers its direct and transitive (from `T1` and `T3`)
protocol requirements to `T2`. At this point all the type variables
in subtype chain have their transitive protocol requirements resolved
and cached so they don't have to be re-inferred later.
2020-10-15 16:27:35 -07:00