Skip to content

[ConstraintSystem] Temporary revert 84a3db45db96 as a workaround for rdar://problem/62842651 #31569

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 2 commits into from
May 6, 2020
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
36 changes: 12 additions & 24 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4392,34 +4392,22 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
auto meta1 = cast<AnyMetatypeType>(desugar1);
auto meta2 = cast<AnyMetatypeType>(desugar2);

auto instanceType1 = meta1->getInstanceType();
auto instanceType2 = meta2->getInstanceType();

// A.Type < B.Type if A < B and both A and B are classes.
// P.Type < Q.Type if P < Q, both P and Q are protocols, and P.Type
// and Q.Type are both existential metatypes.
auto getSubKind = [&]() -> ConstraintKind {
auto subKind = std::min(kind, ConstraintKind::Subtype);

// If we have existential metatypes, we need to perform subtyping.
if (!isa<MetatypeType>(meta1))
return subKind;

// If the LHS cannot be a type with a superclass, we can perform a bind.
if (!instanceType1->isTypeVariableOrMember() &&
!instanceType1->mayHaveSuperclass())
return ConstraintKind::Bind;

// If the RHS cannot be a class type, we can perform a bind.
if (!instanceType2->isTypeVariableOrMember() &&
!instanceType2->getClassOrBoundGenericClass())
return ConstraintKind::Bind;

return subKind;
};
// 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::Bind;
}

auto result =
matchTypes(instanceType1, instanceType2, getSubKind(), subflags,
matchTypes(instanceType1, instanceType2, subKind, subflags,
locator.withPathElement(ConstraintLocator::InstanceType));

// If matching of the instance types resulted in the failure make sure
Expand Down
12 changes: 12 additions & 0 deletions test/Constraints/rdar62842651.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this test be in test/SILGen then? (And it can use -emit-silgen instead of -emit-sil, which also runs mandatory passes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem where type-checker infers types incorrectly I just could think of an easier and more robust way to verify it in test-case then check emitted SIL.

Copy link
Contributor Author

@xedin xedin May 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to match against constraint system output because that would just become a constant pain to maintain if anything changes with locators and/or type variables.


class A {}
class B: A {}

func test<T>(_ type: T.Type) -> T? {
fatalError()
}

// CHECK: [[RESULT:%.*]] = function_ref @$s12rdar628426514testyxSgxmlF
// CHECK-NEXT: apply [[RESULT]]<B>({{.*}})
let _: A? = test(B.self)