Commit Graph

1154 Commits

Author SHA1 Message Date
Pavel Yaskevich
aa212d54ef [ConstraintSystem] Add a fix to ignore contextual type mismatch
Ignore contextual type mismatch to allow solver to make progress
towards solution and diagnose the problem later.
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
omochimetaru
5b773fc46d [Sema] fix lvalue-ness constrain 2019-08-11 21:59:58 +09:00
Pavel Yaskevich
d2445d5924 Merge pull request #26577 from xedin/improve-force-downcast-diags
[Diagnostics] Make force downcast fix contextual and move it to `repa…
2019-08-09 09:19:10 -07:00
Pavel Yaskevich
385fb0c665 [Diagnostics] Make force downcast fix contextual and move it to repairFailures
This way it covers a lot more ground and doesn't conflict with
other fixes.

Another notable change is related to check for IUO associated
with source type, that covers cases like:

```swift
func foo(_ v: NSString!) -> String {
  return v
}
```

Instead of general conversion failure check for IUO enables solver
to introduce force downcast fix.
2019-08-09 01:09:52 -07:00
Greg Titus
db3b0d949c Merge pull request #26054 from gregomni/kp_closures
[ConstraintSystem][SE-0249] Key Path Expressions as Functions
2019-08-07 20:43:11 -07:00
Holly Borla
34b6241fc9 [Diagnostics] Move fix creation of AllowAutoClosurePointerConversion from
`matchTypes` to `repairFailures`.

This change handles invalid inout-to-pointer, array-to-pointer, and string-
to-pointer autoclosure conversions in the same way.
2019-08-07 18:05:51 -07:00
Holly Borla
04f09d0406 [ConstraintLocator] Rename isAutoclosureResult to isForAutoclosureResult 2019-08-07 13:56:35 -07:00
Pavel Yaskevich
8ac2ef8611 [Diagnostics] Extend generic argument mismatch to cover [T] to Unsafe*Pointer<T> conversions
Example:

```swift
func foo(_ x: UnsafePointer<Int>) {}
var arr: [Float] = [0, 1, 2]
foo(&arr) // Cannot convert [Float] to UnsafePointer<Int> because of Float vs. Int
```
2019-08-07 13:16:20 -07:00
Holly Borla
7679a5a433 [Diagnostics] Improve the diagnostic for invalid optional pointer conversions for an
autoclosure result type.

Skip `ConstraintLocator::OptionalPayload` when checking if we're in an autoclosure context for
an inout-to-pointer conversion. This lets us apply the `AllowAutoClosurePointerConversion`
constraint fix when the pointer is optional.

This resolves rdar://problem/53532404
2019-08-07 12:41:11 -07:00
Pavel Yaskevich
a34fe9ae55 [Diagnostics] Extend missing & diagnostic to cover pointer conversions
Example:

```swift
func foo(_: UnsafePointer<Int>) {}

