Commit Graph

29 Commits

Author SHA1 Message Date
Anthony Latsis
7f6d3bcd41 ASTPrinter: Turn on explicit any printing for everything and remove the option to disable it 2023-05-13 02:55:49 +03:00
Anthony Latsis
fa3a95e7b8 Gardening: Migrate test suite to GH issues: refactoring 2022-09-02 01:44:24 +03:00
Hamish Knight
9b493970aa [Aync Refactoring] Better handle comment trailing whitespace
Use `getCommentRange` to trim off the comment's
trailing whitespace, avoiding outputting
extraneous empty lines.

rdar://82072147
2021-09-06 17:51:39 +01:00
Hamish Knight
f58c62c02e [test] Use %refactor-check-compiles in more places
Update a bunch of async refactoring tests to use
%refactor-check-compiles in more cases.
2021-08-18 13:21:06 +01:00
Ben Barham
5bd2d0b932 [Refactoring] Fix invalid legacy body for Result<Void, ...> handler
When adding an async alternative for a function with a
`(Result<Void, ...>) -> Void` completion handler, the legacy body was
adding a call to the handler with `.success(result)`. We don't assign to
`result` for `Void` handlers, so pass `()` instead.

Resolves rdar://81829392
2021-08-12 21:04:18 +10:00
Ben Barham
f6a8dab93d [Refactoring] Use known alternative in async refactoring if one exists
Update the async refactorings to use the name from the async alternative
if one is known, rather than always assuming the name matches the
synchronous function.

Note that this could also be used to determine whether results remain
optional or not, but that has been left for a future patch.

Resolves rdar://80612521
2021-08-03 08:46:44 +10:00
Ben Barham
e7e9b57051 Replace @completionHandlerAsync with @available(*, renamed:)
Instead of a new attribute `@completionHandlerAsync`, allow the use of
the existing `renamed` parameter of `@available` to specify the
asynchronous alternative of a synchronous function.

No errors will be output from invalid names as `@completionHandlerAsync`
had, but if a function is correctly matched then it will be used to
output warnings when using the synchronous function in an asynchronous
context (as before).

Resolves rdar://80612731
2021-07-29 09:14:44 +10:00
Ben Barham
fabb02100f [Test] Add @escaping to async refactoring tests
The async refactorings ignore whether a completion handler had
`@escaping` or not. In preparation of fixing this, fix up all functions
to have `@escaping` for their completion handler parameter.

Also some small miscellaneous fixes in order to reduce the number of
warnings output on test failures and also the addition of `REQUIRES:
concurrency` on all tests.
2021-07-24 09:53:17 +10:00
Hamish Knight
1ebb1fd2f5 [Async Refactoring] Add parens around custom error cast
When passing a forwarded error to a CustomError?
completion handler parameter, wrap the cast in a
set of parentheses to silence a warning in the
refactored code.

rdar://80409905
2021-07-22 23:20:24 +01:00
Hamish Knight
007b91cf7b [Async Refactoring] Update to use Task's init
Use the Task initializer instead of the top-level
`async` function to match the latest Concurrency
API.

rdar:80888385
2021-07-21 11:40:35 +01:00
Hamish Knight
a6bb071776 [ASTWalker] Walk ForEachStmt in source order
Make sure to visit the sequence expr before the
the where clause as the refactoring logic expects
nodes to be visited in source order.

rdar://78781061
2021-06-03 11:26:58 +01:00
Alex Hoppen
58be2bfbea [Refactoring] Don't crash when converting a function to async that contains a call to init
We were trying to retrieve the name of all function calls in the body using `getBaseIdentifier`. But calls to `init` don’t have a base identifier, just a `DeclBaseName` (which is special). Work with the `DeclBaseName` internally to prevent the crash.

Fixes rdar://78024731 [SR-14637]
2021-05-31 12:13:07 +02:00
Hamish Knight
8cb319b640 [Refactoring] Preserve comments in async transform
Previously we would drop comments between nodes in
a BraceStmt, as we printed each node out individually.
To remedy this, always make sure we scan backwards
to find any preceding comments attached to a node,
and also keep track of any SourceLocs which we
don't print, but may have comments attached which
we want to preserve.

rdar://77401810
2021-05-13 14:16:27 +01:00
Ben Barham
8569c8a51b [Refactoring] Avoid redeclarations or shadowing in async refactored code
When converting a call or function, rename declarations such that
redeclaration errors and shadowing are avoided. In some cases this will
be overly conservative, but since any renamed variable can be fixed with
edit all in scope, this is preferred over causing redeclaration errors
or possible shadowing.

