Skip to content

Commit 36fc1e6

Browse files
committed
[CSSimplify] Diagnose an attempt to match non-existential type to an existential one
Resolves: rdar://159401910
1 parent af90907 commit 36fc1e6

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8750,7 +8750,27 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
87508750
return SolutionKind::Solved;
87518751
}
87528752

8753-
return matchExistentialTypes(type, protocol, kind, flags, locator);
8753+
auto result = matchExistentialTypes(type, protocol, kind, flags, locator);
8754+
8755+
if (shouldAttemptFixes() && result.isFailure()) {
8756+
auto *loc = getConstraintLocator(locator);
8757+
8758+
if (loc->isLastElement<LocatorPathElt::InstanceType>())
8759+
loc = getConstraintLocator(loc->getAnchor(), loc->getPath().drop_back());
8760+
8761+
ConstraintFix *fix = nullptr;
8762+
if (loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
8763+
fix = AllowArgumentMismatch::create(*this, type, protocol, loc);
8764+
} else if (loc->isLastElement<LocatorPathElt::ContextualType>()) {
8765+
fix = ContextualMismatch::create(*this, type, protocol, loc);
8766+
}
8767+
8768+
if (fix) {
8769+
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
8770+
}
8771+
}
8772+
8773+
return result;
87548774
}
87558775

87568776
void ConstraintSystem::recordSynthesizedConformance(

test/Constraints/existential_metatypes.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func parameterizedExistentials() {
120120
pt = ppt // expected-error {{cannot assign value of type 'any PP4<Int>.Type' to type 'any P4<Int>.Type'}}
121121
}
122122

123+
// https://github.com/swiftlang/swift/issues/83991
124+
123125
func testNestedMetatype() {
124126
struct S: P {}
125127

@@ -131,8 +133,5 @@ func testNestedMetatype() {
131133

132134
// Make sure we don't crash.
133135
foo2(bar(S.self))
134-
135-
// FIXME: Bad diagnostic
136-
// https://github.com/swiftlang/swift/issues/83991
137-
foo2(bar(0)) // expected-error {{failed to produce diagnostic for expression}}
136+
foo2(bar(0)) // expected-error {{cannot convert value of type 'Int' to expected argument type 'any P.Type'}}
138137
}

0 commit comments

Comments
 (0)