Skip to content

Commit a9073b0

Browse files
committed
[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
1 parent 83915cc commit a9073b0

File tree

14 files changed

+2834
-14
lines changed

14 files changed

+2834
-14
lines changed

include/swift/AST/DiagnosticsRefactoring.def

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,23 @@ WARNING(mismatched_rename, none, "the name at the given location cannot be renam
5454

5555
ERROR(no_insert_position, none, "cannot find inserting position", ())
5656

57+
ERROR(missing_callback_arg, none, "cannot refactor as callback closure argument missing", ())
58+
59+
ERROR(mismatched_callback_args, none, "cannot refactor as callback arguments do not match declaration", ())
60+
61+
ERROR(unknown_callback_conditions, none, "cannot refactor complex if conditions", ())
62+
63+
ERROR(mixed_callback_conditions, none, "cannot refactor mixed nil and not-nil conditions", ())
64+
65+
ERROR(callback_multiple_bound_names, none, "cannot refactor when multiple names bound to single declaration, had '%0' and found '%1'", (StringRef, StringRef))
66+
67+
ERROR(callback_with_fallthrough, none, "cannot refactor switch with fallthrough", ())
68+
69+
ERROR(callback_with_default, none, "cannot refactor switch with default case", ())
70+
71+
ERROR(callback_multiple_case_items, none, "cannot refactor switch using a case with multiple items", ())
72+
73+
ERROR(callback_where_case_item, none, "cannot refactor switch using a case with where clause", ())
74+
5775
#define UNDEFINE_DIAGNOSTIC_MACROS
5876
#include "DefineDiagnosticMacros.h"

include/swift/AST/KnownStdlibTypes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ KNOWN_STDLIB_TYPE_DECL(KeyedEncodingContainer, NominalTypeDecl, 1)
9191
KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
9292
KNOWN_STDLIB_TYPE_DECL(RangeReplaceableCollection, ProtocolDecl, 1)
9393

94+
KNOWN_STDLIB_TYPE_DECL(Result, NominalTypeDecl, 2)
95+
9496
#undef KNOWN_STDLIB_TYPE_DECL

include/swift/IDE/RefactoringKinds.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ CURSOR_REFACTORING(MemberwiseInitLocalRefactoring, "Generate Memberwise Initiali
5454

5555
CURSOR_REFACTORING(AddEquatableConformance, "Add Equatable Conformance", add.equatable.conformance)
5656

57+
CURSOR_REFACTORING(ConvertCallToAsyncAlternative, "Convert Call to Async Alternative", convert.call-to-async)
58+
59+
CURSOR_REFACTORING(ConvertToAsync, "Convert Function to Async", convert.func-to-async)
60+
61+
CURSOR_REFACTORING(AddAsyncAlternative, "Add Async Alternative", add.async-alternative)
62+
5763
RANGE_REFACTORING(ExtractExpr, "Extract Expression", extract.expr)
5864

5965
RANGE_REFACTORING(ExtractFunction, "Extract Method", extract.function)

include/swift/IDE/Utils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,9 @@ class SourceEditJsonConsumer : public SourceEditConsumer {
559559

560560
/// Outputs replacements to `OS` in the form
561561
/// ```
562-
/// // </path/to/file> [startLine:startCol, endLine:endCol)
562+
/// // </path/to/file> startLine:startCol -> endLine:endCol
563+
/// replacement
563564
/// text
564-
/// to
565-
/// replace
566565
///
567566
/// ```
568567
class SourceEditTextConsumer : public SourceEditConsumer {

0 commit comments

Comments
 (0)