Commit Graph

2949 Commits

Author SHA1 Message Date
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
Pavel Yaskevich cd08350fe5 [Diagnostics] Prioritize type mismatches over labeling failures for calls
Since labels are considered part of the name, mismatches in labeling
should be invalidate overload choices. Let's prefer an overload with
correct labels but incorrect types over the one with incorrect labels.

This also means that it's possible to restore performance optimizations
related to early filtering in diagnostic mode, which is important for
deeply nested code i.e. SwiftUI views.
2021-03-03 18:50:06 -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
Luciano Almeida d4a52827ea Merge pull request #36208 from LucianoPAlmeida/SR-14280-ambiguity-repr
[SR-14280][CSSimplify] Increase the score when matching functions with different representations for contextual type
2021-03-02 18:39:05 -03:00
Luciano Almeida f5f7204974 [CSSimplify] Increase the score when matching functions with different representations for contextual type 2021-03-01 22:08:38 -03:00
Pavel Yaskevich b487b6956b [ConstraintSystem] Delay inference until let's clear that type variable attempt is successful
Currently bindings where inferred on every `bindTypeVariable` call,
but that's wasteful because not all binds are always correct. To
avoid unnecessary inference traffic let's wait until re-activated
constraints are simplified and notify binding inference about new
fixed type only if all of them are successful.
2021-03-01 10:52:29 -08:00
Holly Borla b7a170cd77 [NFC][ConstraintSystem] Rename applyPropertyWrapperParameter. 2021-02-25 18:35:14 -08:00
Holly Borla db387273f6 [ConstraintSystem] Don't allow dollar prefixes in argument labels unless
the parameter has an attached property wrapper.
2021-02-25 18:35:14 -08:00
Holly Borla 133dec0e05 [ConstraintSystem] Implement implicit property wrapper attribute
inference for closure parameters.
2021-02-25 18:35:14 -08:00
Holly Borla ea3fe03c98 [Property Wrappers] Add a constraint fix and diagnostic for the case
where the programmer tries to pass a projection argument to a wrapped
parameter without 'var projectedValue'.
2021-02-25 18:35:14 -08:00
Holly Borla 31f7b1ac75 [ConstraintSystem] Change the type of an unapplied function reference
when it has property wrapper parameters.

The property wrapper type will be replaced with either the wrapped-value
or projected-value type, depending on the argument label/parameter name,
and CSApply will build a thunk to construct the property wrapper and call
the function.
2021-02-25 18:35:13 -08:00
Holly Borla 19a4f93586 [Property Wrappers] Teach the constraint system to build init(projectedValue:)
calls.
2021-02-25 18:35:13 -08:00
Holly Borla 4bdeed570b [ConstraintSystem] Use a wrapped value placeholder when matching property
wrapper arguments in the constraint system in order to avoid generating
constraints for the argument multiple times.
2021-02-25 18:35:13 -08:00
Holly Borla 8325a52b41 [ConstraintSystem] Teach the constraint system about property wrapper
parameters.
2021-02-25 18:35:13 -08:00
Pavel Yaskevich b9b5bd214b Merge pull request #34523 from xedin/static-member-lookup-on-protocol
[SE-0299][TypeChecker] Allow static member references on protocol metatypes in generic contexts
2021-02-24 10:16:25 -08:00
Luciano Almeida 8b0b89ee34 Merge pull request #36076 from LucianoPAlmeida/SR-2705-ranking-autoclosure
[SR-2705][Sema] Always favor non-autoclosure parameter function overload
2021-02-24 07:00:42 -03:00
Luciano Almeida 7c92f7d5da [CSSimplify] Add autoclosure check for contravariant match and extra test case 2021-02-23 19:39:14 -03:00
Pavel Yaskevich c4ee329991 [CSSimplify] NFC: Adjust to API change - isHole -> isPlaceholder 2021-02-23 12:19:27 -08:00
Pavel Yaskevich cf5e1fff8d [Diagnostics] Add a tailored diagnostic for contextual lookup on protocols without Self requirement 2021-02-23 11:33:11 -08:00
Pavel Yaskevich 2d8f002299 [CSDiagnostics] Adjacent diagnostics for the new rules of static member lookup in generic context 2021-02-23 11:33:11 -08:00
Pavel Yaskevich c44a92ae0b [ConstraintSystem] Implement new rule for static member lookup in generic contexts
Instead of requiring result type of the member to conform to declaring protocol,
as originally proposed by SE-0299, let's instead require `Self` to be bound to
some concrete type in extension that declares a static member.

This would make sure that:

