Commit Graph

128 Commits

Author SHA1 Message Date
Mishal Shah
c2fd49cebb Merge pull request #39473 from apple/rebranch
Update swift:main to support llvm-project:stable/20210726 changes (Rebranch merge)
2021-10-11 09:00:51 -07:00
Pavel Yaskevich
67a721485f [ConstraintSystem] Compute variables referenced by conjunction elements incrementally
Attempting to pre-compute a set of referenced type variables
upfront is incorrect because parameter(s) and/or result type
could be bound before conjunction is attempted. Let's compute
a set of referenced variables before each element gets attempted.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
c554fca5f7 [CSStep] Isolated conjunctions can't see outer solutions
All of the previously deduced solutions should be hidden
until isolated conjunction is successfully solved.
2021-10-08 10:08:03 -07:00
Pavel Yaskevich
98a6a8441c [CSStep] Conjunction: integrate isolation scope into snapshot
It helps to simply handling of outer constrants because they have
to be added to the constraint system before scope is created but
constraint graph have to get updated after to make sure that
incremental binding inference already knows about types inferred
from conjunction.
2021-10-08 10:08:02 -07:00
Pavel Yaskevich
405034f416 [CSStep] Always restore snapshot once isolated conjunction step is done
Turn `SolverSnapshot::restore` into a destructor to make sure that
constraints are always returned when optional is reset.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
f05ec83e01 [CSStep] Filter solutions after each conjunction step
`SplitterStep` doesn't filter solutions if it results in a single
component (which is does for conjunction elements), so the solutions
have to be filtered manually after each element is done.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
1fa7e1d72c [CSStep] Fail conjunction if element attempt fails 2021-10-08 10:08:01 -07:00
Pavel Yaskevich
28d1bacdce [ConstraintSystem] Implement conjunction step
Iterate over all of the elements one-by-one and make sure that
each results in a single solution, otherwise fail the conjunction step.

Once all of the elements are handled either stop or,
if conjunction step has been performed in isolation,
return all of the outer constraints back to the system
and attempt to solve for outer context - that should
produce one or more solutions for conjunction to be
considered successfully solved.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
27275f6214 [CSStep] Add an implementation of ConjunctionStep
It behavies similar to `DisjunctionStep` but attempts all of
its elements unless there is an inference failure.
2021-10-08 10:08:00 -07:00
swift-ci
849dee3b22 Merge remote-tracking branch 'origin/main' into rebranch 2021-08-06 15:33:39 -07:00
Pavel Yaskevich
48d4997891 [CSStep] Don't abort upon discovering constraint system in an invalid state
Setting `InvalidState` already makes sure that there is no possibility
of miscompile.
2021-08-06 10:24:39 -07:00
Pavel Yaskevich
d8cdbcd0c7 [ConstraintSystem] Add InvalidState flag to indicate that any attempt to solve should fail
As soon as `InvalidState` flag is set solving of the constraint
system as aborted and all subsequent calls to `solveImpl` would
produce no solutions.
2021-08-06 10:03:05 -07:00
Pavel Yaskevich
04faa2aadb [ConstraintSystem] Remove check for left-over free type variables since it's handle gracefully 2021-08-05 16:05:54 -07:00
Pavel Yaskevich
b07139e591 [ConstraintSystem] Relax the left-over information check in ComponentStep
Instead of crashing in release builds, let's simplify fail the
step and let type-checker produce a fallback diagnostic. Maintain
a trap in debug builds to make it easier to investigate the root
cause of the issue.

