Commit Graph

998 Commits

Author SHA1 Message Date
swift-ci
7d437ec4a8 Merge pull request #27629 from DougGregor/builder-refactoring-prelude 2019-10-11 15:35:11 -07:00
Doug Gregor
87b5df7333 [Constraint system] Abstract the record of an applied function builder.
std::pair and std::tuple are a bad, bad drug. NFC, but helps with
future refactoring.
2019-10-11 10:31:27 -07:00
Doug Gregor
c128f6f3aa [Constraint system] Generalize ParamDecl type tracking to VarDecl. 2019-10-11 10:10:15 -07:00
Doug Gregor
eb61ae7971 [Type checker] Factor out ConstraintSystem::addJoinConstraint().
Use it for ternary expressions; it'll gain other clients soon.
2019-10-11 10:07:21 -07:00
Pavel Yaskevich
773ac24bc9 [ConstraintSystem] Detect and diagnose type mismatch failures of inout parameters
Currently absence of `subtyping` is the only problem detected and diagnosed specifically
for `inout` parameters, but there could be type mismatches in `inout` positions as well
 and we can use `argument-to-parameter mismatch fix to detect and diagnose them.
2019-10-10 17:06:13 -07:00
Hamish Knight
ec45b24b63 Use hasAppliedSelf in getFunctionArgApplyInfo
`callee->hasCurriedSelf()` isn't the correct check
here when we have an argument mismatch for the
self parameter in a curried application.
2019-10-03 15:26:31 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Pavel Yaskevich
b15ef15a60 [ConstraintSystem] Change missingArgument callback to produce an index of synthesized argument
In diagnostic mode allow argument matcher to synthesize new
arguments, which makes it much easier to diagnose problems
related to missing arguments.
2019-09-23 12:59:27 -07:00
Pavel Yaskevich
1cd1f259e0 [ConstraintSystem] NFC: Fix typo implact -> impact 2019-09-13 22:35:53 -07:00
Pavel Yaskevich
b898eaf326 [Diagnostics] Tailored diagnostics for reference equality operator mismatches 2019-09-13 22:35:52 -07:00
Pavel Yaskevich
8a4fcaa4ba [Diagnostics] Don't diagnoses missing Equatable in pattern-matching of invalid expressions
If expression is incorrect it most likely wouldn't be able to satisfy
`Equatable` or other requirements of `~=` operator overloads, but
at the same time the main problem is related to `case` expression
itself so let's not diagnose missing conformances.
2019-09-13 22:35:52 -07:00
Pavel Yaskevich
446d0f84f4 [ConstraintSystem] Rate impact of conformance failures based on non-conforming type use
Impact of conformance failure should be rated based on use of
associated generic parameter, because a single failure could
span multiple parameters and a result.

Originally score was only increased once for conformance failure.
That is not good enough in cases when conformance is associated
with an overload choice, because in that case we can have an argument
mismatch in other choice which would have the same score impact.
2019-09-13 22:35:50 -07:00
Pavel Yaskevich
93b39c9c23 [Diagnostics] Port pattern-matching mismatch diagnostic
Port diagnostics associated with implicit use of `~=` operator
which is used in `case` statements.
2019-09-13 22:35:50 -07:00
Slava Pestov
de4b63defc Sema: Refactor typeCheckCheckedCast() a bit
This removes all calls to typesSatisfyConstraint() except for the
isConvertibleTo() check at the beginning, in the process making the
analysis a little bit more accurate.
2019-09-12 16:37:53 -04:00
Hamish Knight
b9ad96a944 [CS] Add locator version of findSelectedOverloadFor 2019-09-10 12:06:35 +01:00
Hamish Knight
c745c17c38 Revert naming changes to getCalleeLocator & getChoiceFor
In addition, add a document comment to `getCalleeLocator` to clarify
its semantics.
2019-09-09 22:08:52 +01:00
Hamish Knight
67ee821123 [CS] Allow getCalleeLocator to find key path component callees
In order to do this we need it to take a ConstraintLocator argument so
we can tell which component we want the callee for. To make it clear
that we're looking for a callee at the anchor, also rename the member
to getAnchormostCalleeLocator.
2019-09-09 14:14:35 +01:00
Hamish Knight
fdbc21911b [CS] Don't create new locator when simplifying to anchor
We can directly use the version of `simplifyLocator` that works on
an anchor and path array ref instead.
2019-09-02 18:09:47 +01:00
Pavel Yaskevich
7b8fb88eb9 Merge pull request #26852 from xedin/refactor-treat-r-as-l-value
[ConstraintSystem] Be more principled about recording r-value -> l-value fix
2019-08-28 00:13:55 -07:00
Pavel Yaskevich
1b397ed486 [ConstraintSystem] Be more principled about recording r-value -> l-value fix
Instead of recording `TreatRValueAsLValue` fix directly inside
`matchTypes`, let's move towards recording it specifically for
each possible case in `repairFailures` which makes it a lot
easier to determine what other fixes could be applied (if any).
2019-08-27 13:13:28 -07:00
Pavel Yaskevich
fa9c3f3a10 [ConstraintSystem] Track missing members via "holes"
Replace specialized `MissingMembers` tracking with more general
constraint system "holes" which simplifies solver logic.
2019-08-22 17:25:05 -07:00
Pavel Yaskevich
8afc560708 [ConstraintSystem] Introduce a notion of a "hole"
A "hole" is a type variable which type couldn't be determined
due to an inference failure e.g. missing member, ambiguous generic
parameter which hasn't been explicitly specified.

It is used to propagate information about failures and avoid
recording fixes which are a consequence of earlier failures e.g.

```swift
func foo<T: BinaryInteger>(_: T) {}

