[Diagnostics] Diagnose existential mismatch in a literal collection element position

If key or value of a literal collection expression doesn't conform
to protocol(s) expected by the contextual existential type, let's
diagnose that via a tailed collection mismatch fix instead of a
generic conformance one.

Resolves: rdar://103045274
This commit is contained in:
Pavel Yaskevich
2025-05-26 00:28:01 -07:00
parent b3e9cf3424
commit d83ec7b3a5
2 changed files with 17 additions and 0 deletions

View File

@@ -4311,6 +4311,15 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
break;
}
if ((isExpr<ArrayExpr>(anchor) || isExpr<DictionaryExpr>(anchor)) &&
last.is<LocatorPathElt::TupleElement>()) {
auto *fix = CollectionElementContextualMismatch::create(
*this, type1, type2, getConstraintLocator(anchor, path));
if (recordFix(fix, /*impact=*/2))
return getTypeMatchFailure(locator);
break;
}
// TODO(diagnostics): If there are any requirement failures associated
// with result types which are part of a function type conversion,
// let's record general conversion mismatch in order for it to capture

View File

@@ -577,3 +577,11 @@ do {
isFooableError(overloaded()) // Ok
}
do {
func takesFooables(_: [any Fooable]) {}
func test(v: String) {
takesFooables([v]) // expected-error {{cannot convert value of type 'String' to expected element type 'any Fooable'}}
}
}