Skip to content

Commit 84642a6

Browse files
committed
[CSDiagnostics] Update test cases for variadic generic functions
1 parent 9276bb0 commit 84642a6

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8203,19 +8203,18 @@ bool UnableToInferGenericPackElementType::diagnoseAsError() {
82038203
auto *locator = getLocator();
82048204
auto path = locator->getPath();
82058205

8206-
const auto applyArgToParamElt =
8207-
(path.end() - 2)->getAs<LocatorPathElt::ApplyArgToParam>();
8208-
const auto packElementElt =
8209-
(path.end() - 1)->getAs<LocatorPathElt::PackElement>();
8210-
if (!applyArgToParamElt || !packElementElt) {
8211-
return false;
8212-
}
8206+
auto packElementElt = (path.end() - 1)->getAs<LocatorPathElt::PackElement>();
8207+
assert(packElementElt && "Expected path to end with a pack element locator");
82138208

82148209
if (isExpr<NilLiteralExpr>(getAnchor())) {
82158210
// `nil` appears as an element of generic pack params, let's record a
82168211
// specify contextual type for nil fix.
82178212
emitDiagnostic(diag::unresolved_nil_literal);
82188213
} else {
8214+
if (llvm::any_of(getSolution().Fixes, [&locator](const ConstraintFix *fix) {
8215+
return fix->getLocator() == locator;
8216+
}))
8217+
return false;
82198218
// unable to infer the type of an element of generic pack params
82208219
emitDiagnostic(diag::could_not_infer_pack_element,
82218220
packElementElt->getIndex());
@@ -8226,6 +8225,8 @@ bool UnableToInferGenericPackElementType::diagnoseAsError() {
82268225
if (auto *calleeLocator = getSolution().getCalleeLocator(locator)) {
82278226
if (const auto choice = getOverloadChoiceIfAvailable(calleeLocator)) {
82288227
if (auto *decl = choice->choice.getDeclOrNull()) {
8228+
const auto applyArgToParamElt =
8229+
(path.end() - 2)->getAs<LocatorPathElt::ApplyArgToParam>();
82298230
if (auto paramDecl =
82308231
getParameterAt(decl, applyArgToParamElt->getParamIdx())) {
82318232
emitDiagnosticAt(

test/Constraints/variadic_generic_functions.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking
22

33
func debugPrint<each T>(_ items: repeat each T)
44
where repeat each T: CustomDebugStringConvertible
@@ -83,25 +83,33 @@ do {
8383

8484
// apple/swift#69432 - Passing nil to a parameter pack fails to produce diagnostic for expression
8585
do {
86-
func foo<each T>(_ value: repeat each T) {} // expected-note {{in inferring pack element #1 of 'value'}}
87-
// expected-note@-1 {{in inferring pack element #1 of 'value'}}
88-
// expected-note@-2 {{in inferring pack element #2 of 'value'}}
86+
func foo<each T>(_ value: repeat each T) {} // expected-note {{in inferring pack element #0 of 'value'}}
87+
// expected-note@-1 {{in inferring pack element #0 of 'value'}}
88+
// expected-note@-2 {{in inferring pack element #1 of 'value'}}
8989

9090
foo(nil) // expected-error {{'nil' requires a contextual type}}
9191
foo(nil, 1) // expected-error {{'nil' requires a contextual type}}
9292
foo(2, nil) // expected-error {{'nil' requires a contextual type}}
9393

94-
func bar<each T, U, each W>(_ t: repeat each T, u: U, w: repeat each W) {} // expected-note {{in inferring pack element #3 of 'w'}}
95-
// expected-note@-1 {{in inferring pack element #4 of 't'}}
94+
func bar<each T, U, each W>(_ t: repeat each T, u: U, w: repeat each W) {} // expected-note {{in inferring pack element #2 of 'w'}}
95+
// expected-note@-1 {{in inferring pack element #3 of 't'}}
9696

9797
bar(1, 2, 3, nil, "Hello", u: 3, w: 4, 8, nil) // expected-error {{'nil' requires a contextual type}}
9898
// expected-error@-1 {{'nil' requires a contextual type}}
9999

100100

101101
func fooWithOverload(_ value: Int) {}
102102
func fooWithOverload<each T>(_ value: repeat each T) {}
103-
// expected-note@-1 {{in inferring pack element #5 of 'value'}}
103+
// expected-note@-1 {{in inferring pack element #4 of 'value'}}
104104

105105
fooWithOverload(0, 1, 2, 3, nil) // expected-error {{'nil' requires a contextual type}}
106106

107+
struct Foo<each T> {
108+
init(_ value: repeat each T) {}
109+
// expected-note@-1 {{in inferring pack element #0 of 'value'}}
110+
// expected-note@-2 {{in inferring pack element #0 of 'value'}}
111+
}
112+
113+
_ = Foo(nil) // expected-error {{'nil' requires a contextual type}}
114+
_ = Foo(nil, 1) // expected-error {{'nil' requires a contextual type}}
107115
}

0 commit comments

Comments
 (0)