Skip to content

Commit 6437531

Browse files
committed
[ConstraintSystem] Introduce TVO_PackExpansion
This flag indicates that a type variable could only be bound to PackExpansionType and nothing else.
1 parent 5c2f77c commit 6437531

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,12 @@ class alignas(1 << TypeAlignInBits) TypeBase
400400
NumProtocols : 16
401401
);
402402

403-
SWIFT_INLINE_BITFIELD_FULL(TypeVariableType, TypeBase, 6+32,
403+
SWIFT_INLINE_BITFIELD_FULL(TypeVariableType, TypeBase, 7+31,
404404
/// Type variable options.
405-
Options : 6,
405+
Options : 7,
406406
: NumPadBits,
407407
/// The unique number assigned to this type variable.
408-
ID : 32
408+
ID : 31
409409
);
410410

411411
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+4+1+2+1+1,

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ enum TypeVariableOptions {
335335

336336
/// Whether the type variable can be bound to a pack type or not.
337337
TVO_CanBindToPack = 0x20,
338+
339+
/// Whether the type variable can be bound only to a pack expansion type.
340+
TVO_PackExpansion = 0x40,
338341
};
339342

340343
/// The implementation object for a type variable used within the
@@ -407,6 +410,9 @@ class TypeVariableType::Implementation {
407410
/// Whether this type variable can bind to a PackType.
408411
bool canBindToPack() const { return getRawOptions() & TVO_CanBindToPack; }
409412

413+
/// Whether this type variable can bind only to PackExpansionType.
414+
bool isPackExpansion() const { return getRawOptions() & TVO_PackExpansion; }
415+
410416
/// Whether this type variable prefers a subtype binding over a supertype
411417
/// binding.
412418
bool prefersSubtypeBinding() const {
@@ -650,6 +656,7 @@ class TypeVariableType::Implementation {
650656
ENTRY(TVO_CanBindToHole, "hole");
651657
ENTRY(TVO_PrefersSubtypeBinding, "");
652658
ENTRY(TVO_CanBindToPack, "pack");
659+
ENTRY(TVO_PackExpansion, "pack expansion");
653660
}
654661
#undef ENTRY
655662
}

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,9 +2086,7 @@ static bool isPackExpansionType(Type type) {
20862086
return true;
20872087

20882088
if (auto *typeVar = type->getAs<TypeVariableType>())
2089-
return typeVar->getImpl()
2090-
.getLocator()
2091-
->isLastElement<LocatorPathElt::PackExpansionType>();
2089+
return typeVar->getImpl().isPackExpansion();
20922090

20932091
return false;
20942092
}
@@ -4311,6 +4309,11 @@ ConstraintSystem::matchTypesBindTypeVar(
43114309
type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
43124310
}
43134311

4312+
// FIXME: Add a fix for this.
4313+
if (typeVar->getImpl().isPackExpansion() && !type->is<PackExpansionType>()) {
4314+
return getTypeMatchFailure(locator);
4315+
}
4316+
43144317
// Binding to a pack expansion type is always an error in Swift 6 mode.
43154318
// This indicates that a pack expansion expression was used in a context
43164319
// that doesn't support it.
@@ -4322,7 +4325,7 @@ ConstraintSystem::matchTypesBindTypeVar(
43224325
// generic parameter - `init(_ data: repeat each T)`.
43234326
//
43244327
// See BindTupleOfFunctionParams constraint for more details.
4325-
if (type->is<PackExpansionType>()) {
4328+
if (!typeVar->getImpl().isPackExpansion() && type->is<PackExpansionType>()) {
43264329
bool representsParameterList =
43274330
typeVar->getImpl()
43284331
.getLocator()

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
6969
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToHole);
7070
if (canBindToPack())
7171
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToPack);
72+
if (isPackExpansion())
73+
bindingOptions.push_back(TypeVariableOptions::TVO_PackExpansion);
7274
if (!bindingOptions.empty()) {
7375
OS << " [allows bindings to: ";
7476
interleave(bindingOptions, OS,

0 commit comments

Comments
 (0)