Resolves rdar://73973517
2021-05-13 17:48:41 +10: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
Ben Barham
398124c61a [Refactoring] Only unwrap optionals if the handler has an optional error
Resolves rdar://73973459
2021-05-06 10:27:34 +10:00
Ben Barham
058613dd42 [Refactoring] Add tests for convert to async
Convert to async and add async alternative differ when there is no/an
invalid completion handler. In those cases, also check convert to async.
2021-05-06 10:27:34 +10:00
Alex Hoppen
98e6680c85 [Refactoring] When adding an async alternative refactor the old method to call the async method using detach
Instead of leaving two copies of the same implementation, rewrite the old method with the completion handler to call the newly added `async` method.

Resolves rdar://74464833
2021-05-04 12:06:27 +02:00
Alex Hoppen
b2378a401e [Refactoring] Convert completion handler when converting function to async
Convert function to async currently only adds "async" to the function and runs the convert call refactoring on the body.

This was intentional, but it turns out to be somewhat confusing. Instead, run the same refactoring as the add async alternative refactoring but just replace rather than add.

Resolves rdar://77103049
2021-04-30 12:53:22 +02:00
Hamish Knight
c1e4dc3d82 [Refactoring] Improve Void handling for async conversion
When converting a function with a completion handler
that has a Void success parameter, e.g
`(Void?, Error?) -> Void`, or more likely a
`Result<Void, Error>` parameter, make sure to omit
the `-> Void` from the resulting async function
conversion.

In addition, strip any Void bindings from an async
function call, and any explicit Void return values
from inside the async function.

Resolves rdar://75189289
2021-04-30 01:09:04 +01:00
Alex Hoppen
1bd6086cbf Merge pull request #37071 from ahoppen/pr/block-convention-to-async
[Refactoring] Support refactoring to async if callback is `@convention(block)`
2021-04-28 11:40:19 +02:00
Alex Hoppen
f17fe868c6 [Refactoring] Support refactoring to async if callback is @convention(block)
We already have special logic to extrac the closure for closures with capture lists, add the same kind of logic for closures that are marked `@convention(block)` etc.

Resolves rdar://75301524 [SR-14328]
2021-04-27 22:25:57 +02:00
Alex Hoppen
9d62f9d4db [Refactoring] Fix crash when refactoring protocol requirement to async
When a function declaration has no body (e.g. because it’s a protocol requirement), we construct the range to replace by the `async` keyword as follows:
- Start: One character after the closing `)` (or potentially the `throws` keyword if it exists)
- End: Last token in the function declaration

Since the last token in the function declaration is the `)`, we end up with a range that has `End < Start`, which crashes when trying to print the range.

If the function has no body, we should just use the range’s start location as the end location to construct an empty range.

Fixes rdar://76677035
2021-04-22 11:56:34 +02:00
Ben Barham
0717448611 [Refactoring] Handle default arguments when converting call to async
Default arguments were still being visited when converting the call,
adding extra commas to the converted call. Skip over them.

Resolves rdar://74248990
2021-02-12 08:18:55 +10:00
Ben Barham
384b309f89 [Refactoring] Only convert calls with handlers that have completion-like names
Still convert the call if it was requested directly - only check the name
when converting a whole function. Once we have an attribute, we should
use that instead.
2021-02-12 07:43:57 +10:00
Ben Barham
bc5229701e [Refactoring] Handle closures with capture list in async refactoring
Resolves rdar://74064061
2021-02-12 07:43:57 +10:00
Ben Barham
783c177956 [Refactoring] Fix stack use after free in new async refactorings
Resolves rdar://73984220
2021-02-05 15:32:35 +10:00
Rintaro Ishizaki
f67b5d9917 [Tests] Disable refactoring/ConvertAsync in ASAN
ASAN detected stack-use-after-scope errors. Disable tests while fixing.

rdar://problem/73984220
2021-02-04 10:17:49 -08:00
Ben Barham
a9073b0922 [Refactoring] Add async refactorings
Adds three refactorings intended to help users migrate their existing
code to use the new async language features:
  1. Convert call to use async alternative
  2. Convert function to async
  3. Add async alternative function

A function is considered to have an async alternative if it has a void
return type and has a void returning closure as its last parameter. A
method to explicitly mark functions as having an async alternative may
be added to make this more accurate in the future (required for eg.
a warning about a call to the non-async version of a function in an
async context).

(1) converts a call to use the new `await` async language syntax. If the
async alternative throws, it will also add `try`. The closure itself is
hoisted out of the call, see the comments on
`AsyncConversionStringBuilder` for specifics.

(2) converts a whole function to `async`, using (1) to convert any calls
in the function to their async alternatives. (3) is similar to (2), but
instead *adds* a function and replaces calls to its
completion/handler/callback closure parameter with `return` or `throws`.

Resolves rdar://68254700
2021-02-03 15:54:46 +10:00