Skip to content

Commit ed23aec

Browse files
committed
[ConstraintSystem] Record opened pack expansion type in the locator element
1 parent cc2b6ce commit ed23aec

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

include/swift/Sema/ConstraintLocator.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,22 @@ class LocatorPathElt::AnyPatternDecl final
11771177
}
11781178
};
11791179

1180+
class LocatorPathElt::PackExpansionType final
1181+
: public StoredPointerElement<swift::PackExpansionType> {
1182+
public:
1183+
PackExpansionType(swift::PackExpansionType *openedPackExpansionTy)
1184+
: StoredPointerElement(PathElementKind::PackExpansionType,
1185+
openedPackExpansionTy) {
1186+
assert(openedPackExpansionTy);
1187+
}
1188+
1189+
swift::PackExpansionType *getOpenedType() const { return getStoredPointer(); }
1190+
1191+
static bool classof(const LocatorPathElt *elt) {
1192+
return elt->getKind() == PathElementKind::PackExpansionType;
1193+
}
1194+
};
1195+
11801196
namespace details {
11811197
template <typename CustomPathElement>
11821198
class PathElement {

include/swift/Sema/ConstraintLocatorPathElts.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ CUSTOM_LOCATOR_PATH_ELT(PackElement)
202202
/// The shape of a parameter pack.
203203
SIMPLE_LOCATOR_PATH_ELT(PackShape)
204204

205-
/// The type of pack expansion
206-
SIMPLE_LOCATOR_PATH_ELT(PackExpansionType)
205+
/// The type of an "opened" pack expansion
206+
CUSTOM_LOCATOR_PATH_ELT(PackExpansionType)
207207

208208
/// The pattern of a pack expansion.
209209
SIMPLE_LOCATOR_PATH_ELT(PackExpansionPattern)

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11313,24 +11313,16 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1131311313

1131411314
bool ConstraintSystem::resolvePackExpansion(TypeVariableType *typeVar,
1131511315
ConstraintLocatorBuilder locator) {
11316-
// TODO: Pack expansion type variable can carry opened type.
11317-
auto expansion =
11318-
llvm::find_if(OpenedPackExpansionTypes, [&](const auto &expansion) {
11319-
return expansion.second->isEqual(typeVar);
11320-
});
11321-
11322-
assert(expansion != OpenedPackExpansionTypes.end());
11323-
11324-
assignFixedType(typeVar, expansion->first, getConstraintLocator(locator));
11325-
11326-
if (isDebugMode()) {
11327-
PrintOptions PO;
11328-
PO.PrintTypesForDebugging = true;
11329-
llvm::errs().indent(solverState->getCurrentIndent())
11330-
<< "(pack expansion variable " << typeVar->getString(PO)
11331-
<< " is bound to '" << expansion->first->getString(PO) << "')\n";
11316+
Type openedExpansionType;
11317+
if (auto last = locator.last()) {
11318+
auto expansionElt = last->castTo<LocatorPathElt::PackExpansionType>();
11319+
openedExpansionType = expansionElt.getOpenedType();
1133211320
}
1133311321

11322+
if (!openedExpansionType)
11323+
return false;
11324+
11325+
assignFixedType(typeVar, openedExpansionType, getConstraintLocator(locator));
1133411326
return true;
1133511327
}
1133611328

lib/Sema/ConstraintLocator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ void LocatorPathElt::dump(raw_ostream &out) const {
504504
break;
505505

506506
case ConstraintLocator::PackExpansionType:
507-
out << "pack expansion type";
507+
auto expansionElt = elt.castTo<LocatorPathElt::PackExpansionType>();
508+
out << "pack expansion type ("
509+
<< expansionElt.getOpenedType()->getString(PO) << ")";
508510
break;
509511
}
510512
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,15 +1018,17 @@ Type ConstraintSystem::openPackExpansionType(PackExpansionType *expansion,
10181018
return known->second;
10191019

10201020
auto *expansionVar = createTypeVariable(
1021-
getConstraintLocator({}, ConstraintLocator::PackExpansionType),
1021+
getConstraintLocator(
1022+
{}, LocatorPathElt::PackExpansionType(openedPackExpansion)),
10221023
TVO_PackExpansion);
10231024

10241025
// This constraint is important to make sure that pack expansion always
10251026
// has a binding and connect pack expansion var to any type variables
10261027
// that appear in pattern and shape types.
10271028
addUnsolvedConstraint(Constraint::create(
10281029
*this, ConstraintKind::Defaultable, expansionVar, openedPackExpansion,
1029-
getConstraintLocator({}, ConstraintLocator::PackExpansionType)));
1030+
getConstraintLocator(
1031+
{}, LocatorPathElt::PackExpansionType(openedPackExpansion))));
10301032

10311033
OpenedPackExpansionTypes[openedPackExpansion] = expansionVar;
10321034
return expansionVar;

0 commit comments

Comments
 (0)