[CSSimplify] Diagnose an attempt to match non-existential type to an existential one

Resolves: rdar://159401910
This commit is contained in:
Pavel Yaskevich
2025-10-06 09:47:14 -07:00
parent af909078e5
commit 36fc1e6c43
2 changed files with 24 additions and 5 deletions

View File

@@ -8750,7 +8750,27 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
return SolutionKind::Solved;
}
return matchExistentialTypes(type, protocol, kind, flags, locator);
auto result = matchExistentialTypes(type, protocol, kind, flags, locator);
if (shouldAttemptFixes() && result.isFailure()) {
auto *loc = getConstraintLocator(locator);
if (loc->isLastElement<LocatorPathElt::InstanceType>())
loc = getConstraintLocator(loc->getAnchor(), loc->getPath().drop_back());
ConstraintFix *fix = nullptr;
if (loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
fix = AllowArgumentMismatch::create(*this, type, protocol, loc);
} else if (loc->isLastElement<LocatorPathElt::ContextualType>()) {
fix = ContextualMismatch::create(*this, type, protocol, loc);
}
if (fix) {
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
}
}
return result;
}
void ConstraintSystem::recordSynthesizedConformance(

View File

@@ -120,6 +120,8 @@ func parameterizedExistentials() {
pt = ppt // expected-error {{cannot assign value of type 'any PP4<Int>.Type' to type 'any P4<Int>.Type'}}
}
// https://github.com/swiftlang/swift/issues/83991
func testNestedMetatype() {
struct S: P {}
@@ -131,8 +133,5 @@ func testNestedMetatype() {
// Make sure we don't crash.
foo2(bar(S.self))
// FIXME: Bad diagnostic
// https://github.com/swiftlang/swift/issues/83991
foo2(bar(0)) // expected-error {{failed to produce diagnostic for expression}}
foo2(bar(0)) // expected-error {{cannot convert value of type 'Int' to expected argument type 'any P.Type'}}
}