Skip to content

[CSSimplify] Delay binding metatype instance types if one side is type variable #20786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,21 +1962,23 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
auto meta1 = cast<AnyMetatypeType>(desugar1);
auto meta2 = cast<AnyMetatypeType>(desugar2);

ConstraintKind subKind = ConstraintKind::Equal;
// A.Type < B.Type if A < B and both A and B are classes.
if (isa<MetatypeType>(meta1) &&
meta1->getInstanceType()->mayHaveSuperclass() &&
meta2->getInstanceType()->getClassOrBoundGenericClass())
subKind = std::min(kind, ConstraintKind::Subtype);
// P.Type < Q.Type if P < Q, both P and Q are protocols, and P.Type
// and Q.Type are both existential metatypes.
else if (isa<ExistentialMetatypeType>(meta1))
subKind = std::min(kind, ConstraintKind::Subtype);

return matchTypes(meta1->getInstanceType(), meta2->getInstanceType(),
subKind, subflags,
locator.withPathElement(
ConstraintLocator::InstanceType));
// and Q.Type are both existential metatypes
auto subKind = std::min(kind, ConstraintKind::Subtype);
// If instance types can't have a subtype relationship
// it means that such types can be simply equated.
auto instanceType1 = meta1->getInstanceType();
auto instanceType2 = meta2->getInstanceType();
if (isa<MetatypeType>(meta1) &&
!(instanceType1->mayHaveSuperclass() &&
instanceType2->getClassOrBoundGenericClass())) {
subKind = ConstraintKind::Equal;
}

return matchTypes(
instanceType1, instanceType2, subKind, subflags,
locator.withPathElement(ConstraintLocator::InstanceType));
}

case TypeKind::Function: {
Expand Down
16 changes: 16 additions & 0 deletions test/Constraints/rdar44816848.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-typecheck-verify-swift

class A {}
class B: A {}
class C: A {}

struct S {
func foo<T: A>(types: [T.Type]) {}
}

func bar(_ s: S, _ forced_s: S!) {
s.foo(types: [A.self, B.self]) // ok
s.foo(types: [B.self, A.self]) // ok
forced_s.foo(types: [A.self, B.self]) // ok
forced_s.foo(types: [B.self, A.self]) // ok
}