Skip to content

Commit d69f0df

Browse files
authored
Merge pull request #38259 from ahoppen/pr/rename-cont-and-err
[Async Refactoring] Cosmetic Improvements to "Add Async Wrapper" Refactoring
2 parents 3a1a423 + 3dcb08a commit d69f0df

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

59255925
OS << "await ";
59265926

5927-
// withChecked[Throwing]Continuation { cont in
5927+
// withChecked[Throwing]Continuation { continuation in
59285928
if (TopHandler.HasError) {
59295929
OS << "withCheckedThrowingContinuation";
59305930
} else {
59315931
OS << "withCheckedContinuation";
59325932
}
5933-
OS << " " << tok::l_brace << " cont " << tok::kw_in << "\n";
5933+
OS << " " << tok::l_brace << " continuation " << tok::kw_in << "\n";
59345934

59355935
// fnWithHandler(args...) { ... }
5936-
auto ClosureStr = getAsyncWrapperCompletionClosure("cont", TopHandler);
5937-
addForwardingCallTo(FD, TopHandler, /*HandlerReplacement*/ ClosureStr);
5936+
auto ClosureStr =
5937+
getAsyncWrapperCompletionClosure("continuation", TopHandler);
5938+
addForwardingCallTo(FD, TopHandler, /*HandlerReplacement=*/ClosureStr);
59385939

59395940
OS << tok::r_brace << "\n"; // end continuation closure
59405941
OS << tok::r_brace << "\n"; // end function body
@@ -5998,21 +5999,21 @@ class AsyncConverter : private SourceEntityWalker {
59985999
std::string OutputStr;
59996000
llvm::raw_string_ostream OS(OutputStr);
60006001

6001-
OS << " " << tok::l_brace; // start closure
6002+
OS << tok::l_brace; // start closure
60026003

60036004
// Prepare parameter names for the closure.
60046005
auto SuccessParams = HandlerDesc.getSuccessParams();
60056006
SmallVector<SmallString<4>, 2> SuccessParamNames;
60066007
for (auto idx : indices(SuccessParams)) {
6007-
SuccessParamNames.emplace_back("res");
6008+
SuccessParamNames.emplace_back("result");
60086009

60096010
// If we have multiple success params, number them e.g res1, res2...
60106011
if (SuccessParams.size() > 1)
60116012
SuccessParamNames.back().append(std::to_string(idx + 1));
60126013
}
60136014
Optional<SmallString<4>> ErrName;
60146015
if (HandlerDesc.getErrorParam())
6015-
ErrName.emplace("err");
6016+
ErrName.emplace("error");
60166017

60176018
auto HasAnyParams = !SuccessParamNames.empty() || ErrName;
60186019
if (HasAnyParams)
@@ -6044,8 +6045,21 @@ class AsyncConverter : private SourceEntityWalker {
60446045
OS << tok::kw_if << " " << tok::kw_let << " ";
60456046
OS << *ErrName << " " << tok::equal << " " << *ErrName << " ";
60466047
OS << tok::l_brace << "\n";
6048+
for (auto Idx : indices(SuccessParamNames)) {
6049+
auto &Name = SuccessParamNames[Idx];
6050+
auto ParamTy = SuccessParams[Idx].getParameterType();
6051+
if (!HandlerDesc.shouldUnwrap(ParamTy))
6052+
continue;
6053+
6054+
// assert(res == nil, "Expected nil-success param 'res' for non-nil
6055+
// error")
6056+
OS << "assert" << tok::l_paren << Name << " == " << tok::kw_nil;
6057+
OS << tok::comma << " \"Expected nil success param '" << Name;
6058+
OS << "' for non-nil error\"";
6059+
OS << tok::r_paren << "\n";
6060+
}
60476061

6048-
// cont.resume(throwing: err)
6062+
// continuation.resume(throwing: err)
60496063
OS << ContName << tok::period << "resume" << tok::l_paren;
60506064
OS << "throwing" << tok::colon << " " << *ErrName;
60516065
OS << tok::r_paren << "\n";
@@ -6077,15 +6091,15 @@ class AsyncConverter : private SourceEntityWalker {
60776091
OS << tok::r_brace << "\n";
60786092
}
60796093

6080-
// cont.resume(returning: (res1, res2, ...))
6094+
// continuation.resume(returning: (res1, res2, ...))
60816095
OS << ContName << tok::period << "resume" << tok::l_paren;
60826096
OS << "returning" << tok::colon << " ";
60836097
addTupleOf(SuccessParamNames, OS, [&](auto Ref) { OS << Ref; });
60846098
OS << tok::r_paren << "\n";
60856099
break;
60866100
}
60876101
case HandlerType::RESULT: {
6088-
// cont.resume(with: res)
6102+
// continuation.resume(with: res)
60896103
assert(SuccessParamNames.size() == 1);
60906104
OS << ContName << tok::period << "resume" << tok::l_paren;
60916105
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)