mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[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.
This commit is contained in:
@@ -2142,6 +2142,8 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
const ParameterList *paramList = callee->getParameters();
|
const ParameterList *paramList = callee->getParameters();
|
||||||
|
if (paramList->getArray().empty())
|
||||||
|
return false;
|
||||||
const ParamDecl *param = paramList->getArray().back();
|
const ParamDecl *param = paramList->getArray().back();
|
||||||
|
|
||||||
// Soundness-check that the trailing closure corresponds to this parameter.
|
// Soundness-check that the trailing closure corresponds to this parameter.
|
||||||
|
|||||||
27
test/Sema/ambiguous_init_trailing_closure.swift
Normal file
27
test/Sema/ambiguous_init_trailing_closure.swift
Normal file
@@ -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'}}
|
||||||
Reference in New Issue
Block a user