Skip to content

Commit a0a0a56

Browse files
authored
Merge pull request #6573 from DougGregor/sil-box-type-serialize-substitutions
2 parents 5cdb98a + 8c886fd commit a0a0a56

File tree

4 files changed

+29
-54
lines changed

4 files changed

+29
-54
lines changed

Diff for: include/swift/Serialization/ModuleFormat.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 301; // Last change: Deterministic conformances
57+
const uint16_t VERSION_MINOR = 302; // Last change: SIL box type substitutions
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -716,8 +716,8 @@ namespace decls_block {
716716

717717
using SILBoxTypeLayout = BCRecordLayout<
718718
SIL_BOX_TYPE,
719-
SILLayoutIDField, // layout
720-
BCArray<TypeIDField> // generic arguments
719+
SILLayoutIDField // layout
720+
// trailing substitutions
721721
>;
722722

723723
template <unsigned Code>

Diff for: lib/Serialization/Deserialization.cpp

+13-43
Original file line numberDiff line numberDiff line change
@@ -3977,9 +3977,8 @@ Type ModuleFile::getType(TypeID TID) {
39773977

39783978
case decls_block::SIL_BOX_TYPE: {
39793979
SILLayoutID layoutID;
3980-
ArrayRef<uint64_t> args;
39813980

3982-
decls_block::SILBoxTypeLayout::readRecord(scratch, layoutID, args);
3981+
decls_block::SILBoxTypeLayout::readRecord(scratch, layoutID);
39833982

39843983
// Get the layout.
39853984
auto getLayout = [&]() -> SILLayout * {
@@ -4008,49 +4007,20 @@ Type ModuleFile::getType(TypeID TID) {
40084007

40094008
SmallVector<Substitution, 4> genericArgs;
40104009
if (auto sig = layout->getGenericSignature()) {
4011-
if (args.size() != sig->getAllDependentTypes().size()) {
4012-
error();
4013-
return nullptr;
4014-
}
4015-
TypeSubstitutionMap mappings;
4016-
4017-
for (unsigned i : indices(sig->getGenericParams())) {
4018-
mappings[sig->getGenericParams()[i]] =
4019-
getType(args[i])->getCanonicalType();
4020-
}
4021-
4022-
bool ok = true;
4023-
sig->getSubstitutions(mappings,
4024-
[&](CanType depTy, Type replacementTy, ProtocolType *proto)
4025-
-> ProtocolConformanceRef {
4026-
if (replacementTy->is<SubstitutableType>()
4027-
|| replacementTy->is<DependentMemberType>())
4028-
return ProtocolConformanceRef(proto->getDecl());
4029-
4030-
auto conformance = getAssociatedModule()
4031-
->lookupConformance(replacementTy, proto->getDecl(), nullptr);
4032-
if (!conformance) {
4033-
error();
4034-
ok = false;
4035-
return ProtocolConformanceRef(proto->getDecl());
4036-
}
4037-
return *conformance;
4038-
},
4039-
genericArgs);
4040-
if (!ok)
4041-
return nullptr;
4042-
4043-
for (auto &arg : genericArgs) {
4044-
arg = Substitution(arg.getReplacement()->getCanonicalType(),
4045-
arg.getConformances());
4046-
}
4047-
} else {
4048-
if (args.size() != 0) {
4049-
error();
4050-
return nullptr;
4010+
for (unsigned i : range(sig->getAllDependentTypes().size())) {
4011+
(void)i;
4012+
auto sub = maybeReadSubstitution(DeclTypeCursor);
4013+
if (!sub) {
4014+
error();
4015+
return nullptr;
4016+
}
4017+
4018+
genericArgs.push_back(
4019+
Substitution(sub->getReplacement()->getCanonicalType(),
4020+
sub->getConformances()));
40514021
}
40524022
}
4053-
4023+
40544024
typeOrOffset = SILBoxType::get(getContext(), layout, genericArgs);
40554025
break;
40564026
}

Diff for: lib/Serialization/Serialization.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -3185,13 +3185,18 @@ void Serializer::writeType(Type ty) {
31853185

31863186
unsigned abbrCode = DeclTypeAbbrCodes[SILBoxTypeLayout::Code];
31873187
SILLayoutID layoutRef = addSILLayoutRef(boxTy->getLayout());
3188-
3189-
SmallVector<TypeID, 4> genericArgs;
3190-
for (auto &arg : boxTy->getGenericArgs())
3191-
genericArgs.push_back(addTypeRef(arg.getReplacement()));
3192-
3193-
SILBoxTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,
3194-
layoutRef, genericArgs);
3188+
3189+
#ifndef NDEBUG
3190+
if (auto sig = boxTy->getLayout()->getGenericSignature()) {
3191+
assert(sig->getAllDependentTypes().size()
3192+
== boxTy->getGenericArgs().size());
3193+
}
3194+
#endif
3195+
3196+
SILBoxTypeLayout::emitRecord(Out, ScratchRecord, abbrCode, layoutRef);
3197+
3198+
// Write the set of substitutions.
3199+
writeSubstitutions(boxTy->getGenericArgs(), DeclTypeAbbrCodes);
31953200
break;
31963201
}
31973202

Diff for: test/Serialization/sil_box_types.sil

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ sil @boxes_extra_reqs : $@convention(thin) <T where T : Q, T.AT : P> (@owned <τ
2424
bb0(%0 : $<τ_0_0 where τ_0_0 : Q, τ_0_0.AT : P> { var τ_0_0 } <T, T.AT>):
2525
%1 = project_box %0 : $<τ_0_0 where τ_0_0 : Q, τ_0_0.AT : P> { var τ_0_0 } <T, T.AT>, 0
2626
return undef : $()
27-
}
27+
}

0 commit comments

Comments
 (0)