Skip to content

Commit c8c604a

Browse files
committed
[Async Refactoring] Cosmetic Improvements to "Add Async Wrapper" Refactoring
Two mostly-cosmetic change to the "Add Async Wrapper" refactoring - Rename the continuation from `cont` to `continuation` and error from `err` to `error` to better match Swifts naming guidelines - Add assertions that all result parameters are `nil` if an error is passed to the completion handler. Resolves rdar://80172152
1 parent 4cd2bb6 commit c8c604a

File tree

2 files changed

+79
-61
lines changed

2 files changed

+79
-61
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5889,17 +5889,18 @@ class AsyncConverter : private SourceEntityWalker {
58895889

58905890
OS << "await ";
58915891

5892-
// withChecked[Throwing]Continuation { cont in
5892+
// withChecked[Throwing]Continuation { continuation in
58935893
if (TopHandler.HasError) {
58945894
OS << "withCheckedThrowingContinuation";
58955895
} else {
58965896
OS << "withCheckedContinuation";
58975897
}
5898-
OS << " " << tok::l_brace << " cont " << tok::kw_in << "\n";
5898+
OS << " " << tok::l_brace << " continuation " << tok::kw_in << "\n";
58995899

59005900
// fnWithHandler(args...) { ... }
5901-
auto ClosureStr = getAsyncWrapperCompletionClosure("cont", TopHandler);
5902-
addForwardingCallTo(FD, TopHandler, /*HandlerReplacement*/ ClosureStr);
5901+
auto ClosureStr =
5902+
getAsyncWrapperCompletionClosure("continuation", TopHandler);
5903+
addForwardingCallTo(FD, TopHandler, /*HandlerReplacement=*/ClosureStr);
59035904

59045905
OS << tok::r_brace << "\n"; // end continuation closure
59055906
OS << tok::r_brace << "\n"; // end function body
@@ -5963,21 +5964,21 @@ class AsyncConverter : private SourceEntityWalker {
59635964
std::string OutputStr;
59645965
llvm::raw_string_ostream OS(OutputStr);
59655966

5966-
OS << " " << tok::l_brace; // start closure
5967+
OS << tok::l_brace; // start closure
59675968

59685969
// Prepare parameter names for the closure.
59695970
auto SuccessParams = HandlerDesc.getSuccessParams();
59705971
SmallVector<SmallString<4>, 2> SuccessParamNames;
59715972
for (auto idx : indices(SuccessParams)) {
5972-
SuccessParamNames.emplace_back("res");
5973+
SuccessParamNames.emplace_back("result");
59735974

59745975
// If we have multiple success params, number them e.g res1, res2...
59755976
if (SuccessParams.size() > 1)
59765977
SuccessParamNames.back().append(std::to_string(idx + 1));
59775978
}
59785979
Optional<SmallString<4>> ErrName;
59795980
if (HandlerDesc.getErrorParam())
5980-
ErrName.emplace("err");
5981+
ErrName.emplace("error");
59815982

59825983
auto HasAnyParams = !SuccessParamNames.empty() || ErrName;
59835984
if (HasAnyParams)
@@ -6009,8 +6010,21 @@ class AsyncConverter : private SourceEntityWalker {
60096010
OS << tok::kw_if << " " << tok::kw_let << " ";
60106011
OS << *ErrName << " " << tok::equal << " " << *ErrName << " ";
60116012
OS << tok::l_brace << "\n";
6013+
for (auto Idx : indices(SuccessParamNames)) {
6014+
auto &Name = SuccessParamNames[Idx];
6015+
auto ParamTy = SuccessParams[Idx].getParameterType();
6016+
if (!HandlerDesc.shouldUnwrap(ParamTy))
6017+
continue;
6018+
6019+
// assert(res == nil, "Expected nil-success param 'res' for non-nil
6020+
// error")
6021+
OS << "assert" << tok::l_paren << Name << " == " << tok::kw_nil;
6022+
OS << tok::comma << " \"Expected nil success param '" << Name;
6023+
OS << "' for non-nil error\"";
6024+
OS << tok::r_paren << "\n";
6025+
}
60126026

6013-
// cont.resume(throwing: err)
6027+
// continuation.resume(throwing: err)
60146028
OS << ContName << tok::period << "resume" << tok::l_paren;
60156029
OS << "throwing" << tok::colon << " " << *ErrName;
60166030
OS << tok::r_paren << "\n";
@@ -6042,15 +6056,15 @@ class AsyncConverter : private SourceEntityWalker {
60426056
OS << tok::r_brace << "\n";
60436057
}
60446058

6045-
// cont.resume(returning: (res1, res2, ...))
6059+
// continuation.resume(returning: (res1, res2, ...))
60466060
OS << ContName << tok::period << "resume" << tok::l_paren;
60476061
OS << "returning" << tok::colon << " ";
60486062
addTupleOf(SuccessParamNames, OS, [&](auto Ref) { OS << Ref; });
60496063
OS << tok::r_paren << "\n";
60506064
break;
60516065
}
60526066
case HandlerType::RESULT: {
6053-
// cont.resume(with: res)
6067+
// continuation.resume(with: res)
60546068
assert(SuccessParamNames.size() == 1);
60556069
OS << ContName << tok::period << "resume" << tok::l_paren;
60566070
OS << "with" << tok::colon << " " << SuccessParamNames[0];

test/refactoring/ConvertAsync/convert_async_wrapper.swift

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ func foo1(_ completion: @escaping () -> Void) {}
1515
// FOO1-EMPTY:
1616
// FOO1-NEXT: convert_async_wrapper.swift [[# @LINE-8]]:49 -> [[# @LINE-8]]:49
1717
// FOO1-NEXT: func foo1() async {
18-
// FOO1-NEXT: return await withCheckedContinuation { cont in
18+
// FOO1-NEXT: return await withCheckedContinuation { continuation in
1919
// FOO1-NEXT: foo1() {
20-
// FOO1-NEXT: cont.resume(returning: ())
20+
// FOO1-NEXT: continuation.resume(returning: ())
2121
// FOO1-NEXT: }
2222
// FOO1-NEXT: }
2323
// FOO1-NEXT: }
@@ -33,9 +33,9 @@ func foo2(arg: String, _ completion: @escaping (String) -> Void) {}
3333
// FOO2-EMPTY:
3434
// FOO2-NEXT: convert_async_wrapper.swift [[# @LINE-8]]:68 -> [[# @LINE-8]]:68
3535
// FOO2: func foo2(arg: String) async -> String {
36-
// FOO2-NEXT: return await withCheckedContinuation { cont in
37-
// FOO2-NEXT: foo2(arg: arg) { res in
38-
// FOO2-NEXT: cont.resume(returning: res)
36+
// FOO2-NEXT: return await withCheckedContinuation { continuation in
37+
// FOO2-NEXT: foo2(arg: arg) { result in
38+
// FOO2-NEXT: continuation.resume(returning: result)
3939
// FOO2-NEXT: }
4040
// FOO2-NEXT: }
4141
// FOO2-NEXT: }
@@ -44,9 +44,9 @@ func foo2(arg: String, _ completion: @escaping (String) -> Void) {}
4444
func foo3(arg: String, _ arg2: Int, _ completion: @escaping (String?) -> Void) {}
4545

4646
// FOO3: func foo3(arg: String, _ arg2: Int) async -> String? {
47-
// FOO3-NEXT: return await withCheckedContinuation { cont in
48-
// FOO3-NEXT: foo3(arg: arg, arg2) { res in
49-
// FOO3-NEXT: cont.resume(returning: res)
47+
// FOO3-NEXT: return await withCheckedContinuation { continuation in
48+
// FOO3-NEXT: foo3(arg: arg, arg2) { result in
49+
// FOO3-NEXT: continuation.resume(returning: result)
5050
// FOO3-NEXT: }
5151
// FOO3-NEXT: }
5252
// FOO3-NEXT: }
@@ -55,13 +55,13 @@ func foo3(arg: String, _ arg2: Int, _ completion: @escaping (String?) -> Void) {
5555
func foo4(_ completion: @escaping (Error?) -> Void) {}
5656

5757
// FOO4: func foo4() async throws {
58-
// FOO4-NEXT: return try await withCheckedThrowingContinuation { cont in
59-
// FOO4-NEXT: foo4() { err in
60-
// FOO4-NEXT: if let err = err {
61-
// FOO4-NEXT: cont.resume(throwing: err)
58+
// FOO4-NEXT: return try await withCheckedThrowingContinuation { continuation in
59+
// FOO4-NEXT: foo4() { error in
60+
// FOO4-NEXT: if let error = error {
61+
// FOO4-NEXT: continuation.resume(throwing: error)
6262
// FOO4-NEXT: return
6363
// FOO4-NEXT: }
64-
// FOO4-NEXT: cont.resume(returning: ())
64+
// FOO4-NEXT: continuation.resume(returning: ())
6565
// FOO4-NEXT: }
6666
// FOO4-NEXT: }
6767
// FOO4-NEXT: }
@@ -71,9 +71,9 @@ func foo4(_ completion: @escaping (Error?) -> Void) {}
7171
func foo5(_ completion: @escaping (Error) -> Void) {}
7272

7373
// FOO5: func foo5() async -> Error {
74-
// FOO5-NEXT: return await withCheckedContinuation { cont in
75-
// FOO5-NEXT: foo5() { res in
76-
// FOO5-NEXT: cont.resume(returning: res)
74+
// FOO5-NEXT: return await withCheckedContinuation { continuation in
75+
// FOO5-NEXT: foo5() { result in
76+
// FOO5-NEXT: continuation.resume(returning: result)
7777
// FOO5-NEXT: }
7878
// FOO5-NEXT: }
7979
// FOO5-NEXT: }
@@ -82,16 +82,17 @@ func foo5(_ completion: @escaping (Error) -> Void) {}
8282
func foo6(_ completion: @escaping (String?, Error?) -> Void) {}
8383

8484
// FOO6: func foo6() async throws -> String {
85-
// FOO6-NEXT: return try await withCheckedThrowingContinuation { cont in
86-
// FOO6-NEXT: foo6() { res, err in
87-
// FOO6-NEXT: if let err = err {
88-
// FOO6-NEXT: cont.resume(throwing: err)
85+
// FOO6-NEXT: return try await withCheckedThrowingContinuation { continuation in
86+
// FOO6-NEXT: foo6() { result, error in
87+
// FOO6-NEXT: if let error = error {
88+
// FOO6-NEXT: assert(result == nil, "Expected nil success param 'result' for non-nil error")
89+
// FOO6-NEXT: continuation.resume(throwing: error)
8990
// FOO6-NEXT: return
9091
// FOO6-NEXT: }
91-
// FOO6-NEXT: guard let res = res else {
92-
// FOO6-NEXT: fatalError("Expected non-nil success param 'res' for nil error")
92+
// FOO6-NEXT: guard let result = result else {
93+
// FOO6-NEXT: fatalError("Expected non-nil success param 'result' for nil error")
9394
// FOO6-NEXT: }
94-
// FOO6-NEXT: cont.resume(returning: res)
95+
// FOO6-NEXT: continuation.resume(returning: result)
9596
// FOO6-NEXT: }
9697
// FOO6-NEXT: }
9798
// FOO6-NEXT: }
@@ -100,16 +101,17 @@ func foo6(_ completion: @escaping (String?, Error?) -> Void) {}
100101
func foo7(_ completion: @escaping (String?, Int, Error?) -> Void) {}
101102

102103
// FOO7: func foo7() async throws -> (String, Int) {
103-
// FOO7-NEXT: return try await withCheckedThrowingContinuation { cont in
104-
// FOO7-NEXT: foo7() { res1, res2, err in
105-
// FOO7-NEXT: if let err = err {
106-
// FOO7-NEXT: cont.resume(throwing: err)
104+
// FOO7-NEXT: return try await withCheckedThrowingContinuation { continuation in
105+
// FOO7-NEXT: foo7() { result1, result2, error in
106+
// FOO7-NEXT: if let error = error {
107+
// FOO7-NEXT: assert(result1 == nil, "Expected nil success param 'result1' for non-nil error")
108+
// FOO7-NEXT: continuation.resume(throwing: error)
107109
// FOO7-NEXT: return
108110
// FOO7-NEXT: }
109-
// FOO7-NEXT: guard let res1 = res1 else {
110-
// FOO7-NEXT: fatalError("Expected non-nil success param 'res1' for nil error")
111+
// FOO7-NEXT: guard let result1 = result1 else {
112+
// FOO7-NEXT: fatalError("Expected non-nil success param 'result1' for nil error")
111113
// FOO7-NEXT: }
112-
// FOO7-NEXT: cont.resume(returning: (res1, res2))
114+
// FOO7-NEXT: continuation.resume(returning: (result1, result2))
113115
// FOO7-NEXT: }
114116
// FOO7-NEXT: }
115117
// FOO7-NEXT: }
@@ -118,19 +120,21 @@ func foo7(_ completion: @escaping (String?, Int, Error?) -> Void) {}
118120
func foo8(_ completion: @escaping (String?, Int?, Error?) -> Void) {}
119121

120122
// FOO8: func foo8() async throws -> (String, Int) {
121-
// FOO8-NEXT: return try await withCheckedThrowingContinuation { cont in
122-
// FOO8-NEXT: foo8() { res1, res2, err in
123-
// FOO8-NEXT: if let err = err {
124-
// FOO8-NEXT: cont.resume(throwing: err)
123+
// FOO8-NEXT: return try await withCheckedThrowingContinuation { continuation in
124+
// FOO8-NEXT: foo8() { result1, result2, error in
125+
// FOO8-NEXT: if let error = error {
126+
// FOO8-NEXT: assert(result1 == nil, "Expected nil success param 'result1' for non-nil error")
127+
// FOO8-NEXT: assert(result2 == nil, "Expected nil success param 'result2' for non-nil error")
128+
// FOO8-NEXT: continuation.resume(throwing: error)
125129
// FOO8-NEXT: return
126130
// FOO8-NEXT: }
127-
// FOO8-NEXT: guard let res1 = res1 else {
128-
// FOO8-NEXT: fatalError("Expected non-nil success param 'res1' for nil error")
131+
// FOO8-NEXT: guard let result1 = result1 else {
132+
// FOO8-NEXT: fatalError("Expected non-nil success param 'result1' for nil error")
129133
// FOO8-NEXT: }
130-
// FOO8-NEXT: guard let res2 = res2 else {
131-
// FOO8-NEXT: fatalError("Expected non-nil success param 'res2' for nil error")
134+
// FOO8-NEXT: guard let result2 = result2 else {
135+
// FOO8-NEXT: fatalError("Expected non-nil success param 'result2' for nil error")
132136
// FOO8-NEXT: }
133-
// FOO8-NEXT: cont.resume(returning: (res1, res2))
137+
// FOO8-NEXT: continuation.resume(returning: (result1, result2))
134138
// FOO8-NEXT: }
135139
// FOO8-NEXT: }
136140
// FOO8-NEXT: }
@@ -139,9 +143,9 @@ func foo8(_ completion: @escaping (String?, Int?, Error?) -> Void) {}
139143
func foo9(_ completion: @escaping (Result<String, Error>) -> Void) {}
140144

141145
// FOO9: func foo9() async throws -> String {
142-
// FOO9-NEXT: return try await withCheckedThrowingContinuation { cont in
143-
// FOO9-NEXT: foo9() { res in
144-
// FOO9-NEXT: cont.resume(with: res)
146+
// FOO9-NEXT: return try await withCheckedThrowingContinuation { continuation in
147+
// FOO9-NEXT: foo9() { result in
148+
// FOO9-NEXT: continuation.resume(with: result)
145149
// FOO9-NEXT: }
146150
// FOO9-NEXT: }
147151
// FOO9-NEXT: }
@@ -150,9 +154,9 @@ func foo9(_ completion: @escaping (Result<String, Error>) -> Void) {}
150154
func foo10(arg: Int, _ completion: @escaping (Result<(String, Int), Error>) -> Void) {}
151155

152156
// FOO10: func foo10(arg: Int) async throws -> (String, Int) {
153-
// FOO10-NEXT: return try await withCheckedThrowingContinuation { cont in
154-
// FOO10-NEXT: foo10(arg: arg) { res in
155-
// FOO10-NEXT: cont.resume(with: res)
157+
// FOO10-NEXT: return try await withCheckedThrowingContinuation { continuation in
158+
// FOO10-NEXT: foo10(arg: arg) { result in
159+
// FOO10-NEXT: continuation.resume(with: result)
156160
// FOO10-NEXT: }
157161
// FOO10-NEXT: }
158162
// FOO10-NEXT: }
@@ -161,9 +165,9 @@ func foo10(arg: Int, _ completion: @escaping (Result<(String, Int), Error>) -> V
161165
func foo11(completion: @escaping (Result<String, Never>) -> Void) {}
162166

163167
// FOO11: func foo11() async -> String {
164-
// FOO11-NEXT: return await withCheckedContinuation { cont in
165-
// FOO11-NEXT: foo11() { res in
166-
// FOO11-NEXT: cont.resume(with: res)
168+
// FOO11-NEXT: return await withCheckedContinuation { continuation in
169+
// FOO11-NEXT: foo11() { result in
170+
// FOO11-NEXT: continuation.resume(with: result)
167171
// FOO11-NEXT: }
168172
// FOO11-NEXT: }
169173
// FOO11-NEXT: }
@@ -172,9 +176,9 @@ func foo11(completion: @escaping (Result<String, Never>) -> Void) {}
172176
func foo12(completion: @escaping (Result<String, CustomError>) -> Void) {}
173177

174178
// FOO12: func foo12() async throws -> String {
175-
// FOO12-NEXT: return try await withCheckedThrowingContinuation { cont in
176-
// FOO12-NEXT: foo12() { res in
177-
// FOO12-NEXT: cont.resume(with: res)
179+
// FOO12-NEXT: return try await withCheckedThrowingContinuation { continuation in
180+
// FOO12-NEXT: foo12() { result in
181+
// FOO12-NEXT: continuation.resume(with: result)
178182
// FOO12-NEXT: }
179183
// FOO12-NEXT: }
180184
// FOO12-NEXT: }

0 commit comments

Comments
 (0)