Skip to content

Commit 76e7be0

Browse files
committed
[Refactoring] Do not try to unique '_'
The async refactorings should not try to unique '_' when it's used as the parameter name. Also skip adding a let binding to the `catch` if the error parameter is '_'. Resolves rdar://82158389
1 parent a5d8c33 commit 76e7be0

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7254,7 +7254,7 @@ class AsyncConverter : private SourceEntityWalker {
72547254
/*Success=*/true);
72557255

72567256
addAwaitCall(CE, ArgList.ref(), Blocks.SuccessBlock, SuccessParams,
7257-
InlinePatterns, HandlerDesc, /*AddDeclarations*/ true);
7257+
InlinePatterns, HandlerDesc, /*AddDeclarations=*/true);
72587258
printOutOfLineBindingPatterns(Blocks.SuccessBlock, InlinePatterns);
72597259
convertNodes(Blocks.SuccessBlock.nodesToPrint());
72607260
clearNames(SuccessParams);
@@ -7265,7 +7265,8 @@ class AsyncConverter : private SourceEntityWalker {
72657265

72667266
// Always use the ErrParam name if none is bound.
72677267
prepareNames(Blocks.ErrorBlock, llvm::makeArrayRef(ErrParam),
7268-
ErrInlinePatterns, HandlerDesc.Type != HandlerType::RESULT);
7268+
ErrInlinePatterns,
7269+
/*AddIfMissing=*/HandlerDesc.Type != HandlerType::RESULT);
72697270
preparePlaceholdersAndUnwraps(HandlerDesc, SuccessParams, ErrParam,
72707271
/*Success=*/false);
72717272

@@ -7546,7 +7547,7 @@ class AsyncConverter : private SourceEntityWalker {
75467547
void addCatch(const ParamDecl *ErrParam) {
75477548
OS << "\n" << tok::r_brace << " " << tok::kw_catch << " ";
75487549
auto ErrName = newNameFor(ErrParam, false);
7549-
if (!ErrName.empty()) {
7550+
if (!ErrName.empty() && ErrName != "_") {
75507551
OS << tok::kw_let << " " << ErrName << " ";
75517552
}
75527553
OS << tok::l_brace;
@@ -7630,6 +7631,8 @@ class AsyncConverter : private SourceEntityWalker {
76307631
/// other names in the current scope.
76317632
Identifier createUniqueName(StringRef Name) {
76327633
Identifier Ident = getASTContext().getIdentifier(Name);
7634+
if (Name == "_")
7635+
return Ident;
76337636

76347637
auto &CurrentNames = Scopes.back().Names;
76357638
if (CurrentNames.count(Ident)) {

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,46 @@ func twoCompletionHandlerCalls(completion: @escaping (String?, Error?) -> Void)
622622
// TWO-COMPLETION-HANDLER-CALLS-NEXT: return res
623623
// TWO-COMPLETION-HANDLER-CALLS-NEXT: return res
624624
// TWO-COMPLETION-HANDLER-CALLS-NEXT: }
625+
626+
// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=NESTED-IGNORED %s
627+
func nestedIgnored() throws {
628+
simple { _ in
629+
print("done")
630+
simple { _ in
631+
print("done")
632+
}
633+
}
634+
}
635+
// NESTED-IGNORED: func nestedIgnored() async throws {
636+
// NESTED-IGNORED-NEXT: let _ = await simple()
637+
// NESTED-IGNORED-NEXT: print("done")
638+
// NESTED-IGNORED-NEXT: let _ = await simple()
639+
// NESTED-IGNORED-NEXT: print("done")
640+
// NESTED-IGNORED-NEXT: }
641+
642+
// RUN: %refactor-check-compiles -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=IGNORED-ERR %s
643+
func nestedIgnoredErr() throws {
644+
simpleErr(arg: "") { str, _ in
645+
if str == nil {
646+
print("error")
647+
}
648+
649+
simpleErr(arg: "") { str, _ in
650+
if str == nil {
651+
print("error")
652+
}
653+
}
654+
}
655+
}
656+
// IGNORED-ERR: func nestedIgnoredErr() async throws {
657+
// IGNORED-ERR-NEXT: do {
658+
// IGNORED-ERR-NEXT: let str = try await simpleErr(arg: "")
659+
// IGNORED-ERR-NEXT: do {
660+
// IGNORED-ERR-NEXT: let str1 = try await simpleErr(arg: "")
661+
// IGNORED-ERR-NEXT: } catch {
662+
// IGNORED-ERR-NEXT: print("error")
663+
// IGNORED-ERR-NEXT: }
664+
// IGNORED-ERR-NEXT: } catch {
665+
// IGNORED-ERR-NEXT: print("error")
666+
// IGNORED-ERR-NEXT: }
667+
// IGNORED-ERR-NEXT: }

0 commit comments

Comments
 (0)