Skip to content

Commit e58a75e

Browse files
authored
Merge pull request #31608 from hamishknight/too-meta
2 parents 77da956 + 9d19a92 commit e58a75e

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,34 +4463,22 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
44634463
auto meta1 = cast<AnyMetatypeType>(desugar1);
44644464
auto meta2 = cast<AnyMetatypeType>(desugar2);
44654465

4466-
auto instanceType1 = meta1->getInstanceType();
4467-
auto instanceType2 = meta2->getInstanceType();
4468-
44694466
// A.Type < B.Type if A < B and both A and B are classes.
44704467
// P.Type < Q.Type if P < Q, both P and Q are protocols, and P.Type
4471-
// and Q.Type are both existential metatypes.
4472-
auto getSubKind = [&]() -> ConstraintKind {
4473-
auto subKind = std::min(kind, ConstraintKind::Subtype);
4474-
4475-
// If we have existential metatypes, we need to perform subtyping.
4476-
if (!isa<MetatypeType>(meta1))
4477-
return subKind;
4478-
4479-
// If the LHS cannot be a type with a superclass, we can perform a bind.
4480-
if (!instanceType1->isTypeVariableOrMember() &&
4481-
!instanceType1->mayHaveSuperclass())
4482-
return ConstraintKind::Bind;
4483-
4484-
// If the RHS cannot be a class type, we can perform a bind.
4485-
if (!instanceType2->isTypeVariableOrMember() &&
4486-
!instanceType2->getClassOrBoundGenericClass())
4487-
return ConstraintKind::Bind;
4488-
4489-
return subKind;
4490-
};
4468+
// and Q.Type are both existential metatypes
4469+
auto subKind = std::min(kind, ConstraintKind::Subtype);
4470+
// If instance types can't have a subtype relationship
4471+
// it means that such types can be simply equated.
4472+
auto instanceType1 = meta1->getInstanceType();
4473+
auto instanceType2 = meta2->getInstanceType();
4474+
if (isa<MetatypeType>(meta1) &&
4475+
!(instanceType1->mayHaveSuperclass() &&
4476+
instanceType2->getClassOrBoundGenericClass())) {
4477+
subKind = ConstraintKind::Bind;
4478+
}
44914479

44924480
auto result =
4493-
matchTypes(instanceType1, instanceType2, getSubKind(), subflags,
4481+
matchTypes(instanceType1, instanceType2, subKind, subflags,
44944482
locator.withPathElement(ConstraintLocator::InstanceType));
44954483

44964484
// If matching of the instance types resulted in the failure make sure

test/Constraints/rdar62842651.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s
2+
3+
class A {}
4+
class B: A {}
5+
6+
func test<T>(_ type: T.Type) -> T? {
7+
fatalError()
8+
}
9+
10+
// CHECK: [[RESULT:%.*]] = function_ref @$s12rdar628426514testyxSgxmlF
11+
// CHECK-NEXT: apply [[RESULT]]<B>({{.*}})
12+
let _: A? = test(B.self)

0 commit comments

Comments
 (0)