Commit Graph

2078 Commits

Author SHA1 Message Date
Alex Hoppen
d64b8ecea6 [CodeCompletion] Migrate key path completion to be solver based
This commit essentially consistes of the following steps:
- Add a new code completion key path component that represents the code completion token inside a key path. Previously, the key path would have an invalid component at the end if it contained a code completion token.
- When type checking the key path, model the code completion token’s result type by a new type variable that is unrelated to the previous components (because the code completion token might resolve to anything).
- Since the code completion token is now properly modelled in the constraint system, we can use the solver based code completion implementation and inspect any solution determined by the constraint solver. The base type for code completion is now the result type of the key path component that preceeds the code completion component.

This resolves bugs where code completion was not working correctly if the key path’s type had a generic base or result type. It’s also nice to have moved another completion type over to the solver-based implementation.

Resolves rdar://78779234 [SR-14685] and rdar://78779335 [SR-14703]
2021-06-25 23:19:35 +02:00
Pavel Yaskevich
0562c31cad [CSSimplify] Look through optionals in contextual type while resolving a closure
Let's look through all optionals associated with contextual
type to make it possible to infer parameter/result type of
the closure faster e.g.:

```swift
func test(_: ((Int) -> Void)?) {
   ...
}

test { $0 + ... }
```

In this case dropping optionality from contextual type
`((Int) -> Void)?` allows `resolveClosure` to infer type
of `$0` directly (via `getContextualParamAt`) instead of
having to use type variable inference mechanism.
2021-06-24 15:29:01 -07:00
Holly Borla
d083c66686 [ConstraintSystem] Before matching tuple types, add each complete tuple
type to the locator.

This will provide context for tuple element mismatch diagnostics, instead
of attempting to compute the full tuple type in diagnostics code with a pile
of special cases (see getStructuralTypeContext in CSFix.cpp).
2021-06-24 09:40:14 -07:00
Pavel Yaskevich
be6e2fad8c [Diagnostics] Adjust Self conformance check to find non-decl overload choices
Use `findSelectedOverloadFor` instead of `findResolvedMemberRef`
to cover cases where member is found via base type unwrap, otherwise
conformance constraint would be re-inserted but never re-attempted
which results in a compiler crash.

Resolves: rdar://79268378
2021-06-22 15:32:51 -07:00
Pavel Yaskevich
c1bac12bb6 [CSSimplify] Fix warning check to account that "from" might be less optional than "to" type
While checking whether compiler needs to produce a checked cast warning,
account for the fact that "from" could be less optional than "to" e.g.
`0 as Any?`, so the difference has to be stored as a signed integer
otherwise it's going to underflow and result in a crash or infinite
recursion in the diagnostics.

Resolves: rdar://79523605
2021-06-21 16:38:04 -07:00
Doug Gregor
4afa371094 Introduce subtyping rule for functions with "isolated" parameters.
With isolated parameters being part of a function's type, check to
ensure that isolated and non-isolated parameters aren't incorrectly
matched. Specifically, it is okay to add `isolated` to a parameter
when there is a subtyping relationship, but not remove it:

```swift
actor A { }
func f(_: isolated A) { }
func g(_: A) { }

func test() {
  let _: (isolated A) -> Void = g    // okay to add 'isolated'
  let _: (A) -> Void = f             // error when removing 'isolated'
}
```
2021-06-07 23:59:39 -07:00
Pavel Yaskevich
9e54006cc6 [ConstraintSystem] Don't record a mismatch for synthesized arguments
Without context synthesized argument would be inferred as a hole,
so let's not record any additional fixes for it if there is no way
to infer it properly from a parameter (which could also be a hole
if it is a generic parameter type).

Resolves: rdar://78781552
2021-06-03 16:14:20 -07:00
Pavel Yaskevich
1c3d685fd8 [ConstraintSystem] Simplify relational constraints with the same dependent member type on both sides
If relational constraint has the same dependent member type on both
sides e.g. `$T1.Element == $T1.Element` allow its simplification,
since inference of `$T1` results in dependent member being resolved
to the same concrete type. Otherwise constraint system would be left
with this constraint in inactive state if `$T1` couldn't be resolved
which results in a crash.

Resolves: rdar://78623338
2021-05-28 17:49:55 -07:00
Joe Groff
92f56e7ec8 Allow conversions from actor-bound sync function type to unbound async function type.
For `async` function types, an actor constraint can be enforced by the callee by hopping executors,
unlike with `sync` functions, so doesn't need to influence the outward type of the function.

