Commit Graph

68066 Commits

Author SHA1 Message Date
Alex Hoppen
65b9a1c1fc Merge pull request #37314 from ahoppen/pr/refactor-to-async-variable-callback
[Refactoring] Support refactoring calls to async if a variable or function is used as completion handler
2021-05-12 16:57:34 +02:00
Pavel Yaskevich
d500f9cc8a Merge pull request #37373 from xedin/rdar-76058892
[CSSimplify] Increase fix impact when passing closure to a non-function type parameter
2021-05-12 00:30:17 -07:00
Alex Hoppen
32ceb24b6c [Refactoring] Promote call to refactored completion handlers to return
When a function’s completion handler is being passed to another function and the function that the completion handler is being declared in is converted to async, replace the call to the completion handler by a `return` statement.

For example:
```swift
func foo(completion: (String) -> Void) {
  bar(completion)
}
```

becomes
```swift
func foo() async -> String {
  return await bar()
}
```

Previously, we were calling the completion handler, which no longer exists in the newly created `async` function.
2021-05-12 09:26:25 +02:00
Holly Borla
c297070106 [Diagnostics] Always use the parameter name for closure parameter diagnostics,
because the $ prefix does not indicate that the parameter is anonymous.
2021-05-11 18:30:27 -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
fredriss
3ed11125f3 Merge pull request #37325 from fredriss/async-task-id
[Concurrency] Add a unique Task ID to AsyncTask
2021-05-11 17:59:47 -07:00
Slava Pestov
161c837a9b AST: Avoid expensive calls to getAttachedPropertyWrappers()
Check for the presence of a CustomAttr first, before calling into the
request evaluator.