1. Members are only visible on a particular type;
2. There is no ambiguity regarding what base of the member chain is going to be.
2021-02-23 11:33:11 -08:00
Pavel Yaskevich 74d2a6c700 [ConstraintSystem] Limit static member reference on protocol metatype to leading dot syntax only
Do not allow references to a static members with explicitly specified
protocol metatype base, limit that to leading dot syntax only.
2021-02-23 11:33:11 -08:00
Pavel Yaskevich 2b6d24eb99 [ConstraintSystem] NFC: Slightly clarify comment about existence static members on a protocol metatype 2021-02-23 11:33:11 -08:00
Pavel Yaskevich e548366bb5 [TypeChecker] Add tests for static member refs on protocol metatypes feature 2021-02-23 11:33:11 -08:00
Pavel Yaskevich 30f255755b [Diagnostics] NFC: Adjust all diagnostics improved/changed by static member refs on protocol metatypes feature 2021-02-23 11:33:10 -08:00
Pavel Yaskevich dcd746906f [ConstraintSystem] Mark member result type as a potential hole if base was incorrect
If it has been established that member found via static member
lookup on protocol metatype doesn't have correct result type,
chain's result type the should be treated as a hole.
2021-02-23 11:33:10 -08:00
Pavel Yaskevich d1ab178471 [ConstraintSystem] Diagnose conformance failure with base of a static member lookup
Produce a tailored diagnostic when it has been established that
base type of a static member reference on protocol metatype doesn't
conform to a required protocol.
2021-02-23 11:33:10 -08:00
Pavel Yaskevich 5758ae2bcc [ConstraintSystem] Filtering for static member lookup on protocol metatypes
Detect and filter some of the overload choices which can't possibly
match because their result type cannot conform to the expected protocol.
2021-02-23 11:33:10 -08:00
Pavel Yaskevich df76400fe5 [ConstraintSystem] Add a new reason for invalid member reference
This new reason indicates that result type of a static member
doesn't conform to a specific protocol so it can't be referenced
on a protocol metatype.
2021-02-23 11:33:10 -08:00
Pavel Yaskevich 497d42d90e [ConstraintSystem] Add a new constraint which connects base with result of the member chain
The first type represents a result of an unresolved member chain,
and the second type is its base type. This constraint acts almost
like `Equal` but also enforces following semantics:

- It's possible to infer a base from a result type by looking through
  this constraint, but it's only solved when both types are bound.

- If base is a protocol metatype, this constraint becomes a conformance
  check instead of an equality.
2021-02-23 11:33:10 -08:00
Pavel Yaskevich eaabd24746 [CSFix] Add a new fix to diagnose invalid static member refs on protocol metatype 2021-02-23 11:32:24 -08:00
Pavel Yaskevich 7009207491 [ConstraintSystem] Adjust handling of incorrect member references on protocol metatypes
Since it's now possible to refer to static members declared on a protocol
metatype if result type conforms to the protocol we need to adjust failure
detection to identify that conformance failure means and invalid reference
in certain situations.
2021-02-23 11:32:24 -08:00
Pavel Yaskevich 3a145f5464 [CSSimplify] Change equality semantics between base and result of unresolved member ref
Since it's now possible to infer base type of an unresolved member
reference to be a protocol type we need to adjust `matchTypes` to
check conformance instead of type equality between base (represented
by a protocol) and chain's concrete result type.
2021-02-23 11:32:24 -08:00
Pavel Yaskevich b05678f18e [ConstraintSystem] Allow static member references on protocol metatypes 2021-02-23 11:31:43 -08:00
Luciano Almeida ef86e9edbe [ConstraintSystem] Adding new score kind SK_FunctionToAutoClosureConversion to increase impact of a function to autoclosure 2021-02-23 07:21:58 -03:00
Luciano Almeida 21a1d908b8 [CSSimplify] Increase score for closure argument when in a call autoclosure context to avoid ambiguity 2021-02-23 00:49:42 -03:00
Nathan Hawes 44e09bc246 Merge branch 'main' into dont-allow-missing-args-if-trailing-and-not-function-type 2021-02-23 12:53:50 +10:00
Nathan Hawes cdcaa110e8 [CodeCompletion][Sema] Refine logic that ignores missing arguments after the code completion position when solving
If the completion loc was in a trailing closure arg, it only makes sense to
ignore later missing args if they have function type, as there's no way for the
user to complete the call for overload being matched otherwise.

Also fix a bug where we were incorrectly penalizing solutions with holes that
were ultimately due to missing arguments after the completion position.

Resolves rdar://problem/72412302
2021-02-20 08:59:47 +10:00
Frederick Kellison-Linn e4ea1678dc Rename HoleType to PlaceholderType
HoleType basically served the same purpose as PlaceholderType. This commit unifies the two.
2021-02-16 22:59:19 -05:00
Frederick Kellison-Linn d78d95ef0d [AST] Introduce PlaceholderType and ThePlaceholderType singleton 2021-02-16 22:59:18 -05:00