Commit Graph

1988 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
f00c578761 Merge pull request #34401 from xedin/implicit-cgfloat-conversion
[DNM][TypeChecker] Implement Double <-> CGFloat implicit conversion
2021-03-31 10:20:28 -07:00
Holly Borla
6f4b62a93c Merge pull request #36568 from hborla/property-wrapper-constraint
[ConstraintSystem] Add a property-wrapper constraint to allow for inference of implicit property wrappers
2021-03-24 17:13:51 -07:00
Holly Borla
76c4c3dc3f [Diagnostics] Add back a dedicated fix/diagnostic for using an invalid
type as a property wrapper.
2021-03-24 11:24:21 -07:00
Holly Borla
7cd8add885 [ConstraintSystem] Add a property wrapper constraint to delay checking
if an inferred wrapper type is a property wrapper until that type is
resolved.
2021-03-24 11:24:21 -07:00
Holly Borla
f05589f6c4 [Diagnostics] Improve diagnostics for passing an invalid projected
value argument.
2021-03-24 11:24:21 -07:00
Pavel Yaskevich
d7e0971764 Merge pull request #36531 from LucianoPAlmeida/SR-14096-kp-as-fn
[Sema][SR-14096] Change conditions in which key path component mismatch fix are diagnosed
2021-03-22 10:42:08 -07:00
Luciano Almeida
0bca6ce73c [Sema] Do not skip key path last component/value equal constraint failure fix 2021-03-21 18:29:47 -03:00
Doug Gregor
52096a640e SE-0302: Rename ConcurrentValue/@concurrent to Sendable/@Sendable. 2021-03-18 23:48:21 -07:00
swift-ci
3b2c3d27fe Merge pull request #36473 from DougGregor/global-actor-function-type-conversion 2021-03-17 18:55:13 -07:00
Pavel Yaskevich
5e6820466c Merge pull request #36459 from xedin/rdar-75146811
[ConstraintSystem] Teach `getFunctionArgApplyInfo` about `inout` expr…
2021-03-17 16:18:23 -07:00
Doug Gregor
e50a944942 Limit conversion that adds a global actor to a function type to subtyping.
Without this, we'll end up accepting invalid conversions when there
is a global actor-qualified function type in a non-top-level structural
position.
2021-03-17 16:16:17 -07:00
Pavel Yaskevich
1dd914a651 Merge pull request #36445 from xedin/rdar-75367157
[Diagnostics] Check whether missing conformance could be fixed by using `.rawValue`
2021-03-17 15:24:33 -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
Doug Gregor
d6fc1fd60f Implement conversions for global-actor-qualified functions.
One can convert from a function value without a global actor to a
similar function type that does have a global actor, but one cannot
remove or change a global actor on a function type.
2021-03-17 00:51:26 -07:00
Pavel Yaskevich
6fe4ffc0ff [ConstraintSystem] Adjust ranking rules associated with Double/CGFloat conversions
Change the conversion rule to favor any number of widenings (CGFloat -> Double)
over even a single narrowing conversion and if there is no way to avoid narrowing
(due to contextual requirements) attempt it as late as possible (the deeper in
the AST that conversion is located the higher its score).

This is a generally better rule when it comes to rounding and information
loss that would result from narrowing conversions.
2021-03-17 00:18:20 -07:00
Pavel Yaskevich
4f5a8d4934 [CSSimplify] Avoid using designated initializer for ArgumentInfo (currently unsupported on Windows) 2021-03-17 00:18:20 -07:00
Pavel Yaskevich
3c731d8748 [TypeChecker] Add a flag to disable Double<->CGFloat implicit conversion 2021-03-17 00:18:19 -07:00
Pavel Yaskevich
24472c9d13 [ConstraintSystem] Allow Double<->CGFloat conversions only if they are AST representable
Allow Double <-> CGFloat conversion if the associated locator is
simplifiable down to a AST node (e.g. expression) this limits
applications of such conversions to e.g. argument-to-parameter,
contextual conversions and forbids conversions in generic arguments,
function result, collection elements etc.
2021-03-17 00:18:18 -07:00
Pavel Yaskevich
bc8d34f2b1 [ConstraintSystem] Increase impact of implicit Double<->CGFloat associated with contextual type
Consider conversion with context to be worse by default
because it means that expression as a whole couldn't get
to the expected type.

This also helps to disambiguate situations with
multiple solutions where one of them has a contextual
mismatch that requires implicit conversion and
another produces correct contextual type but requires
implicit conversion inside to get there.
2021-03-17 00:18:17 -07:00
Pavel Yaskevich
ac6b717f0e [ConstraintSystem] Don't attempt Double-to-CGFloat in coercions/casts 2021-03-17 00:18:16 -07:00
Pavel Yaskevich
c77d0862bb [ConstraintSystem] Record all Double <-> CGFloat conversions performed along a current solver path
This information makes it easier to compute a score for the next
implicit value conversion solver would have to perform.
2021-03-17 00:18:15 -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
6382e7be66 [ConstraintSystem] Favor overloads which match choice type exactly 2021-03-17 00:18:12 -07:00
Pavel Yaskevich
f9eb71ddad [ConstraintSystem] Attempt Double <-> CGFloat implicit conversion only once and re-use results 2021-03-17 00:18:11 -07:00
Pavel Yaskevich
8c6017687a [ConstraintSystem] Limit new implicit conversion to only Double <-> CGFloat 2021-03-17 00:18:10 -07:00
Pavel Yaskevich
9d9a8d6a26 [ConstraintSystem] Start using implicit conversion to/from CGFloat
Add logic to `matchTypes` to insert an implicit conversion to/from
CGFloat type when two nominal types are matched and one of them is
of `CGFloat` type.
2021-03-17 00:18:09 -07:00
Pavel Yaskevich
fac81884fb [CSSimplify] Implement logic for an implicit conversion to/from CGFloat
To support implicit conversion to/from CGFloat type we need to:

