From 8b7b0e944baff3e3a9e57f4f45bd0b05ae58176c Mon Sep 17 00:00:00 2001 From: naveen-seth Date: Mon, 10 Nov 2025 03:04:17 +0100 Subject: [PATCH 1/4] [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. --- lib/Sema/CSDiagnostics.cpp | 2 ++ .../ambiguous_init_trailing_closure.swift | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/Sema/ambiguous_init_trailing_closure.swift diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index 975b57d9125d2..e60d5509ddf2e 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -2142,6 +2142,8 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() { return false; const ParameterList *paramList = callee->getParameters(); + if (paramList->getArray().empty()) + return false; const ParamDecl *param = paramList->getArray().back(); // Soundness-check that the trailing closure corresponds to this parameter. diff --git a/test/Sema/ambiguous_init_trailing_closure.swift b/test/Sema/ambiguous_init_trailing_closure.swift new file mode 100644 index 0000000000000..ce66778201aab --- /dev/null +++ b/test/Sema/ambiguous_init_trailing_closure.swift @@ -0,0 +1,27 @@ +// RUN: %target-typecheck-verify-swift -swift-version 6 +// Checks that the compiler correctly diagnoses ambiguous initializers using +// trailing closures, rather than crashing. +// +// Fixes #85364 + +struct S1 { // expected-note {{found this candidate}} \ + // expected-note {{'S1' previously declared here}} + var c: () -> Void +} + +S1 {} // expected-error {{ambiguous use of 'init'}} + +struct S1 { // expected-note {{found this candidate}} \ + // expected-error {{invalid redeclaration of 'S1'}} + func callAsFunction(_: () -> Void) {} +} + +struct S2 { + init() {} // expected-note {{found this candidate}} + + init(_ block: () -> Void) { block() } // expected-note {{found this candidate}} + + func callAsFunction(_ block: () -> Void) { block() } +} + +S2 {} // expected-error {{ambiguous use of 'init'}} From 9a88d614626ccb83718db95f1fd39b5ae5e0c0f5 Mon Sep 17 00:00:00 2001 From: naveen-seth Date: Mon, 10 Nov 2025 23:04:50 +0100 Subject: [PATCH 2/4] Address xedin's review feedback --- lib/Sema/CSDiagnostics.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index e60d5509ddf2e..70bea706011e1 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -2142,9 +2142,9 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() { return false; const ParameterList *paramList = callee->getParameters(); - if (paramList->getArray().empty()) + if (!paramList || paramList->size() == 0) return false; - const ParamDecl *param = paramList->getArray().back(); + const ParamDecl *param = paramList->back(); // Soundness-check that the trailing closure corresponds to this parameter. if (!param->hasInterfaceType() || From d07ea18b555279da6a6d2db87b25ea35a22217b1 Mon Sep 17 00:00:00 2001 From: naveen-seth Date: Tue, 11 Nov 2025 00:43:01 +0100 Subject: [PATCH 3/4] Mark validation test as fixed --- .../TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename validation-test/{compiler_crashers => compiler_crashers_fixed}/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift (82%) diff --git a/validation-test/compiler_crashers/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift b/validation-test/compiler_crashers_fixed/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift similarity index 82% rename from validation-test/compiler_crashers/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift rename to validation-test/compiler_crashers_fixed/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift index bbbc6b3e4d540..6e78cfc8ab1a6 100644 --- a/validation-test/compiler_crashers/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift +++ b/validation-test/compiler_crashers_fixed/TrailingClosureAmbiguityFailure-diagnoseAsNote-d62041.swift @@ -1,5 +1,5 @@ // {"kind":"typecheck","signature":"swift::constraints::TrailingClosureAmbiguityFailure::diagnoseAsNote()","signatureAssert":"Assertion failed: (!empty()), function back"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s { struct a { init (); From a20879b7cc7aee617723a47b0594698e530a99b5 Mon Sep 17 00:00:00 2001 From: naveen-seth Date: Tue, 11 Nov 2025 17:24:37 +0100 Subject: [PATCH 4/4] Move new test to validation-test/compiler_crashers --- .../compiler_crashers_fixed/issue-85364.swift | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/Sema/ambiguous_init_trailing_closure.swift => validation-test/compiler_crashers_fixed/issue-85364.swift (100%) diff --git a/test/Sema/ambiguous_init_trailing_closure.swift b/validation-test/compiler_crashers_fixed/issue-85364.swift similarity index 100% rename from test/Sema/ambiguous_init_trailing_closure.swift rename to validation-test/compiler_crashers_fixed/issue-85364.swift