Skip to content

Commit 8b7b0e9

Browse files
committed
[Sema] Fix crash when diagnosing ambiguous trailing closure inits
Fixes #85376. This fixes a compiler crash that occurred when diagnosing an ambiguous call using trailing closure syntax, where one of the candidates was a function or initializer with no parameters.
1 parent 087aee8 commit 8b7b0e9

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,8 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
21422142
return false;
21432143

21442144
const ParameterList *paramList = callee->getParameters();
2145+
if (paramList->getArray().empty())
2146+
return false;
21452147
const ParamDecl *param = paramList->getArray().back();
21462148

21472149
// Soundness-check that the trailing closure corresponds to this parameter.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 6
2+
// Checks that the compiler correctly diagnoses ambiguous initializers using
3+
// trailing closures, rather than crashing.
4+
//
5+
// Fixes #85364
6+
7+
struct S1 { // expected-note {{found this candidate}} \
8+
// expected-note {{'S1' previously declared here}}
9+
var c: () -> Void
10+
}
11+
12+
S1 {} // expected-error {{ambiguous use of 'init'}}
13+
14+
struct S1 { // expected-note {{found this candidate}} \
15+
// expected-error {{invalid redeclaration of 'S1'}}
16+
func callAsFunction(_: () -> Void) {}
17+
}
18+
19+
struct S2 {
20+
init() {} // expected-note {{found this candidate}}
21+
22+
init(_ block: () -> Void) { block() } // expected-note {{found this candidate}}
23+
24+
func callAsFunction(_ block: () -> Void) { block() }
25+
}
26+
27+
S2 {} // expected-error {{ambiguous use of 'init'}}

0 commit comments

Comments
 (0)