Skip to content

Commit 0c71cc9

Browse files
committed
AST: Use TypeTransform::transformSubMap() to transform SILBoxType
1 parent 6532a8b commit 0c71cc9

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

include/swift/AST/TypeTransform.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,32 +164,30 @@ case TypeKind::Id:
164164
case TypeKind::SILBox: {
165165
bool changed = false;
166166
auto boxTy = cast<SILBoxType>(base);
167-
#ifndef NDEBUG
168-
// This interface isn't suitable for updating the substitution map in a
169-
// generic SILBox.
170-
for (Type type : boxTy->getSubstitutions().getReplacementTypes()) {
171-
assert(type->isEqual(
172-
doIt(type, TypePosition::Invariant)) &&
173-
"SILBoxType substitutions can't be transformed");
174-
}
175-
#endif
167+
176168
SmallVector<SILField, 4> newFields;
177169
auto *l = boxTy->getLayout();
178170
for (auto f : l->getFields()) {
179171
auto fieldTy = f.getLoweredType();
180-
auto transformed = doIt(fieldTy, TypePosition::Invariant)
181-
->getCanonicalType();
172+
auto transformed = asDerived().transformSILField(
173+
fieldTy, TypePosition::Invariant);
182174
changed |= fieldTy != transformed;
183175
newFields.push_back(SILField(transformed, f.isMutable()));
184176
}
177+
178+
auto oldSubMap = boxTy->getSubstitutions();
179+
auto newSubMap = asDerived().transformSubMap(oldSubMap);
180+
if (oldSubMap && !newSubMap)
181+
return Type();
182+
changed |= (oldSubMap != newSubMap);
185183
if (!changed)
186184
return t;
187185
boxTy = SILBoxType::get(ctx,
188186
SILLayout::get(ctx,
189187
l->getGenericSignature(),
190188
newFields,
191189
l->capturesGenericEnvironment()),
192-
boxTy->getSubstitutions());
190+
newSubMap);
193191
return boxTy;
194192
}
195193

@@ -1013,6 +1011,10 @@ case TypeKind::Id:
10131011
QueryReplacementTypeArray{sig, newSubs},
10141012
LookUpConformanceInModule());
10151013
}
1014+
1015+
CanType transformSILField(CanType fieldTy, TypePosition pos) {
1016+
return doIt(fieldTy, pos)->getCanonicalType();
1017+
}
10161018
};
10171019

10181020
}

lib/AST/TypeSubstitution.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ class TypeSubstituter : public TypeTransform<TypeSubstituter> {
491491
std::optional<Type> transform(TypeBase *type, TypePosition position);
492492

493493
SubstitutionMap transformSubstitutionMap(SubstitutionMap subs);
494+
495+
CanType transformSILField(CanType fieldTy, TypePosition pos);
494496
};
495497

496498
}
@@ -504,17 +506,6 @@ TypeSubstituter::transform(TypeBase *type, TypePosition position) {
504506
"should not be doing AST type-substitution on a lowered SIL type;"
505507
"use SILType::subst");
506508

507-
// Special-case handle SILBoxTypes and substituted SILFunctionTypes;
508-
// we want to structurally substitute the substitutions.
509-
if (auto boxTy = dyn_cast<SILBoxType>(type)) {
510-
auto subMap = boxTy->getSubstitutions();
511-
auto newSubMap = subMap.subst(IFS);
512-
513-
return SILBoxType::get(boxTy->getASTContext(),
514-
boxTy->getLayout(),
515-
newSubMap);
516-
}
517-
518509
if (auto packExpansionTy = dyn_cast<PackExpansionType>(type)) {
519510
auto eltTys = IFS.expandPackExpansionType(packExpansionTy);
520511
if (eltTys.size() == 1)
@@ -617,6 +608,13 @@ SubstitutionMap TypeSubstituter::transformSubstitutionMap(SubstitutionMap subs)
617608
return subs.subst(IFS);
618609
}
619610

611+
CanType TypeSubstituter::transformSILField(CanType fieldTy, TypePosition pos) {
612+
// Type substitution does not walk into the SILBoxType's field types, because
613+
// that's written with respect to the generic signature of the box type,
614+
// and not the input generic signature of the substitution.
615+
return fieldTy;
616+
}
617+
620618
Type Type::subst(SubstitutionMap substitutions,
621619
SubstOptions options) const {
622620
InFlightSubstitutionViaSubMap IFS(substitutions, options);

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class EqualUpToClangTypes
103103
};
104104
};
105105

106+
/// FIXME: This should be removed in favor of fixing ASTDemangler to wrap types in
107+
/// ExistentialType where appropriate.
106108
static bool equalWithoutExistentialTypes(Type t1, Type t2) {
107109
static Type (*withoutExistentialTypes)(Type) = [](Type type) -> Type {
108110
return type.transformRec([](TypeBase *type) -> std::optional<Type> {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -emit-ir -g %s | %FileCheck %s
2+
3+
func f<T>(_ g: @escaping (() -> T) -> T) -> (() -> T) {
4+
var h: (() -> T)? = nil
5+
h = { () -> T in g(h!) }
6+
return h!
7+
}
8+
9+
// CHECK-LABEL: !DICompositeType(tag: DW_TAG_structure_type, name: "$sxIegr_Sgz_x_lXXD",

0 commit comments

Comments
 (0)