var x: Int = 0
foo(x) <- should suggest adding `&`
```
2019-08-06 17:52:24 -07:00
Greg Titus
9c0aad3737 Handle conversions of keypath tyvars to function type specifically in the solver. 2019-08-06 07:52:56 -07:00
Greg Titus
9ab22cf8ae Combine internal tryMatchRootAndValue funcs for cleaner code. 2019-08-06 07:52:56 -07:00
gregomni
a2cd53d802 Handle simplification of optional-chaining key path literals, distinguish function type and keypath BGT type without explicit disjunctions. 2019-08-06 07:52:56 -07:00
gregomni
5e98f3e8e5 Implicit conversion of KeyPath<Root,Value> to (Root) -> Value.
Fix autoclosure param interface type when it involves archetypes.

Had some repeated locals from moving this block of code around - cleaned up.

Alternate implementation where KeyPathExpr is essentially a literal type and can be either a KeyPath(R,V) or (R)->V.

Some unneccessary code now.

Implicit closure pieces need valid sourceLocs in case a coerce expr needs to refer to the beginning of the closure in `\.foo as (A) -> B`.
Removed explicit noescape from function type so `let a: (A) -> B = \.foo` is valid.
Remove optimization that optional path is always read-only KP type in CSGen, since it can also now be of function type.
2019-08-06 07:52:56 -07:00
Holly Borla
caeb2a44a8 Merge pull request #26470 from hborla/autoclosure-pointer-conversion-diagnostic
[Diagnostics] Improve the diagnostic for invalid pointer conversion for an autoclosure result type.
2019-08-05 10:06:15 -07:00
Pavel Yaskevich
b148ea0a41 Merge pull request #26444 from xedin/diag-missing-call-with-defaults
[CSDiagnostics] Teach `missing explicit call` fix to account for defa…
2019-08-02 16:36:40 -07:00
Holly Borla
e76f5f1d0b [Diagnostics] Improve the diagnostic for invalid pointer conversion for an autoclosure result type.
Add constraint fix `AllowAutoClosurePointerConversion` and corresponding diagnostic
`AutoClosurePointerConversionFailure`. When we discover that we're trying to do an
inout-to-pointer conversion in `matchTypes`, add the constraint fix, which tries to do the
conversion as if the pointer type is a regular function argument.
2019-08-02 14:16:06 -07:00
Pavel Yaskevich
2cfcdf49dc Merge pull request #26391 from sl/port-tuple-assignment-diagnostic
[Diagnostics] Ported tuple mismatch diagnostic to new diagnostic framework
2019-08-02 13:22:52 -07:00
Sam Lazarus
0fa9ec3c22 Diagnostics: Changed tuple mismatch fix constraint generation to use matchTupleTypes 2019-08-02 13:52:24 -04:00
Pavel Yaskevich
5c82b57096 [CSDiagnostics] Teach missing explicit call fix to account for defaults
Currently we only produce a fix if function type doesn't have any
parameters, but it should also account for function having defaults
for all of its parameters which would still allow for nullary call.
2019-07-31 17:21:49 -07:00
Xi Ge
b6f1f0659e Merge pull request #26437 from nkcsgexi/HasDynamicMemberLookupAttributeRequest
IDE+Evaluator: refactor the sema implementation of hasDynamicMemberLookupAttribute to a request and move the function wrapper to libIDE. NFC
2019-07-31 13:39:27 -07:00
Xi Ge
9065ae93f6 IDE+Evaluator: refactor the sema implementation of hasDynamicMemberLookupAttribute as a request and move the function wrapper to libIDE. NFC 2019-07-31 11:30:25 -07:00
Pavel Yaskevich
7e1eae4a68 [ConstraintSystem] NFC: getCalleeDeclAndArgs no longer needs scratch space for argument labels 2019-07-31 00:00:50 -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
e2dd9da5e3 [ConstraintSystem] NFC: Move argument info handling into constraint system 2019-07-31 00:00:21 -07:00
Sam Lazarus
a3b56c2790 Diagnostics: Ported tuple mismatch diagnostic to new diagnostic framework 2019-07-29 13:45:08 -04:00
Pavel Yaskevich
effd0d00c6 Merge pull request #26302 from xedin/remove-label-mismatch-from-lookup
[Diagnostics] Don't filter overload set candidates based on labels in lookup
2019-07-26 10:34:23 -07:00
Pavel Yaskevich
be470f5dd7 [ConstraintSystem] Don't attempt to optimize disjunction in diagnostic mode
Don't attempt disjunction optimization in "diagnostic mode"
because in such mode we'd like to attempt all of the available
overloads regardless of of problems related to missing or
extraneous labels and/or arguments.
2019-07-25 00:36:00 -07:00
Pavel Yaskevich
8b33cd0e2e [ConstraintSystem] Include argument labels into member name for AnyObject lookup
If we're referencing AnyObject and we have argument labels, put
the argument labels into the name: we don't want to look for
anything else, because the cost of the general search is so high.
2019-07-25 00:36:00 -07:00
Pavel Yaskevich
b5cd400262 [ConstraintSystem] De-prioritize fixes which suggest removing extraneous labels
The rule is that if there is a label it's evidence that the
intent is to reference a particular overload where that label
would match, so let's try to de-prioritize fixes for overloads
where labels didn't line up correctly and suggestion is to
remove certain labels vs. fixes when labels did line up but
types or requirements didn't.
2019-07-25 00:36:00 -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
8355f3d270 [Constraint graph] Move constraint uniquing into gatherConstraints().
Simplify the interface to gatherConstraints() by performing the
uniquing within the function itself and returning only the resulting
(uniqued) vector of constraints.
2019-07-25 02:26:49 -04:00
Suyash Srijan
afa14713e9 Merge branch 'master' into fix/SR-11074 2019-07-17 08:40:49 +01:00
Suyash Srijan
1a1bff46d8 [CS] Don't crash when using magic literals as default arg
Squash all commits into one
2019-07-17 01:20:25 +01:00
Pavel Yaskevich
eb627085e7 [CSFix] Adjust tuple splat fix attempt to record a fix and return a boolean 2019-07-16 11:06:42 -07:00
Pavel Yaskevich
9c196eb981 [ConstraintSystem] Add a fix to allow tuple splat for calls with single tuple parameter 2019-07-16 11:06:42 -07:00
Pavel Yaskevich
cd07652f22 [ConstraintSystem] Don't attempt dynamic member lookup on invalid base
If `subscript(dynamicMember:)` is unviable because it's either
an instance method referenced on type or static method
referenced on an instance of type, attempting dynamic
member lookup would be incorrect since it's unclear
what is intended.

Resolves: rdar://problem/48994658
2019-07-11 15:57:17 -07:00
Pavel Yaskevich
22fb079373 [ConstraintSystem] Don't let conditional conformances shadow members accessible through dynamic lookup
Currently if there is a conditional conformance to a type marked
as `@dynamicMemberLookup` with member that shadows one accessible
through dynamic lookup we'd report an error if that conformance
has not been satisfied.

Better behavior would be to consider dynamic member lookup and
if that fits let the expression type-check.

Resolves: rdar://problem/52779809
2019-07-10 15:31:24 -07:00
Pavel Yaskevich
0316bb1cce [CSFix] NFC: Fix a typo orRValueBase -> onRValueBase 2019-07-09 11:00:14 -07:00
Pavel Yaskevich
1e8ae99008 [CSFix] Add a fix for invalid reference to mutating member on immutable base 2019-07-09 00:25:30 -07:00
Pavel Yaskevich
133e85bec5 [CSFix] NFC: Extract common base class for all invalid member refs 2019-07-09 00:25:30 -07:00
Pavel Yaskevich
626b93cca2 [ConstraintSystem] Make sure that property wrapper diagnostics don't consider invalid declarations 2019-07-04 20:01:19 -07:00
Pavel Yaskevich
1c161e7109 [Diagnostics] Extend property wrapper diagnostics to support subscript refs
Previously unintended property wrapper unwrap diagnostics supported only
dot expressions, let's extend it to cover subscripts as well.
2019-07-03 22:13:31 -07:00
Pavel Yaskevich
b8c25db47e [ConstraintSystem] Add keypath subscript choice only if argument has an expected label
Currently only valid way to form keypath subscript is to use `keyPath:`
label in subscript invocation, so let's avoid adding keypath overload
choice to every subscript lookup and instead only add it when it could
potentially match.

This among other things greatly helps diagnostics because sometimes
`keypath application` becomes the only choice even although it's
not really viable, which impedes member reference diagnostics.
2019-07-03 00:48:04 -07:00
Pavel Yaskevich
7a0b8950cc Merge pull request #25904 from xedin/dont-fix-wrapper-if-generic-args-didnt-line-up
[ConstraintSystem] Don't try fixes while matching types for property …
2019-07-01 11:19:50 -07:00
Pavel Yaskevich
84a6103128 Merge pull request #25844 from xedin/diagnose-instance-method-on-metatype
[Diagnostics] Diagnose all invalid references to instance methods
2019-07-01 00:33:54 -07:00
Pavel Yaskevich
f7913f869e [ConstraintSystem] Don't try fixes while matching types for property wrapper diagnostics
While trying to figure out whether there is a missing or extraneous
property wrapper reference, let's not try to fix any generic argument
mismatches associated with wrapper or wrapped types, otherwise the
fix is going to be incorrect.
2019-07-01 00:27:38 -07:00