rdar://76248452
2021-05-21 14:17:50 -07:00
Pavel Yaskevich
87113d76ac Merge pull request #37430 from xedin/rdar-77942193
[ConstraintSystem] Detect passing sync closure to async argument early
2021-05-18 09:21:35 -07: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
Pavel Yaskevich
ace4d3ac5e [ConstraintSystem] Detect passing sync closure to async argument early
This helps in situations where there are multiple overloads which
differ only in async effect of their parameters. Passing sync argument
to a sync parameter is always preferred and when detected early
allows solver to avoid some of the duplicate work re-solving for
the rest of the path e.g.

```swift
func test<T>(_: () -> T) {}
func test<T>(_: () async -> T) {}

test {
  // sync work
}.op(...)
```

In this case since closure is synchronous first overload of `test`
is always preferred (when it's a match) and solver can skip re-checking
body of the closure and `op` call when it encounters `async` version.

Resolves: rdar://77942193
2021-05-14 13:49:39 -07:00
Holly Borla
e1591314cf Merge pull request #37380 from hborla/property-wrapper-inference-crash
[ConstraintSystem] Fix a constraint system crash with property wrapper inference using the $ syntax.
2021-05-12 08:55:51 -07:00
Holly Borla
ef58f6a827 [ConstraintSystem] If the contextual parameter type doesn't exist when
resolving a closure, create a new type variable for inferred property
wrapper types.
2021-05-11 18:26:22 -07:00
Pavel Yaskevich
7ce8a44f6d [CSSimplify] Increase fix impact when passing closure to a non-function type parameter
In overloaded context it's possible that there is an overload that
expects a closure but it can have other issues e.g. different number
of parameters, so in order to pick a better solution let's always
increase a score for overloads where closure is matched against a
non-function type parameter.
2021-05-11 15:07:17 -07:00
Pavel Yaskevich
29cb7ddbcb [Diagnostics] Handle ambiguities related to use of nil literal
When `nil` is passed as an argument to call with multiple overloads
it's possible that this would result in ambiguity where matched
expected argument type doesn't conform to `ExpressibleByNilLiteral`.

To handle situations like this locator for contextual mismatch
has to be adjusted to point to the call where `nil` is used, so
`diagnoseAmbiguityWithFixes` can identify multiple overloads and
produce a correct ambiguity diagnostic.

Resolves: rdar://75514153
2021-05-05 12:56:25 -07:00
Luciano Almeida
c069e86f83 [NFC][Sema] Changing API to facilitate recording pontential holes recursively 2021-05-01 23:06:25 -03:00
Pavel Yaskevich
f36ecf2fa1 [CSSimplify] Allow overload choices with missing labels to be considered for diagnostics
Let's make use of a newly added "disable for performance" flag to
allow solver to consider overload choices where the only issue is
missing one or more labels - this makes it for a much better
diagnostic experience without any performance impact for valid code.
2021-04-28 12:04:57 -07:00
Pavel Yaskevich
71372bce67 [Diagnostics] Switch to use contextual purpose associated with locator
`ContextualFailure` is the main beneficiary of additional information
associated with `ContextualType` element because it no longer has to
query solution for "purpose" of the contextual information.

Resolves: rdar://68795727
2021-04-26 09:51:25 -07:00
Pavel Yaskevich
51ff12d06e [ConstraintLocator] Augment ContextualType element to carry its purpose
Having purpose attached to the contextual type element makes it much
easier to diagnose contextual mismatches without involving constraint
system state.
2021-04-26 09:51:21 -07:00
Luciano Almeida
c15dde4265 Merge pull request #37057 from LucianoPAlmeida/placeholder-checkedcast-warn
[Sema] Do not attempt warn extraneous checked cast for placeholder types
2021-04-25 08:03:18 -03:00
Luciano Almeida
b851974424 [Sema] Do not attempt warn extraneous checked cast for placeholder types 2021-04-24 23:52:58 -03:00
Strieker
4e2b67cbae [NFC] Resolved merge conflict in CSSimplify.cpp after making changes to improve error handling for composed wrapped value mismatches 2021-04-22 11:49:21 -07:00
Strieker
c9b53ec66f [ConstraintSystem] Created a locator in CSGen to store the location where the wrappedValue type of a property wrapper was determined and added necessary logic so that a constraint locator can recognize a composed property wrapper’s wrapped value type in CSFix.cpp, CSFix.h, and CSSimplify.cpp. 2021-04-22 11:31:41 -07:00
Azoy
9ed732f0ab Introduce isDecl and getDeclType
fix enum logic issue

fix tests

guard against null types
2021-04-20 02:22:16 -04:00
Nathan Hawes
f75f5fe78d Merge pull request #36879 from nathawes/track-match-call-result
[ConstraintSystem] Record parameter bindings in solutions (NFC)
2021-04-18 06:10:23 +10:00
Luciano Almeida
00c25317ea Merge pull request #36883 from LucianoPAlmeida/SR-13483-closure-args
[SR-13483][Sema] Make sure to record the correct remove args fix when repairing a tuple destructure attempt
2021-04-16 08:34:07 -03:00
Nathan Hawes
c57c403ffa [ConstraintSystem] Record parameter bindings in solutions (NFC)
This saves us from needing to re-match args to params in CSApply and is also
useful for a forthcoming change migrating code completion in argument position
to use the solver-based typeCheckForCodeCompletion api.

rdar://76581093
2021-04-16 18:32:06 +10:00
Holly Borla
5bda9bd0c1 Merge pull request #36939 from hborla/wrapped-parameter-diagnostics
[ConstraintSystem] Fix a few diagnostic bugs for inferred property wrapper types.
2021-04-15 19:52:04 -07:00
Holly Borla
b11f465561 [ConstraintSystem] Fix a few diagnostic bugs when an inferred property
wrapper type is a hole or a structural type.
2021-04-15 16:49:25 -07:00
Luciano Almeida
f40b9b6c29 [Sema] Minor adjusting on RemoveExtraneousArguments record logic 2021-04-15 20:33:46 -03:00
Pavel Yaskevich
2de9053dae [Diagnostics] Filter operators if all their arguments are holes
Restrict filtering in `simplifyAppliedOverloadsImpl` to disable
overload set for operators only, because other calls e.g. regular
functions or subscripts could be filtered on labels and are less
overloaded.

Filtering non-operator calls could also lead to incorrect diagnostics
because first choice could have all sorts of different issues e.g.
incorrect labels and number of parameters.

Resolves: rdar://55369704
2021-04-14 15:44:13 -07:00
Luciano Almeida
ae93790c52 [Sema] Record the correct RemoveExtranousArguments fix for closure mismatch involving tuple destructuring 2021-04-14 09:29:08 -03:00
Pavel Yaskevich
b088aea88b [ConstraintSystem] Allow fixing r-value -> l-value mismatch without a fix for placeholders
If left-hand side of a conversion that requires l-value is a placeholder type,
let's fix that by propagating placeholder to the order side (to allow it to
infer a placeholder if needed) without recording a fix since placeholder can
be converted to `inout` and/or l-value and already indicates existence of a
problem at some other spot in the expression.

