Skip to content

Commit 2cfd2bf

Browse files
committed
Don't handle unclassified switch cases for now
Previously we would silently add these to the success block. For now, let's leave them unhandled.
1 parent c818172 commit 2cfd2bf

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

include/swift/AST/DiagnosticsRefactoring.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ ERROR(callback_multiple_case_items, none, "cannot refactor switch using a case w
7070

7171
ERROR(callback_where_case_item, none, "cannot refactor switch using a case with where clause", ())
7272

73+
ERROR(unknown_callback_case_item, none, "cannot refactor complex case conditions", ())
74+
7375
#define UNDEFINE_DIAGNOSTIC_MACROS
7476
#include "DefineDiagnosticMacros.h"

lib/IDE/Refactoring.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,7 +5326,12 @@ struct CallbackClassifier {
53265326
auto CC = classifyCallbackCondition(
53275327
CallbackCondition(ErrParam, &Items[0]), SuccessNodes,
53285328
/*elseStmt*/ nullptr);
5329-
if (CC && CC->Path == ConditionPath::FAILURE)
5329+
if (!CC) {
5330+
DiagEngine.diagnose(CS->getLoc(), diag::unknown_callback_case_item);
5331+
return;
5332+
}
5333+
5334+
if (CC->Path == ConditionPath::FAILURE)
53305335
std::swap(Block, OtherBlock);
53315336

53325337
// We'll be dropping the case, but make sure to keep any attached
@@ -5339,8 +5344,7 @@ struct CallbackClassifier {
53395344
Block->addPossibleCommentLoc(SS->getRBraceLoc());
53405345

53415346
setNodes(Block, OtherBlock, std::move(SuccessNodes));
5342-
if (CC)
5343-
Block->addBinding(*CC, DiagEngine);
5347+
Block->addBinding(*CC, DiagEngine);
53445348
if (DiagEngine.hadAnyError())
53455349
return;
53465350
}

test/refactoring/ConvertAsync/convert_pattern.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
enum E : Error { case e }
44

55
func anyCompletion(_ completion: (Any?, Error?) -> Void) {}
6+
func anyResultCompletion(_ completion: (Result<Any?, Error>) -> Void) {}
67

78
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=TEST-UNHANDLED %s
89
anyCompletion { val, err in
@@ -43,3 +44,77 @@ anyCompletion { val, err in
4344
// TEST-UNHANDLED-NEXT: if case let "" as String = <#x#> {
4445
// TEST-UNHANDLED-NEXT: print("g")
4546
// TEST-UNHANDLED-NEXT: }
47+
48+
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
49+
anyResultCompletion { res in
50+
switch res {
51+
case .success(let unwrapped?):
52+
print(unwrapped)
53+
case .success(let x):
54+
print(x)
55+
case .failure:
56+
print("oh no")
57+
}
58+
}
59+
60+
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
61+
anyResultCompletion { res in
62+
switch res {
63+
case .success(let str as String):
64+
print(str)
65+
case .success(let x):
66+
print(x)
67+
case .failure:
68+
print("oh no")
69+
}
70+
}
71+
72+
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
73+
anyResultCompletion { res in
74+
switch res {
75+
case .success(E.e?):
76+
print("ee")
77+
case .success(let x):
78+
print(x)
79+
case .failure:
80+
print("oh no")
81+
}
82+
}
83+
84+
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
85+
anyResultCompletion { res in
86+
switch res {
87+
case .success("" as String):
88+
print("empty string")
89+
case .success(let x):
90+
print(x)
91+
case .failure:
92+
print("oh no")
93+
}
94+
}
95+
96+
// FIXME: This should ideally be turned into a 'catch let e as E'.
97+
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
98+
anyResultCompletion { res in
99+
switch res {
100+
case .success(let a):
101+
print(a)
102+
case .failure(let e as E):
103+
print("oh no e")
104+
case .failure:
105+
print("oh no")
106+
}
107+
}
108+
109+
// FIXME: This should ideally be turned into a 'catch E.e'.
110+
// RUN: not %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):1
111+
anyResultCompletion { res in
112+
switch res {
113+
case .success(let a):
114+
print(a)
115+
case .failure(E.e):
116+
print("oh no ee")
117+
case .failure:
118+
print("oh no")
119+
}
120+
}

0 commit comments

Comments
 (0)