struct S {}

foo(S.bar) // Actual failure here is that `S` doesn't have a member
           // `bar` but a consequence of that failure is that generic
           // parameter `T` doesn't conform to `BinaryInteger`.
```
2019-08-22 17:24:45 -07:00
Doug Gregor
562ed4d999 [Constraint solver] Tie together all of the type variables in a key path.
This is a workaround for rdar://problem/54322807 that should limit the
expansion in the number of constraint scopes visited.
2019-08-20 11:11:07 -07:00
Pavel Yaskevich
fc3d9755fb Merge pull request #26677 from xedin/dedup-generic-requirements
[Diagnostics] Correctly identify location of requirement failure
2019-08-17 00:02:08 -07:00
Pavel Yaskevich
df47d1b7c5 [ContraintSystem] Don't de-duplicate generic requirements
Looks like this is not really needed since fixed requirements
cache can handle such de-duplication.
2019-08-16 22:22:16 -07:00
Pavel Yaskevich
12648d6b4a [ConstraintSystem] Track all generic requirements fixed along current path
Such tracking makes it easier to ignore already "fixed" requirements
which have been recorded in the constraint system multiple times e.g.
a call to initializer would open both base type and initializer
method which have shared (if not the same) requirements.
2019-08-16 22:02:37 -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
Pavel Yaskevich
746cad564f Merge pull request #26459 from xedin/diag-conversion-to-specified-type
[ConstraintSystem] Add a fix to ignore contextual type mismatch
2019-08-14 12:58:09 -07:00
Pavel Yaskevich
e7bff97929 [Diagnostics] NFC: Temporarily move couple of auxiliary functions to ConstraintSystem
Functions like `isRawRepresentable*` and `conformsToKnownProtocol`
have to be be shared between CSDiag and new diagnostics framework
until relevant code is removed from the former.
2019-08-13 11:55:08 -07:00
Doug Gregor
3c69f6a305 [Constraint solver] Introduce one-way constraints.
Introduce the notion of "one-way" binding constraints of the form

  $T0 one-way bind to $T1

which treats the type variables $T0 and $T1 as independent up until
the point where $T1 simplifies down to a concrete type, at which point
$T0 will be bound to that concrete type. $T0 won't be bound in any
other way, so type information ends up being propagated right-to-left,
only. This allows a constraint system to be broken up in more
components that are solved independently. Specifically, the connected
components algorithm now proceeds as follows:

1. Compute connected components, excluding one-way constraints from
consideration.
2. Compute a directed graph amongst the components using only the
one-way constraints, where an edge A -> B indicates that the type
variables in component A need to be solved before those in component
B.
3. Using the directed graph, compute the set of components that need
to be solved before a given component.

To utilize this, implement a new kind of solver step that handles the
propagation of partial solutions across one-way constraints. This
introduces a new kind of "split" within a connected component, where
we collect each combination of partial solutions for the input
components and (separately) try to solve the constraints in this
component. Any correct solution from any of these attempts will then
be recorded as a (partial) solution for this component.

For example, consider:

  let _: Int8 = b ? Builtin.one_way(int8Or16(17)) :
  Builtin.one_way(int8Or16(42\
))

where int8Or16 is overloaded with types `(Int8) -> Int8` and
`(Int16) -> Int16`. There are two one-way components (`int8Or16(17)`)
and (`int8Or16(42)`), each of which can produce a value of type `Int8`
or `Int16`. Those two components will be solved independently, and the
partial solutions for each will be fed into the component that
evaluates the ternary operator. There are four ways to attempt that
evaluation:

```
  [Int8, Int8]
  [Int8, Int16]
  [Int16, Int8]
  [Int16, Int16]

To test this, introduce a new expression builtin `Builtin.one_way(x)` that
introduces a one-way expression constraint binding the result of the
expression 'x'. The builtin is meant to be used for testing purposes,
and the one-way constraint expression itself can be synthesized by the
type checker to introduce one-way constraints later on.

Of these two, there are only two (partial) solutions that can work at
all, because the types in the ternary operator need a common
supertype:

  [Int8, Int8]
  [Int16, Int16]

Therefore, these are the partial solutions that will be considered the
results of the component containing the ternary expression. Note that
only one of them meets the final constraint (convertibility to
`Int8`), so the expression is well-formed.

