Skip to content

Commit 4d8d719

Browse files
authored
Merge pull request #85406 from naveen-seth/fix-ambiguous-trailing-closure-init
[Sema] Fix crash when diagnosing ambiguous trailing closure inits
2 parents 65d5425 + a20879b commit 4d8d719

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,9 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
21422142
return false;
21432143

21442144
const ParameterList *paramList = callee->getParameters();
2145-
const ParamDecl *param = paramList->getArray().back();
2145+
if (!paramList || paramList->size() == 0)
2146+
return false;
2147+
const ParamDecl *param = paramList->back();
21462148

21472149
// Soundness-check that the trailing closure corresponds to this parameter.
21482150
if (!param->hasInterfaceType() ||

validation-test/compiler_crashers/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift renamed to validation-test/compiler_crashers_fixed/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// {"kind":"typecheck","signature":"swift::constraints::TrailingClosureAmbiguityFailure::diagnoseAsNote()","signatureAssert":"Assertion failed: (!empty()), function back"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
{
44
struct a {
55
init ();
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)