Resolves: rdar://76250381
2021-04-12 17:14:07 -07:00
Pavel Yaskevich
ceeee459b4 Merge pull request #36631 from xedin/conformance-perf-experiment
[Perf][CSSimplify] Transfer conformance requirements of a parameter to an argument
2021-04-12 13:13:56 -07:00
Pavel Yaskevich
ba1fc170bd Merge pull request #36845 from xedin/allow-double-cgfloat-with-one-side-is-optional
[TypeChecker] Allow Double<->CGFloat conversion with optional promotions
2021-04-12 13:10:20 -07:00
Pavel Yaskevich
c10f04d241 [TypeChecker] Allow Double<->CGFloat conversion with optional promotions
There are APIs that expect a `Double?` or `CGFloat?` argument
and it should be possible to pass `CGFloat` and `Double` respectively.
2021-04-09 12:04:34 -07:00
Pavel Yaskevich
4f19ba2c2a [CSSimplify] Teach transitive conformance check about Unsafe{Mutable}RawPointer conversions 2021-04-09 10:27:17 -07:00
Pavel Yaskevich
6add8f282f [CSSimplify] Fix transitive conformance check to use Optional<T> on if resolved type is not optional 2021-04-08 13:45:51 -07:00
Pavel Yaskevich
439cbafa8b [CSSimplify] Check if String/AnyHashable/Unsafe{Mutable}Pointer decls are available before using them 2021-04-08 13:42:15 -07:00
Pavel Yaskevich
ea9539d47f [CSSimplify] Add AnyHashable check to simplifyTransitivelyConformsTo
Just like `Optional` and `UnsafePointer` argument could be implicitly
converted to `AnyHashable`.
2021-04-08 13:41:25 -07:00
Pavel Yaskevich
fad62cb4b3 [CSSimplify] Avoid checking conditional conformances in simplifyTransitivelyConformsTo
It's not always clear what generic parameter type implicit conversion to pointer
is going to have, and its safer to check direct conformance in `simplifyTransitivelyConformsTo`
and let parameter handle the rest.
2021-04-08 10:17:10 -07:00
Pavel Yaskevich
5f2fdc9604 [CSSimplify] NFC: Move protocol composition check to simplifyTransitivelyConformsTo 2021-04-07 17:55:11 -07:00
Pavel Yaskevich
c509c030ea [CSSimplify] Transfer conformance requirements of a parameter to an argument
Performance optimization to help rule out generic overload choices sooner.