Resolves: rdar://56167233
2021-08-05 15:08:13 -07:00
Arnold Schwaighofer
5a83172a55 Merge remote-tracking branch 'upstream/main' into rebranch 2021-08-05 12:04:56 -07:00
Holly Borla
2d851ff619 Merge pull request #38700 from hborla/improve-transitive-conformance-heuristic
[ConstraintSystem] Improve the "transitive conformance" heuristic and remove early conformance checking from `DisjunctionStep::shouldSkip`
2021-07-31 13:51:49 -07:00
Holly Borla
241c1e4ea0 [ConstraintSystem] Remove the conformance checking heuristic in
DisjunctionStep::shouldSkip. This heuristic is generalized via the
transitive conformance constraint.
2021-07-29 19:32:06 -07:00
swift-ci
fd87ebc401 Merge remote-tracking branch 'origin/main' into rebranch 2021-07-23 07:13:34 -07:00
Robert Widmann
1329f3cfbd [NFC] Lift getGenericEnvironment() into GenericSignature 2021-07-22 23:33:02 -07:00
Robert Widmann
d86551de67 Lift Requirement and Parameter Accessors up to GenericSignature
Start treating the null {Can}GenericSignature as a regular signature
with no requirements and no parameters. This not only makes for a much
safer abstraction, but allows us to simplify a lot of the clients of
GenericSignature that would previously have to check for null before
using the abstraction.
2021-07-22 23:27:05 -07:00
Evan Wilde
0919c2cd70 StringRef ends/startswith-lower -> insensitive
Updating StringRef startswith and endswith API to use insensitive rather
than lower.

(cherry picked from commit 1b8b39e6de)
2021-07-02 10:55:17 -07:00
Alex Hoppen
c3ce40ddb6 [Sema] Use different solution vectors for ComponentSteps created by DependentComponentSplitterStep
Currently all `ComponentSteps` created by `DependentComponentSplitterStep` share the same `Solutions` vector. Because of this, the `ComponentStep`s might modify solutions created by previous `ComponentStep`s. Use different `Solutions` vectors for each `ComponentStep` to avoid sharing information between the `ComponentStep`s.

The concrete manifestation in the added test case is that the `Bar` overload gets added to `Solutions`, it’s score gets reduced by its `ComponentStep` original score, then the `Foo` overload gets added to `Solutions` and both solutions have their score decreased by the `OriginalScore` of `Foo`’s `ComponentStep`, causing `Bar`’s score to underflow.

Fixes rdar://78780840 [SR-14692]
2021-06-18 15:17:11 +02:00
Slava Pestov
131d3f4bce Sema: Pass down a ModuleDecl instead of a DeclContext to conformsToProtocol()
... and a bunch of follow-up simplifications pushing ModuleDecls further
up, since I couldn't resist the yak shave.
2021-05-17 16:34:18 -04:00
Slava Pestov
73deb7e833 Sema: Introduce a simpler form of checkGenericArguments() 2021-05-17 16:34:18 -04:00
Ikko Ashimine
97029aa9f9 Fix typo in CSStep.cpp
substition -> substitution
2021-05-02 02:20:20 +09:00
Pavel Yaskevich
957e05cfb1 [Constraint] Allow constraints to be disabled only in "performance" mode 2021-04-27 16:11:47 -07:00
Pavel Yaskevich
a559ca8c53 [TypeChecker/SE-0307] NFC: Fix typos and clarify a couple of comments
Thanks Xiaodi Wu!
2021-03-17 14:11:53 -07:00
Pavel Yaskevich
cd3ec8c508 [ConstraintSystem] Allow attempting generic unary operators in presence of Double<->CGFloat conversion
Not all of the unary operators have `CGFloat` overloads,
so in order to preserve previous behavior (and overall
best solution) with implicit Double<->CGFloat conversion
we need to allow  attempting generic operators for such cases.

```swift
let _: CGFloat = -.pi / 2
```

`-` doesn't have `CGFloat` overload (which might be an oversight),
so in order to preserve type-checking behavior solver can't be
allowed to pick `-(Double) -> Double` based on overload of `/`,
the best possible solution would be with `/` as `(CGFloat, CGFloat) -> CGFloat`
and `-` on a `FloatingPoint` protocol.
2021-03-17 00:18:19 -07:00
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