Skip to content

[Constraint system] Hoist some common code in matchTypes. #11905

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
Sep 13, 2017
Merged
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
38 changes: 13 additions & 25 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,18 +1608,25 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,

// If either (or both) types are type variables, unify the type variables.
if (typeVar1 || typeVar2) {
// Handle the easy case of both being type variables, and being
// identical, first.
if (typeVar1 && typeVar2) {
auto rep1 = getRepresentative(typeVar1);
auto rep2 = getRepresentative(typeVar2);
if (rep1 == rep2) {
// We already merged these two types, so this constraint is
// trivially solved.
return SolutionKind::Solved;
}
}

switch (kind) {
case ConstraintKind::Bind:
case ConstraintKind::BindToPointerType:
case ConstraintKind::Equal: {
if (typeVar1 && typeVar2) {
auto rep1 = getRepresentative(typeVar1);
auto rep2 = getRepresentative(typeVar2);
if (rep1 == rep2) {
// We already merged these two types, so this constraint is
// trivially solved.
return SolutionKind::Solved;
}

// If exactly one of the type variables can bind to an lvalue, we
// can't merge these two type variables.
Expand Down Expand Up @@ -1675,28 +1682,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
assignFixedType(typeVar1, type2);
}
return SolutionKind::Solved;
} else if (typeVar1 && typeVar2) {
auto rep1 = getRepresentative(typeVar1);
auto rep2 = getRepresentative(typeVar2);
if (rep1 == rep2) {
return SolutionKind::Solved;
}
}

return formUnsolvedResult();
}

case ConstraintKind::ArgumentTupleConversion:
case ConstraintKind::Conversion:
if (typeVar1 && typeVar2) {
auto rep1 = getRepresentative(typeVar1);
auto rep2 = getRepresentative(typeVar2);
if (rep1 == rep2) {
// We already merged these two types, so this constraint is
// trivially solved.
return SolutionKind::Solved;
}
}
LLVM_FALLTHROUGH;

case ConstraintKind::Subtype:
Expand Down Expand Up @@ -1739,12 +1731,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
case ConstraintKind::OperatorArgumentConversion:
// We couldn't solve this constraint. If only one of the types is a type
// variable, perhaps we can do something with it below.
if (typeVar1 && typeVar2) {
if (typeVar1 == typeVar2) return SolutionKind::Solved;

if (typeVar1 && typeVar2)
return formUnsolvedResult();
}

break;

case ConstraintKind::ApplicableFunction:
Expand Down