This is based on an observation of the constraint solver behavior,
consider following example:

```swift
func test<U: ExpressibleByStringLiteral>(_: U) {}

test(42)
```

Constraint System for call to `test` is going to look like this:

```
$T_test := ($T_U) -> $T_U
$T_U <conforms to> ExpressibleByStringLiteral
$T_42 <argument conversion> $T_U
$T_42 <literal conforms to> ExpressibleByIntegerLiteral
```

Currently to find out that `test` doesn't match, solver would have
to first find a binding for `$T_42`, then find a binding for `$T_U`
(through `<argument conversion>` constraint) and only later fail
while checking conformance of `Int` to `ExpressibleByStringLiteral`.

Instead of waiting for parameter to be bound, let's transfer conformance
requirements through a conversion constraint directly to an argument type,
since it has to conform to the same set of protocols as parameter to
be convertible (restrictions apply i.e. protocol composition types).

With that change it's possible to verify conformances early and reject
`test<U: ExpressibleByStringLiteral>` overload as soon as type of an
argument has been determined. This especially powerful for chained calls
because solver would only have to check as deep as first invald argument
binding.
2021-04-07 17:55:11 -07:00
Pavel Yaskevich
985843a21f [ConstraintSystem] Add a new transitive conformance constraint
Conformance constraints could be transferred through conversions,
but that would also require checking implicit conversions
such as optional and pointer promotions for conformance is the
type itself doesn't conform, for that let's add a special constraint
`TransitivelyConformsTo`.
2021-04-07 17:55:11 -07:00
Pavel Yaskevich
4a94afa357 [CSSimplify] Add a null check to guard against broken/missing ExpressibleByNilLiteral
I couldn't come up with an isolated test-case to add to the suite,
but it's possible that `getProtocol` returns `nullptr` for invalid
or missing protocol so there has to be a check for that otherwise
compiler is going to crash trying to access `getDeclaredType()`.

Resolves: rdar://76187450
2021-04-07 14:35:31 -07:00
Pavel Yaskevich
225e2dbeed [ConstraintSystem] Bind external closure parameter type to a concrete contextual type
Performance optimization.

If there is a concrete contextual type we could use, let's bind
it to the external type right away because internal type has to
be equal to that type anyway (through `BindParam` on external type
i.e. <internal> bind param <external> conv <concrete contextual>).

```swift
func test(_: ([String]) -> Void) {}

test { $0 == ["a", "b"] }
```

Without this optimization for almost all overloads of `==`
expect for one on `Equatable` and one on `Array` solver would
have to repeatedly try the same `[String]` type for `$0` and
fail, which does nothing expect hurts performance.

Resolves: rdar://19836070
Resolves: rdar://19357292
Resolves: rdar://75476311
2021-04-02 22:11:03 -07:00
Holly Borla
85e3c7b97e Merge pull request #36696 from hborla/wrapped-parameter-accessor-synthesis
[Property Wrappers] Fix accessor synthesis of wrapped parameters that infer the property wrapper attribute.
2021-04-01 18:14:56 -07:00
Holly Borla
2a4cc912b6 [Property Wrappers] Fix accessor synthesis of wrapped parameters that infer
the property wrapper attribute.
2021-03-31 18:38:41 -07:00
Pavel Yaskevich
ed8491c15b Revert "[TypeChecker] Add a flag to disable Double<->CGFloat implicit conversion"
This reverts commit 3c731d8748.
2021-03-31 11:10:21 -07:00