Skip to content

Commit 4af129e

Browse files
authored
Merge pull request #11905 from rudkx/refactor-match-types
[Constraint system] Hoist some common code in matchTypes.
2 parents c63edac + 12587fd commit 4af129e

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,18 +1608,25 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
16081608

16091609
// If either (or both) types are type variables, unify the type variables.
16101610
if (typeVar1 || typeVar2) {
1611+
// Handle the easy case of both being type variables, and being
1612+
// identical, first.
1613+
if (typeVar1 && typeVar2) {
1614+
auto rep1 = getRepresentative(typeVar1);
1615+
auto rep2 = getRepresentative(typeVar2);
1616+
if (rep1 == rep2) {
1617+
// We already merged these two types, so this constraint is
1618+
// trivially solved.
1619+
return SolutionKind::Solved;
1620+
}
1621+
}
1622+
16111623
switch (kind) {
16121624
case ConstraintKind::Bind:
16131625
case ConstraintKind::BindToPointerType:
16141626
case ConstraintKind::Equal: {
16151627
if (typeVar1 && typeVar2) {
16161628
auto rep1 = getRepresentative(typeVar1);
16171629
auto rep2 = getRepresentative(typeVar2);
1618-
if (rep1 == rep2) {
1619-
// We already merged these two types, so this constraint is
1620-
// trivially solved.
1621-
return SolutionKind::Solved;
1622-
}
16231630

16241631
// If exactly one of the type variables can bind to an lvalue, we
16251632
// can't merge these two type variables.
@@ -1675,28 +1682,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
16751682
assignFixedType(typeVar1, type2);
16761683
}
16771684
return SolutionKind::Solved;
1678-
} else if (typeVar1 && typeVar2) {
1679-
auto rep1 = getRepresentative(typeVar1);
1680-
auto rep2 = getRepresentative(typeVar2);
1681-
if (rep1 == rep2) {
1682-
return SolutionKind::Solved;
1683-
}
16841685
}
16851686

16861687
return formUnsolvedResult();
16871688
}
16881689

16891690
case ConstraintKind::ArgumentTupleConversion:
16901691
case ConstraintKind::Conversion:
1691-
if (typeVar1 && typeVar2) {
1692-
auto rep1 = getRepresentative(typeVar1);
1693-
auto rep2 = getRepresentative(typeVar2);
1694-
if (rep1 == rep2) {
1695-
// We already merged these two types, so this constraint is
1696-
// trivially solved.
1697-
return SolutionKind::Solved;
1698-
}
1699-
}
17001692
LLVM_FALLTHROUGH;
17011693

17021694
case ConstraintKind::Subtype:
@@ -1739,12 +1731,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
17391731
case ConstraintKind::OperatorArgumentConversion:
17401732
// We couldn't solve this constraint. If only one of the types is a type
17411733
// variable, perhaps we can do something with it below.
1742-
if (typeVar1 && typeVar2) {
1743-
if (typeVar1 == typeVar2) return SolutionKind::Solved;
1744-
1734+
if (typeVar1 && typeVar2)
17451735
return formUnsolvedResult();
1746-
}
1747-
17481736
break;
17491737

17501738
case ConstraintKind::ApplicableFunction:

0 commit comments

Comments
 (0)