Also clean up the logic a tiny bit.
2021-05-11 18:42:26 -04:00
Pavel Yaskevich
c346bbd514 [Diagnostics] Point out where anonymous closure parameters are used in a multi-statement closure 2021-05-11 15:07:44 -07:00
Pavel Yaskevich
493ad7c8fa [Diagnostics] NFC: Re-phrase reference to unused closure parameter in extraneous arguments diagnostic 2021-05-11 15:07:17 -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
Dario Rexin
29883c806f [Sema] Refactor enum Codable synthesis (#36685) 2021-05-11 13:40:39 -07:00
Slava Pestov
56da3f45b5 AST: Add a fast path for IterableDeclContext::getBodyFingerprint()
getParsedMembers() on imported and deserialized contexts has to pull
in all the members. This is wasteful. Imported contexts do not have
fingerprints at all, and fingerprints of deserialized contexts  are
serialized separately.

The fast path handles these cases, only calling down to
getParsedMembers() if the parent FileUnit is a SourceFile.
2021-05-11 13:26:11 -04:00
Pavel Yaskevich
ddfaf80181 Merge pull request #37347 from xedin/rdar-77700261
[CSFix] Suppress ambiguity warning about non-`Optional` base with sta…
2021-05-11 10:13:14 -07:00
fwcd
ea86221d41 Update LocalizationFormat to use diagnosticIDStringFor 2021-05-11 19:06:50 +02:00
Fred Riss
bbda706393 [Concurrency] Add a unique Task ID to AsyncTask
This commit changes JobFlags storage to be 32bits, but leaves the runtime
API expressed in terms of size_t. This allows us to pack an Id in the
32bits we freed up.

The offset of this Id in the AsyncTask is an ABI constant. This way
introspection tools can extract the currently running task identifier
without any need for special APIs.
2021-05-11 08:28:17 -07:00
Alex Hoppen
921443e8ab [Refactoring] Support creation of async legacy bodies even if a parameter is not optional
If the parameter is not an `Optional`, add a placeholder instead that the user can fill with a sensible default value.
2021-05-11 15:49:55 +02:00
Alex Hoppen
dd978cca0b [Refactoring] Support refactoring calls to async if a variable or function is used as completion handler
Previously, we only supported  refactoring a function to call the async alternative if a closure was used for the callback parameter. With this change, we also support calling a arbitrary function (or variable with function type) that is passed to the completion handler argument.

The implementation basically re-uses the code we already have to create the legacy function’s body (which calls the newly created async version and then forwards the arguments to the legacy completion handler).

To describe the completion handler that the result is being forwarded to, I’m also using `AsyncHandlerDesc`, but since the completion handler may be a variable, it doesn’t necessarily have an `Index` within a function decl that declares it. Because of this, I split the `AsyncHandlerDesc` up into a context-free `AsyncHandlerDesc` (without an `Index`) and `AsyncHandlerParamDesc` (which includes the `Index`). It turns out that `AsyncHandlerDesc` is sufficient in most places.

Resolves rdar://77460524
2021-05-11 15:48:24 +02:00
Alex Hoppen
420afdc415 Merge pull request #37344 from ahoppen/pr/no-unresolved-type-variables
[Sema] Remove `TypeCheckExprFlags::AllowUnresolvedTypeVariables`
2021-05-11 13:23:00 +02:00
Alex Hoppen
499844eb20 Merge pull request #37341 from ahoppen/pr/check-null-getresulttype
[AST] Return `null` type in `AbstractClosureExpr::getResultType` if `getType` is `null`
2021-05-11 13:10:04 +02:00
Pavel Yaskevich
2fdebccbd7 Merge pull request #37342 from xedin/rdar-77466241
[ResultBuilders] Diagnose pre-check errors inline
2021-05-10 15:53:26 -07:00
Victoria Mitchell
81bc80d565 add sourceOrigin field for symbols implementing remote protocol requirements
rdar://77626724
2021-05-10 16:41:50 -06:00
Rintaro Ishizaki
861bbcc5d6 Merge pull request #37330 from rintaro/ide-completion-rdar75620636
[CodeCompletion] Make object literals optional
2021-05-10 13:47:10 -07:00
Pavel Yaskevich
a3fe65d87e [CSFix] Suppress ambiguity warning about non-Optional base with static member lookup
Disable non-Optional "assumption" warning for ambiguities related to a
static member lookup in generic context because it's possible to declare
a member with the same name on a concrete type and in an extension of a
protocol that type conforms to e.g.:

```swift
struct S : P { static var test: S { ... }

extension P where Self == S { static var test: { ... } }
```

And use that in an optional context e.g. passing `.test`
to a parameter of expecting `S?`.

Resolves: rdar://77700261
2021-05-10 12:52:26 -07:00
Alex Hoppen
4566bf3136 [Sema] Remove TypeCheckExprFlags::AllowUnresolvedTypeVariables
Remove the `TypeCheckExprFlags::AllowUnresolvedTypeVariables` flag, fixing another occurance of rdar://76686564 (https://github.com/apple/swift/pull/37309)

This does not break anything in the test suite, so I think the removal of the flag is fine.

Resolves rdar://76686564 and rdar://77659417
2021-05-10 20:11:43 +02:00
Pavel Yaskevich
4b0d9a2509 [ResultBuilders] Diagnose pre-check errors inline
Not all of the pre-check errors could be diagnosed by re-running
`PreCheckExpression` e.g. key path expressions are mutated to
a particular form after an error has been produced.

To make the behavior consistent, let's allow pre-check to emit
diagnostics and unify pre-check and constraint generation fixes.

Resolves: rdar://77466241
2021-05-10 11:06:58 -07:00
Alex Hoppen
1e8f4c133c [AST] Return null type in AbstractClosureExpr::getResultType if getType is null
If `getType` returns a `null` type, also return a `null` type in `AbstractClosureExpr::getResultType` instead of crashing.

Fixes rdar://77565983
2021-05-10 15:25:00 +02:00
eeckstein
cf2c052628 Merge pull request #37315 from eeckstein/fix-abc-opt
ArrayBoundCheckOpts: introduce a limit for the maximum dominator tree recursion depth
2021-05-10 09:15:04 +02:00
Slava Pestov
e91b305b94 Merge pull request #37154 from slavapestov/new-redundant-requirements-algorithm-part-2
GSB: New algorithm for computing redundant requirements
2021-05-08 16:50:27 -04:00
fwcd
754d19910e Expose diagnostic category via SourceKit 2021-05-08 19:39:09 +02:00
John McCall
d7a89a7108 Merge pull request #37328 from rjmccall/di-box-scaling
In DI, cache whether a memory object is a box
2021-05-08 13:37:03 -04:00
fwcd
9a86d2ecd2 Add diagnostic categories indicating deprecations and non-usage 2021-05-08 16:19:59 +02:00
fwcd
204827c6ab Update default value for unknown diagnostic IDs 2021-05-08 16:04:56 +02:00
swift-ci
7f4a1d5e20 Merge pull request #37334 from DougGregor/implicit-self-capture-feature 2021-05-07 22:22:19 -07:00
Doug Gregor
9411baab9f Add feature for _implicitSelfCapture attribute.
Fixes rdar://77681413.
2021-05-07 19:53:31 -07:00
Rintaro Ishizaki
5ea60e8044 Merge pull request #37300 from rintaro/ide-completion-rdar76355581
[CodeCompletion] Complete pattern introducer for 'for'
2021-05-07 18:41:04 -07:00
Rintaro Ishizaki
fc0e748d64 Merge pull request #37324 from rintaro/ide-completion-rdar75358153
[CodeCompletion] Reset 'hasSingleExpressionBody' when setting function body
2021-05-07 16:46:22 -07:00
Rintaro Ishizaki
da96ef1f51 [CodeCompletion] Make object literals optional
For example, non-Darwin platforms probably don't want
`#colorLiteral(red:green:blue":alpha:)` and `#imageLiteral(named:)`.
Add an completion option to include them, which is "on" by default.

rdar://75620636
2021-05-07 16:27:13 -07:00
John McCall
12a4d471c0 In DI, cache whether a memory object is a box.
Boxes tend to have a small number of uses, so frequently finding the
unique projection isn't too bad.  Non-boxes, however, can have a large
number of uses: for example, a class instance has expected uses
proportionate to the number of stored properties.  So if we do a linear
scan of the uses on a non-box instruction, we'll scale quadratically.

Fixes SR-14532.
2021-05-07 18:50:00 -04:00
Slava Pestov
e45b566642 GSB: Try harder to prove derivation via redundant requirements
Consider a signature with a conformance requirement, and two
identical superclass requirements, both of which make the
conformance requirement redundant.

The conformance will have two requirement sources, the explicit
source and a derived source based on one of the two superclass
requirements, whichever one was processed first.

If we end up marking the *other* superclass requirement as
redundant, we would incorrectly conclude that the conformance
was not redundant. Instead, if a derived source is based on a
redundant requirement, we can't just discard it right away;
instead, we have to check if that source could have been
derived some other way.
2021-05-07 18:43:57 -04:00
Slava Pestov
3c136c00ea GSB: Remove obsolete hack for well-founded requirements
This used to be necessary to handle this case:

    protocol P1 {
      associatedtype A
    }

    protocol P2 {
      associatedtype A : P1
    }

    func foo<T : P1>(_: T) where T.A : P2,  T.A.A == T {}

We don't want to mark 'T : P1' as redundant, even though it could
be recovered from T.A.A, because it uses the requirement T.A, and
we cannot recover the type metadata for T.A without the witness
table for T : P1.

Now this is handled via a more general mechanism, so we no longer
need this hack.
2021-05-07 18:43:52 -04:00
Slava Pestov
f6359e9d6f GSB: Replace 'self derived' check with a more sound notion of well-foundedness
Consider this example:

    protocol P1 {
      associatedtype A : P2
    }

    protocol P2 {
      associatedtype A
    }

    func foo<T : P2>(_: T) where T.A : P1, T.A.A == T {}

We cannot drop 'T : P2', even though it can be derived from
T.A.A == T and Self.A : P2 in protocol P1, because we actually
need the conformance T : P2 in order to recover the concrete
type for T.A.

This was handled via a hack which would ensure that the
conformance path (T.A : P1)(Self.A : P2) was not a candidate
derivation for T : P2, because T : P2 is one of the conformances
used to construct the subject type T.A in the conformance path.

This hack did not generalize to "cycles" of length greater than 1
however:

    func foo<T : P2, U : P2>(_: T, _: U)
      where T.A : P1, T.A.A == U, U.A : P1, U.A.A == T {}

We used to drop both T : P2 and U : P2, because each one had a
conformance path (U.A : P1)(Self.A : P2) and (T.A : P1)(Self.A : P2),
respectively; however, once we drop T : P2 and U : P2, these two
paths become invalid for the same reason that the concrete type
for T.A and U.A can no longer be recovered.

The correct fix is to recursively visit the subject type of each
base requirement when evaluating a conformance path, and not
consider any requirement whose subject type's parent type cannot
be recovered. This proceeds recursively.

In the worst case, this algorithm is exponential in the number of
conformance requirements, but it is not always exponential, and
a generic signature is not going to have a large number of
conformance requirements anyway (hopefully). Also, the old logic
in getMinimalConformanceSource() wasn't cheap either.

Perhaps some clever minimization can speed this up too, but I'm
going to focus on correctness first, before looking at performance
here.

Fixes rdar://problem/74890907.
2021-05-07 18:43:52 -04:00
Slava Pestov
2d77260d1a GSB: Clean up and comment redundant requirement algorithm some more 2021-05-07 18:43:52 -04:00
Slava Pestov
aa865a2133 GSB: Stop computing 'derived via concrete' sources 2021-05-07 18:43:52 -04:00
Slava Pestov
710621c2eb GSB: Remove checkConformanceConstraints() 2021-05-07 18:43:52 -04:00
Slava Pestov
a10c5ed8a6 GSB: Remove checkSuperclassConstraints() and checkLayoutConstraints() 2021-05-07 18:43:52 -04:00
Slava Pestov
4bf239827e GSB: Fix off-by-one error when merging layout requirements of two equivalence classes
Literally an off-by-one error -- we were skipping over the first
requirement and not copying it over. I think this is because the
updateLayout() call used to be addLayoutRequirementDirect(),
which would add the first layout constraint, and I forgot to
remove the '+ 1' when I refactored this.
2021-05-07 18:43:52 -04:00
Slava Pestov
d311549246 GSB: Rebuild requirement signatures after dropping redundant conformance requirements
This fixes a regression from the new redundant requirements algorithm
and paves the way for removing the notion of 'derived via concrete'
requirements.
2021-05-07 18:43:52 -04:00
Slava Pestov
a49b93c61a GSB: New redundant requirements algorithm
This rewrites the existing redundant requirements algorithm
to be simpler, and fix an incorrect behavior in the case where
we're building a protocol requirement signature.

Consider the following example:

    protocol P {
      associatedtype A : P
      associatedtype B : P where A.B == B
    }

The requirement B : P has two conformance paths here:

    (B : P)
    (A : P)(B : P)

The naive redundancy algorithm would conclude that (B : P) is
redundant because it can be derived as (A : P)(B : P). However,
if we drop (B : P), we lose the derived conformance path
as well, since it involves the same requirement (B : P).

The above example actually worked before this change, because we
handled this case in getMinimalConformanceSource() by dropping any
derived conformance paths that involve the requirement itself
appearing in the "middle" of the path.

However, this is insufficient because you can have a "cycle"
here with length more than 1. For example,

    protocol P {
      associatedtype A : P where A == B.C
      associatedtype B : P where B == A.C
      associatedtype C : P where C == A.B
    }

The requirement A : P has two conformance paths here:

   (A : P)
   (B : P)(C : P)

Similarly, B : P has these two paths:

   (B : P)
   (A : P)(C : P)

And C : P has these two paths:

   (C : P)
   (A : P)(B : P)

Since each one of A : P, B : P and C : P has a derived conformance
path that does not involve itself, we would conclude that all three
were redundant. But this was wrong; while (B : P)(C : P) is a valid
derived path for A : P that allows us to drop A : P, once we commit to
dropping A : P, we can no longer use the other derived paths
(A : P)(C : P) for B : P, and (A : P)(B : P) for C : P, respectively,
because they involve A : P, which we dropped.

The problem is that we were losing information here. The explicit
requirement A : P can be derived as (B : P)(C : P), but we would
just say that it was implied by B : P alone.

For non-protocol generic signatures, just looking at the root is
still sufficient.

However, when building a requirement signature of a self-recursive
protocol, instead of looking at the root explicit requirement only,
we need to look at _all_ intermediate steps in the path that involve
the same protocol.

This is implemented in a new getBaseRequirements() method, which
generalizes the operation of getting the explicit requirement at
the root of a derived conformance path by returning a vector of
one or more explicit requirements that appear in the path.

Also the new algorithm computes redundancy online instead of building
a directed graph and then computing SCCs. This is possible by
recording newly-discovered redundant requirements immediately,
and then using the set of so-far-redundant requirements when
evaluating a path.

This commit introduces a small regression in an existing test case
involving a protocol with a 'derived via concrete' requirement.
Subsequent commits in this PR fix the regression and remove the
'derived via concrete' mechanism, since it is no longer necessary.

Fixes https://bugs.swift.org/browse/SR-14510 / rdar://problem/76883924.
2021-05-07 18:43:52 -04:00
Slava Pestov
ac3cbe5858 GSB: Add a counter and clean up getConformanceAccessPath() a bit 2021-05-07 18:43:52 -04:00
QuietMisdreavus
08d1c33e6f Merge pull request #37278 from apple/QuietMisdreavus/default-relation
[SymbolGraph] add "memberOf" relations for remote protocol implementations
2021-05-07 16:19:18 -06:00