Commit Graph

1988 Commits

Author SHA1 Message Date
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
Luciano Almeida
d55eed46e8 [Sema][CSApply] Moving some checked cast diagnostics to a fix format (#34980)
* [Sema] Implementing is runtime check always true diagnostic as a fix

* [AST] Implement getWithoutThrows on function type

* [CSSimplify] Detect that checked cast types conversion is always true and record warning fix

* [test] Some additional test cases for SR-13789

* [Sema] Fixing typo on fix name

* [Sema] Move and adjust the ConditionalCast diagnostics to the fix format

* [Sema] Remove some checked cast diagnostics from check constraints and move to fix

* [Sema] Renaming checked cast coercible types fix

* [Sema] Some adjustments and rewrite on the logic for downcast record fix

* [Sema] Move logic of runtime function type to AllowUnsupportedRuntimeCheckedCast::attempt

* [Sema] Abstract checked cast fix logic to static function and minor adjustments

* [Sema] Renamings from review
2021-02-10 08:31:06 -03:00
Doug Gregor
f13167d9d1 Merge pull request #35628 from DougGregor/concurrent-function-types
[Concurrency] Add @concurrent function types
2021-01-29 08:34:59 -08:00
Pavel Yaskevich
a7e8034396 [ConstraintSystem] NFC: Print type variables embedded in common result type 2021-01-28 14:01:29 -08:00
Doug Gregor
b77ba9a77f [Concurrency] Infer @concurrent on closures from contextual type 2021-01-27 16:00:39 -08:00
Doug Gregor
ba8819eb58 [Concurrent] Introduce concurrent function types.
Introduce `@concurrent` attribute on function types, including:
* Parsing as a type attribute
* (De-/re-/)mangling for concurrent function types
* Implicit conversion from @concurrent to non-@concurrent
- (De-)serialization for concurrent function types
- AST printing and dumping support
2021-01-27 14:22:32 -08:00
Pavel Yaskevich
fda3dc5127 Merge pull request #35607 from xedin/type-vars-accepts-set
[AST] Adjust `TypeBase::getTypeVariables` to accept a set
2021-01-27 09:51:45 -08:00
Pavel Yaskevich
b03dc63634 [AST] Adjust TypeBase::getTypeVariables to accept a set
Currently the pattern is to collect the type variables and then unique
them. Instead of asking clients to do uniquing, let's just accept a set
as an argument.
2021-01-26 18:13:34 -08:00
Varun Gandhi
86b123a35c [Sema] Diagnose type error on Clang type mismatch.
Fixes rdar://71904525.
2021-01-25 18:51:13 -08:00
swift-ci
a86430b937 Merge pull request #35580 from LucianoPAlmeida/SR-14050 2021-01-25 18:20:25 -08:00
Luciano Almeida
18e604a750 [Sema] Removing check for argument locator at subtype validation at matchFunctionRepresentations 2021-01-25 20:35:44 -03:00
Holly Borla
1ef2174bc2 Merge pull request #35025 from hborla/generic-overload-search-space
[ConstraintSystem] Implement heuristics for pruning the generic operator overload search space
2021-01-22 09:12:50 -08:00