Skip to content

Commit d83ec7b

Browse files
committed
[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
1 parent b3e9cf3 commit d83ec7b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,6 +4311,15 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
43114311
break;
43124312
}
43134313

4314+
if ((isExpr<ArrayExpr>(anchor) || isExpr<DictionaryExpr>(anchor)) &&
4315+
last.is<LocatorPathElt::TupleElement>()) {
4316+
auto *fix = CollectionElementContextualMismatch::create(
4317+
*this, type1, type2, getConstraintLocator(anchor, path));
4318+
if (recordFix(fix, /*impact=*/2))
4319+
return getTypeMatchFailure(locator);
4320+
break;
4321+
}
4322+
43144323
// TODO(diagnostics): If there are any requirement failures associated
43154324
// with result types which are part of a function type conversion,
43164325
// let's record general conversion mismatch in order for it to capture

test/Constraints/protocols.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,11 @@ do {
577577

578578
isFooableError(overloaded()) // Ok
579579
}
580+
581+
do {
582+
func takesFooables(_: [any Fooable]) {}
583+
584+
func test(v: String) {
585+
takesFooables([v]) // expected-error {{cannot convert value of type 'String' to expected element type 'any Fooable'}}
586+
}
587+
}

0 commit comments

Comments
 (0)