Part of rdar://problem/50150793.
2019-08-13 11:48:42 -07:00
Doug Gregor
4c04ced939 [Constraint graph] Make connected components more self-contained.
Have the constraint graph's connected-component implementation be more
self-contained, producing a vector containing each of the actual
components (where each is defined by a list of type variables and a list
of constraints). This simplifies the contract with the client
(SplitterStep) and eliminates a bunch of separate mapping steps to
interpret the results.

It also lets us enrich the Component data structure in the future.
2019-08-07 08:32:34 -07:00
Pavel Yaskevich
45aec3c6ad [ConstraintSystem] NFC: Extract logic related to forming locator for argument info retrieval 2019-07-31 00:52:00 -07:00
Pavel Yaskevich
32040ebd65 [ConstraintSystem] NFC: Refactor getArgumentInfo to accept ConstraintLocator 2019-07-31 00:00:21 -07:00
Pavel Yaskevich
d04ab92934 [ConstraintSystem] Use previous collected argument info in getCalleeDeclAndArgs 2019-07-31 00:00:21 -07:00
Pavel Yaskevich
29d1f6a705 [ConstraintSystem] Don't allow overwrites while collecting argument info
Currently, because argument info has been collected based solely
on anchor, it would be possible to overwrite labels for expressions
like `foo[0](x)` since `ApplyExpr` uses its function expression as
a key for argument information cache, which leads to errors while
attempting optimizations based on that information.
2019-07-31 00:00:21 -07:00
Pavel Yaskevich
e2dd9da5e3 [ConstraintSystem] NFC: Move argument info handling into constraint system 2019-07-31 00:00:21 -07:00
Pavel Yaskevich
c85deb1a8d [ConstraintSystem] NFC: Refactor areConservativelyCompatibleArgumentLabels to accept arguments directly
Since `areConservativelyCompatibleArgumentLabels` is only used by
`simplifyAppliedOverloads` now, it's easy to pass arguments directly
instead of trying to form them from list of labels.
2019-07-25 00:36:00 -07:00
Pavel Yaskevich
d3205b202e [ConstraintSystem] Remove UR_LabelMismatch overload unavailability reason
This helps with:

- Diagnostics because solver would get more choices to work with
  in diagnostic mode;
- Avoid adding the same overload multiple times
  (retry after label mismatch and no viable candidates);
- Unify overload handling/filtering in `simplfyAppliedOverloads`.
2019-07-25 00:36:00 -07:00
Doug Gregor
e1b359b8a4 [Type checker] Substitute into alternate properties when trying to fix wrapped property refereces
Fixes SR-11149 / rdar://problem/53204113
2019-07-19 15:40:20 -07:00
Pavel Yaskevich
a2652ea8e1 [Diagnostics] NFC: Move logic related to member ref on optional without unwrap to new framework
Since there is already a diagnostic for this `MemberAccessOnOptionalBaseFailure`
it should incorporate all related diagnostic logic and could be used from CSDiag.
2019-07-02 16:00:36 -07:00
David Zarzycki
d0b221edfd [Sema] NFC: Expand TypeVar ID/GraphIndex to 32 bits
Users are edging closer to the arbitrary but plausible 20-bit limit.
2019-06-29 12:02:59 +02:00
Sam Lazarus
70bdcfd370 Sema: Add handling for fixes when member is present on $$ version of decl 2019-06-28 12:09:50 -07:00
Sam Lazarus
55b17feac1 Sema: Handle storage wrappers correctly in diagnostics 2019-06-28 12:09:49 -07:00
Sam Lazarus
7054fcb270 Sema: Stop anon closure params from being diagnosed like property wrapper decls 2019-06-28 12:09:49 -07:00
Sam Lazarus
6ec03da6b0 Sema: Add property wrapper diagnostic for unnecessary $ in member access
Additionally, refactor some of the logic for the original add $ diagnostic
so that a lot of logic can be shared between the two. Also rename the
original fix and diagnostic to better reflect their purpose.
2019-06-28 12:09:49 -07:00
Slava Pestov
7434e37605 Sema: Fix crash with circular property initial values
Fixes <rdar://45313700>, <https://bugs.swift.org/browse/SR-9015>.
2019-06-27 23:13:46 -04:00
Pavel Yaskevich
b72b3cbf0d Merge pull request #25721 from theblixguy/fix/SR-10992
[ConstraintSystem] Fix crash on function conversion reliant on conditional conformance
2019-06-24 16:52:35 -07:00
Suyash Srijan
376fea12a8 [ConstraintSystem] Adds a new overload for getConstraintLocator that automatically gets the summary flags 2019-06-24 21:51:57 +01:00
Sam Lazarus
ebcbaca968 [Diagnostics] Add a diagnostic for inserting a $ to remove an extraneous property wrapper unwrap (#25507) 2019-06-20 20:28:25 -04:00
Hamish Knight
e1f8af2389 Merge remote-tracking branch 'upstream/master' into a-couple-of-tangents 2019-06-18 19:09:06 +01:00