From 7ba8aa427032c62e0df819538068febe3beebc6e Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Mon, 27 Jan 2025 14:30:29 -0800 Subject: [PATCH 1/2] 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 --- lib/IRGen/GenReflection.cpp | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/IRGen/GenReflection.cpp b/lib/IRGen/GenReflection.cpp index 4baaf85e4a41a..1332afd953bdc 100644 --- a/lib/IRGen/GenReflection.cpp +++ b/lib/IRGen/GenReflection.cpp @@ -929,18 +929,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` 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` 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); + } }); } From 2eb79a05dacf68587c9690f7afc6b7e903251f03 Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Mon, 27 Jan 2025 15:26:23 -0800 Subject: [PATCH 2/2] Remove tabs --- lib/IRGen/GenReflection.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/IRGen/GenReflection.cpp b/lib/IRGen/GenReflection.cpp index 1332afd953bdc..c7c200b916c20 100644 --- a/lib/IRGen/GenReflection.cpp +++ b/lib/IRGen/GenReflection.cpp @@ -932,16 +932,16 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder { // 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` 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; + // 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` 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