- Reference an `init` member on the right-hand side type (either
  CGFloat if we are converting to it, or compatible type if from).

- Add a `ApplicableFunction` constraint to model an implicit
  call to previously referenced initializer with an argument
  of left-hand side type.
2021-03-17 00:18:08 -07:00
Pavel Yaskevich
2b935bab7c [ConstraintSystem] Add conversion restriction to cover implicit conversion from/to CGFloat 2021-03-17 00:18:08 -07:00
Pavel Yaskevich
4ca67f6d27 [ConstraintSystem] Teach getFunctionArgApplyInfo about inout expressions
Currently `getFunctionArgApplyInfo` expects a locator with `ApplyArgToParam`
element to identify location of the argument. `InOutExpr` could only be used
in argument positions but it doesn't have the same locator format as non-inout
arguments, so `getFunctionArgApplyInfo` needs to do some digging in the AST
to retrieve that information.

Resolves: rdar://75146811
2021-03-16 20:12:34 -07:00
Pavel Yaskevich
b6946980fe [Diagnostics] Don't record unwrap fix if ? was used incorrectly
In situations like `""?` or `42?`, let's not record an unwrap fix
since it was incorrect to use `?` in the first place.
2021-03-15 17:16:11 -07:00
Pavel Yaskevich
2a9903dfe4 [Diagnostics] Check whether missing conformance could be fixed by using .rawValue
A lot of operators (and most likely regular functions a well) have
overloads that accept i.e. a generic parameter that conforms to
`StringProtocol`, so the best fix in situations when argument is
a raw representable type would be to check whether `.rawValue`
conforms to the expected protocol and use it if so.

Resolves rdar://problem/75367157
2021-03-15 16:53:45 -07:00
Pavel Yaskevich
dde9fa35e8 [ConstraintSystem] Re-activate constraints if, due to incorrect reference, member type has been bound before base type
If member has been bound before the base and the base was
incorrect at that (e.g. fallback to default `Any` type),
then we need to re-activate all of the constraints
associated with this member reference otherwise some of
the constraints could be left unchecked in inactive state.

This is especially important for key path expressions because
`key path` constraint can't be retired until all components
are simplified.

Resolves: SR-13364
Resolves: rdar://66706980
Resolves: rdar://74711236
2021-03-12 15:31:06 -08:00
Pavel Yaskevich
fb89d11931 [ConstraintLocator] NFC: Adjust name of isForKeyPathComponent to clarify its meaning
New name is `isInKeyPathComponent` since it's implemeneted as a
check for presence of `KeyPathComponent` element in the locator
that covers every sub-element contained in a key path component
as well.
2021-03-12 09:58:54 -08:00
Pavel Yaskevich
15e18e9fbb Merge pull request #36358 from xedin/rdar-74696023
[Diagnostics] Verify that optional evaluation got type from context b…
2021-03-09 12:26:45 -08:00
Anthony Latsis
8242d843cd NFC, Sema: Further propagate the locator through to doesStorageProduceLValue 2021-03-09 17:01:50 +03:00
Pavel Yaskevich
dd70528e6b [Diagnostics] Verify that optional evaluation got type from context before recording a fix
Instead of assuming that there is a mismatch between context and
result of the optional chain, let's actually verify that optional
evaluation expression does have a type before recording a fix.

Resolves: rdar://74696023
2021-03-08 11:48:04 -08:00
Robert Widmann
36c3c14724 Revert "Revert "[ConstraintSystem] Use correct locator when filtering disjunction choices""
This reverts commit b36dbd5bab.
2021-03-06 10:03:24 -08:00
David Zarzycki
b36dbd5bab Revert "[ConstraintSystem] Use correct locator when filtering disjunction choices"
This reverts commit 2df4ba7ae6 from #36300
which conflicts with #36267 and was missed because of classic "time of
test was too old before merging".
2021-03-06 10:02:06 -05:00
Pavel Yaskevich
5c8d64a046 Merge pull request #36300 from xedin/overload-filter-for-members
[ConstraintSystem] Use correct locator when filtering disjunction cho…
2021-03-05 22:19:26 -08:00
Pavel Yaskevich
2df4ba7ae6 [ConstraintSystem] Use correct locator when filtering disjunction choices
`simplifyAppliedOverloads(Constraint *, ...)` wouldn't filter choices
based on labels unless it finds recorded labels in the constraint system
based on the provided locator. `addOverloadSet` used incorrect locator
when calling `simplifyAppliedOverloads` which means that this call was
effectively a no-op.
2021-03-04 17:35:48 -08:00