Skip to content

Commit 1fa5ac4

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

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8203,13 +8203,8 @@ 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 = locator->getLastElementAs<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
@@ -8226,6 +8221,8 @@ bool UnableToInferGenericPackElementType::diagnoseAsError() {
82268221
if (auto *calleeLocator = getSolution().getCalleeLocator(locator)) {
82278222
if (const auto choice = getOverloadChoiceIfAvailable(calleeLocator)) {
82288223
if (auto *decl = choice->choice.getDeclOrNull()) {
8224+
const auto applyArgToParamElt =
8225+
(path.end() - 2)->getAs<LocatorPathElt::ApplyArgToParam>();
82298226
if (auto paramDecl =
82308227
getParameterAt(decl, applyArgToParamElt->getParamIdx())) {
82318228
emitDiagnosticAt(

test/Constraints/pack_expansion_types.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,10 @@ func patternInstantiationConcreteValid() {
243243
func patternInstantiationConcreteInvalid() {
244244
let _: Set<Int> = patternInstantiationTupleTest1()
245245
// expected-error@-1 {{cannot convert value of type '(repeat Array<_>)' to specified type 'Set<Int>'}}
246+
// expected-error@-2 {{could not infer pack element #0 from context}}
246247

247248
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Int, _>)' is not convertible to '(Array<Int>, Set<String>)', tuples have a different number of elements}}
249+
// expected-error@-1 {{could not infer pack element #1 from context}}
248250
}
249251

250252
func patternInstantiationGenericValid<each T, each U>(t: repeat each T, u: repeat each U)
@@ -275,6 +277,7 @@ func patternInstantiationGenericInvalid<each T: Hashable>(t: repeat each T) {
275277
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
276278

277279
let _: (repeat Array<each T>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<repeat each T, _>)' is not convertible to '(repeat Array<each T>, Set<String>)', tuples have a different number of elements}}
280+
// expected-error@-1 {{could not infer pack element #1 from context}}
278281
}
279282

280283
// rdar://107996926 - Vanishing metatype of tuple not supported

test/Constraints/variadic_generic_functions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,24 @@ 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

test/Constraints/variadic_generic_types.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,15 @@ do {
106106
}
107107
}
108108
}
109+
110+
// apple/swift#69432 - Passing nil to a parameter pack fails to produce diagnostic for expression
111+
do {
112+
struct Foo<each T> {
113+
init(_ value: repeat each T) {}
114+
// expected-note@-1 {{in inferring pack element #0 of 'value'}}
115+
// expected-note@-2 {{in inferring pack element #0 of 'value'}}
116+
}
117+
118+
_ = Foo(nil) // expected-error {{'nil' requires a contextual type}}
119+
_ = Foo(nil, 1) // expected-error {{'nil' requires a contextual type}}
120+
}

0 commit comments

Comments
 (0)