Skip to content

Commit e355fd8

Browse files
authored
Merge pull request #64951 from rjmccall/pack-expansions-lowering-verifier
Fix the type-lowering verifier to handle pack expansions
2 parents 0488c5a + e609bdf commit e355fd8

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,14 +1260,16 @@ class TypeConverter {
12601260
/// Check the result of
12611261
/// getTypeLowering(AbstractionPattern,Type,TypeExpansionContext).
12621262
void verifyLowering(const TypeLowering &, AbstractionPattern origType,
1263-
Type origSubstType, TypeExpansionContext forExpansion);
1263+
CanType origSubstType,
1264+
TypeExpansionContext forExpansion);
12641265
bool
1265-
visitAggregateLeaves(Lowering::AbstractionPattern origType, Type substType,
1266+
visitAggregateLeaves(Lowering::AbstractionPattern origType,
1267+
CanType substType,
12661268
TypeExpansionContext context,
1267-
std::function<bool(Type, Lowering::AbstractionPattern,
1269+
std::function<bool(CanType, Lowering::AbstractionPattern,
12681270
ValueDecl *, Optional<unsigned>)>
12691271
isLeafAggregate,
1270-
std::function<bool(Type, Lowering::AbstractionPattern,
1272+
std::function<bool(CanType, Lowering::AbstractionPattern,
12711273
ValueDecl *, Optional<unsigned>)>
12721274
visit);
12731275
#endif

lib/SIL/IR/TypeLowering.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,74 +2712,81 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,
27122712

27132713
#ifndef NDEBUG
27142714
bool TypeConverter::visitAggregateLeaves(
2715-
Lowering::AbstractionPattern origType, Type substType,
2715+
Lowering::AbstractionPattern origType, CanType substType,
27162716
TypeExpansionContext context,
2717-
std::function<bool(Type, Lowering::AbstractionPattern, ValueDecl *,
2717+
std::function<bool(CanType, Lowering::AbstractionPattern, ValueDecl *,
27182718
Optional<unsigned>)>
27192719
isLeafAggregate,
2720-
std::function<bool(Type, Lowering::AbstractionPattern, ValueDecl *,
2720+
std::function<bool(CanType, Lowering::AbstractionPattern, ValueDecl *,
27212721
Optional<unsigned>)>
27222722
visit) {
2723-
llvm::SmallSet<std::tuple<TypeBase *, ValueDecl *, unsigned>, 16> visited;
2723+
llvm::SmallSet<std::tuple<CanType, ValueDecl *, unsigned>, 16> visited;
27242724
llvm::SmallVector<
2725-
std::tuple<TypeBase *, AbstractionPattern, ValueDecl *, unsigned>, 16>
2725+
std::tuple<CanType, AbstractionPattern, ValueDecl *, unsigned>, 16>
27262726
worklist;
27272727
auto insertIntoWorklist = [&visited,
2728-
&worklist](Type substTy, AbstractionPattern origTy,
2728+
&worklist](CanType substTy,
2729+
AbstractionPattern origTy,
27292730
ValueDecl *field,
27302731
Optional<unsigned> maybeIndex) -> bool {
27312732
unsigned index = maybeIndex.value_or(UINT_MAX);
2732-
if (!visited.insert({substTy.getPointer(), field, index}).second)
2733+
if (!visited.insert({substTy, field, index}).second)
27332734
return false;
2734-
worklist.push_back({substTy.getPointer(), origTy, field, index});
2735+
worklist.push_back({substTy, origTy, field, index});
27352736
return true;
27362737
};
27372738
auto popFromWorklist = [&worklist]()
2738-
-> std::tuple<Type, AbstractionPattern, ValueDecl *, Optional<unsigned>> {
2739-
TypeBase *ty;
2739+
-> std::tuple<CanType, AbstractionPattern, ValueDecl *, Optional<unsigned>> {
2740+
CanType ty;
27402741
AbstractionPattern origTy = AbstractionPattern::getOpaque();
27412742
ValueDecl *field;
27422743
unsigned index;
27432744
std::tie(ty, origTy, field, index) = worklist.pop_back_val();
27442745
Optional<unsigned> maybeIndex;
27452746
if (index != UINT_MAX)
27462747
maybeIndex = {index};
2747-
return {ty->getCanonicalType(), origTy, field, index};
2748+
return {ty, origTy, field, index};
27482749
};
2749-
auto isAggregate = [](Type ty) {
2750-
return ty->is<SILPackType>() || ty->is<TupleType>() || ty->getEnumOrBoundGenericEnum() ||
2751-
ty->getStructOrBoundGenericStruct();
2750+
auto isAggregate = [](CanType ty) {
2751+
return isa<SILPackType>(ty) ||
2752+
isa<TupleType>(ty) ||
2753+
isa<PackExpansionType>(ty) ||
2754+
ty.getEnumOrBoundGenericEnum() ||
2755+
ty.getStructOrBoundGenericStruct();
27522756
};
27532757
insertIntoWorklist(substType, origType, nullptr, llvm::None);
27542758
while (!worklist.empty()) {
2755-
Type ty;
2759+
CanType ty;
27562760
AbstractionPattern origTy = AbstractionPattern::getOpaque();
27572761
ValueDecl *field;
27582762
Optional<unsigned> index;
27592763
std::tie(ty, origTy, field, index) = popFromWorklist();
27602764
if (isAggregate(ty) && !isLeafAggregate(ty, origTy, field, index)) {
2761-
if (auto packTy = ty->getAs<SILPackType>()) {
2765+
if (auto packTy = dyn_cast<SILPackType>(ty)) {
27622766
for (auto packIndex : indices(packTy->getElementTypes())) {
27632767
auto origElementTy = origTy.getPackElementType(packIndex);
2764-
auto substElementTy =
2765-
packTy->getElementType(packIndex)->getCanonicalType();
2768+
auto substElementTy = packTy.getElementType(packIndex);
27662769
substElementTy =
27672770
computeLoweredRValueType(context, origElementTy, substElementTy);
27682771
insertIntoWorklist(substElementTy, origElementTy, nullptr,
27692772
packIndex);
27702773
}
2771-
} else if (auto tupleTy = ty->getAs<TupleType>()) {
2774+
} else if (auto tupleTy = dyn_cast<TupleType>(ty)) {
27722775
unsigned tupleIndex = 0;
27732776
origTy.forEachExpandedTupleElement(
2774-
CanTupleType(tupleTy),
2777+
tupleTy,
27752778
[&](auto origElementTy, auto substElementTy, auto element) {
27762779
substElementTy =
27772780
substOpaqueTypesWithUnderlyingTypes(substElementTy, context);
27782781
insertIntoWorklist(substElementTy, origElementTy, nullptr,
27792782
tupleIndex);
27802783
++tupleIndex;
27812784
});
2782-
} else if (auto *decl = ty->getStructOrBoundGenericStruct()) {
2785+
} else if (auto expansion = dyn_cast<PackExpansionType>(ty)) {
2786+
insertIntoWorklist(expansion.getPatternType(),
2787+
origTy.getPackExpansionPatternType(),
2788+
field, index);
2789+
} else if (auto *decl = ty.getStructOrBoundGenericStruct()) {
27832790
for (auto *structField : decl->getStoredProperties()) {
27842791
auto subMap = ty->getContextSubstitutionMap(&M, decl);
27852792
auto substFieldTy =
@@ -2794,7 +2801,7 @@ bool TypeConverter::visitAggregateLeaves(
27942801
insertIntoWorklist(substFieldTy, origFieldType, structField,
27952802
llvm::None);
27962803
}
2797-
} else if (auto *decl = ty->getEnumOrBoundGenericEnum()) {
2804+
} else if (auto *decl = ty.getEnumOrBoundGenericEnum()) {
27982805
auto subMap = ty->getContextSubstitutionMap(&M, decl);
27992806
for (auto *element : decl->getAllElements()) {
28002807
if (!element->hasAssociatedValues())
@@ -2827,16 +2834,17 @@ bool TypeConverter::visitAggregateLeaves(
28272834
}
28282835

28292836
void TypeConverter::verifyLowering(const TypeLowering &lowering,
2830-
AbstractionPattern origType, Type substType,
2837+
AbstractionPattern origType,
2838+
CanType substType,
28312839
TypeExpansionContext forExpansion) {
28322840
// Non-trivial lowerings should always be lexical unless all non-trivial
28332841
// fields are eager move.
28342842
if (!lowering.isTrivial() && !lowering.isLexical()) {
28352843
if (lowering.getRecursiveProperties().isInfinite())
28362844
return;
2837-
auto getLifetimeAnnotation = [](Type ty) -> LifetimeAnnotation {
2845+
auto getLifetimeAnnotation = [](CanType ty) -> LifetimeAnnotation {
28382846
NominalTypeDecl *nominal;
2839-
if (!(nominal = ty->getAnyNominal()))
2847+
if (!(nominal = ty.getAnyNominal()))
28402848
return LifetimeAnnotation::None;
28412849
return nominal->getLifetimeAnnotation();
28422850
};
@@ -2865,7 +2873,7 @@ void TypeConverter::verifyLowering(const TypeLowering &lowering,
28652873

28662874
// If the leaf is the whole type, verify that it is annotated
28672875
// @_eagerMove.
2868-
if (ty->getCanonicalType() == substType->getCanonicalType())
2876+
if (ty == substType)
28692877
return getLifetimeAnnotation(ty) == LifetimeAnnotation::EagerMove;
28702878

28712879
auto &tyLowering = getTypeLowering(origTy, ty, forExpansion);

test/SILGen/variadic-generic-tuples.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,6 @@ func testFancyTuple_concrete() {
331331
func testFancyTuple_pack<each T>(values: repeat each T) {
332332
FancyTuple<Int, String, repeat each T, Bool>(x: (1, "hi", repeat each values, false)).makeTuple()
333333
}
334+
335+
// rdar://107664237
336+
func f<each T>() -> (repeat Array<each T>) {}

0 commit comments

Comments
 (0)