Skip to content

Commit af90907

Browse files
committed
[CSSimplify] Look through InstanceType while attempting to diagnose conformance failures
Matching existential types could introduce `InstanceType` element at the end of the locator path, it's okay to look through them to diagnose the underlying issue.
1 parent 9953b1e commit af90907

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9124,6 +9124,13 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
91249124
continue;
91259125
}
91269126

9127+
// Matching existentials could introduce constraints with `instance type`
9128+
// element at the end if the confirming type wasn't fully resolved.
9129+
if (path.back().is<LocatorPathElt::InstanceType>()) {
9130+
path.pop_back();
9131+
continue;
9132+
}
9133+
91279134
break;
91289135
}
91299136

@@ -9217,7 +9224,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
92179224
}
92189225
}
92199226

9220-
if (loc->isLastElement<LocatorPathElt::MemberRefBase>()) {
9227+
if (path.back().is<LocatorPathElt::MemberRefBase>()) {
92219228
auto *fix = ContextualMismatch::create(*this, protocolTy, type, loc);
92229229
if (!recordFix(fix))
92239230
return SolutionKind::Solved;
@@ -9227,7 +9234,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
92279234
// for example to `AnyHashable`.
92289235
if ((kind == ConstraintKind::ConformsTo ||
92299236
kind == ConstraintKind::NonisolatedConformsTo) &&
9230-
loc->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
9237+
path.back().is<LocatorPathElt::ApplyArgToParam>()) {
92319238
auto *fix = AllowArgumentMismatch::create(*this, type, protocolTy, loc);
92329239
return recordFix(fix, /*impact=*/2) ? SolutionKind::Error
92339240
: SolutionKind::Solved;

test/Constraints/existential_metatypes.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,15 @@ func testNestedMetatype() {
124124
struct S: P {}
125125

126126
func bar<T>(_ x: T) -> T.Type { type(of: x) }
127-
func foo(_ x: P.Type.Type) { }
127+
func foo1(_ x: P.Type) {}
128+
func foo2(_ x: P.Type.Type) { }
129+
130+
foo1(bar(S.self)) // expected-error {{argument type 'S.Type' does not conform to expected type 'P'}}
128131

129132
// Make sure we don't crash.
130-
foo(bar(S.self))
133+
foo2(bar(S.self))
131134

132135
// FIXME: Bad diagnostic
133136
// https://github.com/swiftlang/swift/issues/83991
134-
foo(bar(0)) // expected-error {{failed to produce diagnostic for expression}}
137+
foo2(bar(0)) // expected-error {{failed to produce diagnostic for expression}}
135138
}

test/Generics/inverse_generics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,5 +538,5 @@ func testYap(_ y: Yapping<NC>) {
538538
protocol Veggie: ~Copyable {}
539539
func generalized(_ x: Any.Type) {}
540540
func testMetatypes(_ t: (any Veggie & ~Copyable).Type) {
541-
generalized(t) // expected-error {{cannot convert value of type '(any Veggie & ~Copyable).Type' to expected argument type 'any Any.Type'}}
541+
generalized(t) // expected-error {{argument type 'any Veggie & ~Copyable' does not conform to expected type 'Copyable'}}
542542
}

0 commit comments

Comments
 (0)