Skip to content

Commit 613fdca

Browse files
committed
[CSDiagnostics] Fix trailing closure ambiguity note to not expect that anchor is expression
`TrailingClosureAmbiguityFailure::diagnoseAsNote()` is used by `diagnoseAmbiguity` opportunistically, which means that anchor could be a pattern or a statement condition element.
1 parent 8c21aaf commit 613fdca

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,12 @@ bool TypeChecker::diagnoseSelfAssignment(const Expr *expr) {
17461746
}
17471747

17481748
bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
1749-
auto *anchor = castToExpr(getAnchor());
1749+
auto *anchor = getAsExpr(getAnchor());
1750+
// This diagnostic is used opportunistically in `diagnoseAmbiguity`,
1751+
// which means it cannot assume that anchor is always an expression.
1752+
if (!anchor)
1753+
return false;
1754+
17501755
const auto *expr = findParentExpr(anchor);
17511756
auto *callExpr = dyn_cast_or_null<CallExpr>(expr);
17521757
if (!callExpr)

test/expr/closure/multi_statement.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ func test_local_function_capturing_vars() {
234234
}
235235
}
236236

237+
func test_pattern_ambiguity_doesnot_crash_compiler() {
238+
enum E {
239+
case hello(result: Int) // expected-note {{found this candidate}}
240+
case hello(status: Int) // expected-note {{found this candidate}}
241+
}
242+
243+
let _: (E) -> Void = {
244+
switch $0 {
245+
case let E.hello(x): print(x) // expected-error {{ambiguous use of 'hello'}}
246+
default: break
247+
}
248+
}
249+
}
250+
237251
func test_taps_type_checked_with_correct_decl_context() {
238252
struct Path {
239253
func contains<T>(_: T) -> Bool where T: StringProtocol { return false }

0 commit comments

Comments
 (0)