Skip to content

Commit 9703323

Browse files
authored
Merge pull request #38267 from ahoppen/pr-5.5/rename-cont-and-err
[5.5][Async Refactoring] Cosmetic Improvements to "Add Async Wrapper" Refactoring
2 parents dd43b59 + c8c604a commit 9703323

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
@@ -5938,17 +5938,18 @@ class AsyncConverter : private SourceEntityWalker {
59385938

59395939
OS << "await ";
59405940

5941-
// withChecked[Throwing]Continuation { cont in
5941+
// withChecked[Throwing]Continuation { continuation in
59425942
if (TopHandler.HasError) {
59435943
OS << "withCheckedThrowingContinuation";
59445944
} else {
59455945
OS << "withCheckedContinuation";
59465946
}
5947-
OS << " " << tok::l_brace << " cont " << tok::kw_in << "\n";
5947+
OS << " " << tok::l_brace << " continuation " << tok::kw_in << "\n";
59485948

59495949
// fnWithHandler(args...) { ... }
5950-
auto ClosureStr = getAsyncWrapperCompletionClosure("cont", TopHandler);
5951-
addForwardingCallTo(FD, TopHandler, /*HandlerReplacement*/ ClosureStr);
5950+
auto ClosureStr =
5951+
getAsyncWrapperCompletionClosure("continuation", TopHandler);
5952+
addForwardingCallTo(FD, TopHandler, /*HandlerReplacement=*/ClosureStr);
59525953

59535954
OS << tok::r_brace << "\n"; // end continuation closure
59545955
OS << tok::r_brace << "\n"; // end function body
@@ -6012,21 +6013,21 @@ class AsyncConverter : private SourceEntityWalker {
60126013
std::string OutputStr;
60136014
llvm::raw_string_ostream OS(OutputStr);
60146015

6015-
OS << " " << tok::l_brace; // start closure
6016+
OS << tok::l_brace; // start closure
60166017

60176018
// Prepare parameter names for the closure.
60186019
auto SuccessParams = HandlerDesc.getSuccessParams();
60196020
SmallVector<SmallString<4>, 2> SuccessParamNames;
60206021
for (auto idx : indices(SuccessParams)) {
6021-
SuccessParamNames.emplace_back("res");
6022+
SuccessParamNames.emplace_back("result");
60226023

60236024
// If we have multiple success params, number them e.g res1, res2...
60246025
if (SuccessParams.size() > 1)
60256026
SuccessParamNames.back().append(std::to_string(idx + 1));
60266027
}
60276028
Optional<SmallString<4>> ErrName;
60286029
if (HandlerDesc.getErrorParam())
6029-
ErrName.emplace("err");
6030+
ErrName.emplace("error");
60306031

60316032
auto HasAnyParams = !SuccessParamNames.empty() || ErrName;
60326033
if (HasAnyParams)
@@ -6058,8 +6059,21 @@ class AsyncConverter : private SourceEntityWalker {
60586059
OS << tok::kw_if << " " << tok::kw_let << " ";
60596060
OS << *ErrName << " " << tok::equal << " " << *ErrName << " ";
60606061
OS << tok::l_brace << "\n";
6062+
for (auto Idx : indices(SuccessParamNames)) {
6063+
auto &Name = SuccessParamNames[Idx];
6064+
auto ParamTy = SuccessParams[Idx].getParameterType();
6065+
if (!HandlerDesc.shouldUnwrap(ParamTy))
6066+
continue;
6067+
6068+
// assert(res == nil, "Expected nil-success param 'res' for non-nil
6069+
// error")
6070+
OS << "assert" << tok::l_paren << Name << " == " << tok::kw_nil;
6071+
OS << tok::comma << " \"Expected nil success param '" << Name;
6072+
OS << "' for non-nil error\"";
6073+
OS << tok::r_paren << "\n";
6074+
}
60616075

6062-
// cont.resume(throwing: err)
6076+
// continuation.resume(throwing: err)
60636077
OS << ContName << tok::period << "resume" << tok::l_paren;
60646078
OS << "throwing" << tok::colon << " " << *ErrName;
60656079
OS << tok::r_paren << "\n";
@@ -6091,15 +6105,15 @@ class AsyncConverter : private SourceEntityWalker {
60916105
OS << tok::r_brace << "\n";
60926106
}
60936107

6094-
// cont.resume(returning: (res1, res2, ...))
6108+
// continuation.resume(returning: (res1, res2, ...))
60956109
OS << ContName << tok::period << "resume" << tok::l_paren;
60966110
OS << "returning" << tok::colon << " ";
60976111
addTupleOf(SuccessParamNames, OS, [&](auto Ref) { OS << Ref; });
60986112
OS << tok::r_paren << "\n";
60996113
break;
61006114
}
61016115
case HandlerType::RESULT: {
6102-
// cont.resume(with: res)
6116+
// continuation.resume(with: res)
61036117
assert(SuccessParamNames.size() == 1);
61046118
OS << ContName << tok::period << "resume" << tok::l_paren;
61056119
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)