Skip to content

Sema: Small matchPackExpansionTypes cleanup #78320

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
Jan 2, 2025
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
49 changes: 20 additions & 29 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2405,40 +2405,31 @@ ConstraintSystem::matchPackExpansionTypes(PackExpansionType *expansion1,
}
}

// If both sides are expanded or neither side is, just match them
auto *const pack1 = pattern1->getAs<PackType>();
auto *const pack2 = pattern2->getAs<PackType>();

// If both sides are expanded or neither side is, proceed to matching them
// directly.
if (pattern1->is<PackType>() == pattern2->is<PackType>()) {
return matchTypes(pattern1, pattern2, kind, flags, locator);

// If the right hand side is expanded, we have something like
// Foo<$T0>... vs Pack{Foo<Int>, Foo<String>}...; We're going to
// bind $T0 to Pack{Int, String}.
} else if (!pattern1->is<PackType>() && pattern2->is<PackType>()) {
if (auto *pack2 = pattern2->getAs<PackType>()) {
if (auto *pack1 = replaceTypeVariablesWithFreshPacks(
*this, pattern1, pack2, locator)) {
addConstraint(kind, pack1, pack2, locator);
return getTypeMatchSuccess();
}
// Otherwise, we have something like `Foo<$T0>` vs.
// `Pack{Foo<Int>, Foo<String>}` or vice versa.
// We're going to bind `$T0` to `Pack{Int, String}` and unfold `Foo<$T0>` into
// `Pack{Foo<$T3>, Foo<$T4>} first.
if ((bool)pack1 != (bool)pack2) {
if (pack1) {
pattern2 =
replaceTypeVariablesWithFreshPacks(*this, pattern2, pack1, locator);
} else {
pattern1 =
replaceTypeVariablesWithFreshPacks(*this, pattern1, pack2, locator);
}

return getTypeMatchFailure(locator);

// If the left hand side is expanded, we have something like
// Pack{Foo<Int>, Foo<String>}... vs Foo<$T0>...; We're going to
// bind $T0 to Pack{Int, String}.
} else {
assert(pattern1->is<PackType>() && !pattern2->is<PackType>());
if (auto *pack1 = pattern1->getAs<PackType>()) {
if (auto *pack2 = replaceTypeVariablesWithFreshPacks(
*this, pattern2, pack1, locator)) {
addConstraint(kind, pack1, pack2, locator);
return getTypeMatchSuccess();
}
if (!(pattern1 && pattern2)) {
return getTypeMatchFailure(locator);
}

return getTypeMatchFailure(locator);
}

// Continue matching.
return matchTypes(pattern1, pattern2, kind, flags, locator);
}

/// Check where a representation is a subtype of another.
Expand Down