Skip to content

Commit 3b4be42

Browse files
committed
[ConstraintSystem] Record opened pack expansion type in the locator element
1 parent 85c1632 commit 3b4be42

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<TypeBase> {
1182+
public:
1183+
PackExpansionType(Type openedPackExpansionTy)
1184+
: StoredPointerElement(PathElementKind::PackExpansionType,
1185+
openedPackExpansionTy.getPointer()) {
1186+
assert(openedPackExpansionTy);
1187+
}
1188+
1189+
Type 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
@@ -11331,24 +11331,16 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1133111331

1133211332
bool ConstraintSystem::resolvePackExpansion(TypeVariableType *typeVar,
1133311333
ConstraintLocatorBuilder locator) {
11334-
// TODO: Pack expansion type variable can carry opened type.
11335-
auto expansion =
11336-
llvm::find_if(OpenedPackExpansionTypes, [&](const auto &expansion) {
11337-
return expansion.second->isEqual(typeVar);
11338-
});
11339-
11340-
assert(expansion != OpenedPackExpansionTypes.end());
11341-
11342-
assignFixedType(typeVar, expansion->first, getConstraintLocator(locator));
11343-
11344-
if (isDebugMode()) {
11345-
PrintOptions PO;
11346-
PO.PrintTypesForDebugging = true;
11347-
llvm::errs().indent(solverState->getCurrentIndent())
11348-
<< "(pack expansion variable " << typeVar->getString(PO)
11349-
<< " is bound to '" << expansion->first->getString(PO) << "')\n";
11334+
Type openedExpansionType;
11335+
if (auto last = locator.last()) {
11336+
auto expansionElt = last->castTo<LocatorPathElt::PackExpansionType>();
11337+
openedExpansionType = expansionElt.getOpenedType();
1135011338
}
1135111339

11340+
if (!openedExpansionType)
11341+
return false;
11342+
11343+
assignFixedType(typeVar, openedExpansionType, getConstraintLocator(locator));
1135211344
return true;
1135311345
}
1135411346

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)