Skip to content

Commit 15c5662

Browse files
committed
[CSSimplify] Remove all InstanceType at the end of a locator to diagnose mismatched existential conversion
1 parent 36fc1e6 commit 15c5662

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8755,8 +8755,17 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
87558755
if (shouldAttemptFixes() && result.isFailure()) {
87568756
auto *loc = getConstraintLocator(locator);
87578757

8758-
if (loc->isLastElement<LocatorPathElt::InstanceType>())
8759-
loc = getConstraintLocator(loc->getAnchor(), loc->getPath().drop_back());
8758+
ArrayRef<LocatorPathElt> path = loc->getPath();
8759+
while (!path.empty()) {
8760+
if (!path.back().is<LocatorPathElt::InstanceType>())
8761+
break;
8762+
8763+
path = path.drop_back();
8764+
}
8765+
8766+
if (path.size() != loc->getPath().size()) {
8767+
loc = getConstraintLocator(loc->getAnchor(), path);
8768+
}
87608769

87618770
ConstraintFix *fix = nullptr;
87628771
if (loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {

test/Constraints/existential_metatypes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ func parameterizedExistentials() {
125125
func testNestedMetatype() {
126126
struct S: P {}
127127

128-
func bar<T>(_ x: T) -> T.Type { type(of: x) }
128+
func bar<T>(_ x: T) -> T.Type { }
129+
func metaBar<T>(_ x: T) -> T.Type.Type { }
129130
func foo1(_ x: P.Type) {}
130131
func foo2(_ x: P.Type.Type) { }
131132

@@ -134,4 +135,5 @@ func testNestedMetatype() {
134135
// Make sure we don't crash.
135136
foo2(bar(S.self))
136137
foo2(bar(0)) // expected-error {{cannot convert value of type 'Int' to expected argument type 'any P.Type'}}
138+
foo2(metaBar(0)) // expected-error {{argument type 'Int' does not conform to expected type 'P'}}
137139
}

0 commit comments

Comments
 (0)