Skip to content

Commit 7ba8aa4

Browse files
committed
Fix field count in emitted reflection data
PR #78467 omitted certain fields from the FieldDescriptor list, but did not update the count of fields that's emitted just before that list. Update the count so it matches the number of field descriptors we actually emit. Resolves rdar://143402921
1 parent 9cbe177 commit 7ba8aa4

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

lib/IRGen/GenReflection.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -929,18 +929,35 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
929929
B.addInt16(uint16_t(kind));
930930
B.addInt16(FieldRecordSize);
931931

932-
B.addInt32(getNumFields(NTD));
932+
// Filter to select which fields we'll export FieldDescriptors for.
933+
auto exportable_field =
934+
[](Field field) {
935+
// Don't export private C++ fields that were imported as private Swift fields.
936+
// The type of a private field might not have all the type witness
937+
// operations that Swift requires, for instance,
938+
// `std::unique_ptr<IncompleteType>` would not have a destructor.
939+
if (field.getKind() == Field::Kind::Var &&
940+
field.getVarDecl()->getClangDecl() &&
941+
field.getVarDecl()->getFormalAccess() == AccessLevel::Private)
942+
return false;
943+
// All other fields are exportable
944+
return true;
945+
};
946+
947+
// Count exportable fields
948+
int exportableFieldCount = 0;
933949
forEachField(IGM, NTD, [&](Field field) {
934-
// Skip private C++ fields that were imported as private Swift fields.
935-
// The type of a private field might not have all the type witness
936-
// operations that Swift requires, for instance,
937-
// `std::unique_ptr<IncompleteType>` would not have a destructor.
938-
if (field.getKind() == Field::Kind::Var &&
939-
field.getVarDecl()->getClangDecl() &&
940-
field.getVarDecl()->getFormalAccess() == AccessLevel::Private)
941-
return;
950+
if (exportable_field(field)) {
951+
++exportableFieldCount;
952+
}
953+
});
942954

943-
addField(field);
955+
// Emit exportable fields, prefixed with a count
956+
B.addInt32(exportableFieldCount);
957+
forEachField(IGM, NTD, [&](Field field) {
958+
if (exportable_field(field)) {
959+
addField(field);
960+
}
944961
});
945962
}
946963

0 commit comments

Comments
 (0)