Skip to content

Fix field count in emitted reflection data #78961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions lib/IRGen/GenReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,18 +944,35 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
B.addInt16(uint16_t(kind));
B.addInt16(FieldRecordSize);

B.addInt32(getNumFields(NTD));
// Filter to select which fields we'll export FieldDescriptors for.
auto exportable_field =
[](Field field) {
// Don't export private C++ fields that were imported as private Swift fields.
// The type of a private field might not have all the type witness
// operations that Swift requires, for instance,
// `std::unique_ptr<IncompleteType>` would not have a destructor.
if (field.getKind() == Field::Kind::Var &&
field.getVarDecl()->getClangDecl() &&
field.getVarDecl()->getFormalAccess() == AccessLevel::Private)
return false;
// All other fields are exportable
return true;
};

// Count exportable fields
int exportableFieldCount = 0;
forEachField(IGM, NTD, [&](Field field) {
// Skip private C++ fields that were imported as private Swift fields.
// The type of a private field might not have all the type witness
// operations that Swift requires, for instance,
// `std::unique_ptr<IncompleteType>` would not have a destructor.
if (field.getKind() == Field::Kind::Var &&
field.getVarDecl()->getClangDecl() &&
field.getVarDecl()->getFormalAccess() == AccessLevel::Private)
return;
if (exportable_field(field)) {
++exportableFieldCount;
}
});

addField(field);
// Emit exportable fields, prefixed with a count
B.addInt32(exportableFieldCount);
forEachField(IGM, NTD, [&](Field field) {
if (exportable_field(field)) {
addField(field);
}
});
}

Expand Down