Automated rollback of commit 74f6da4d26173088de178365678891105393799e.

PiperOrigin-RevId: 657257908
diff --git a/hpb/hpb.h b/hpb/hpb.h
index ccb649e..b18569c 100644
--- a/hpb/hpb.h
+++ b/hpb/hpb.h
@@ -224,12 +224,13 @@
   using Extendee = ExtendeeType;
 
   constexpr explicit ExtensionIdentifier(
-      uint32_t number, const upb_MiniTableExtension* mini_table_ext)
-      : ExtensionMiniTableProvider(mini_table_ext), number_(number) {}
+      const upb_MiniTableExtension* mini_table_ext)
+      : ExtensionMiniTableProvider(mini_table_ext) {}
 
  private:
-  uint32_t number_;
-  constexpr uint32_t number() const { return number_; }
+  constexpr uint32_t number() const {
+    return upb_MiniTableExtension_Number(mini_table_ext());
+  }
   friend class PrivateAccess;
 };
 
diff --git a/hpb_generator/gen_extensions.cc b/hpb_generator/gen_extensions.cc
index f5d44ff..00a8dcd 100644
--- a/hpb_generator/gen_extensions.cc
+++ b/hpb_generator/gen_extensions.cc
@@ -7,14 +7,9 @@
 
 #include "google/protobuf/compiler/hpb/gen_extensions.h"
 
-#include <cassert>
-#include <string>
-#include <vector>
-
 #include "absl/strings/str_cat.h"
+#include "google/protobuf/compiler/hpb/gen_utils.h"
 #include "google/protobuf/compiler/hpb/names.h"
-#include "google/protobuf/compiler/hpb/output.h"
-#include "google/protobuf/descriptor.h"
 
 namespace google::protobuf::hpb_generator {
 
@@ -43,19 +38,15 @@
   if (ext->extension_scope()) {
     output(
         R"cc(
-          static constexpr ::hpb::internal::ExtensionIdentifier<$0, $3> $2{$4,
-                                                                           &$1};
+          static const ::hpb::internal::ExtensionIdentifier<$0, $1> $2;
         )cc",
-        ContainingTypeName(ext), mini_table_name, ext->name(),
-        CppTypeParameterName(ext), ext->number());
+        ContainingTypeName(ext), CppTypeParameterName(ext), ext->name());
   } else {
     output(
         R"cc(
-          inline constexpr ::hpb::internal::ExtensionIdentifier<$0, $3> $2{$4,
-                                                                           &$1};
+          extern const ::hpb::internal::ExtensionIdentifier<$0, $1> $2;
         )cc",
-        ContainingTypeName(ext), mini_table_name, ext->name(),
-        CppTypeParameterName(ext), ext->number());
+        ContainingTypeName(ext), CppTypeParameterName(ext), ext->name());
   }
 }
 
@@ -69,5 +60,36 @@
   }
 }
 
+void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext,
+                              Output& output) {
+  std::string mini_table_name =
+      absl::StrCat(ExtensionIdentifierBase(ext), "_", ext->name(), "_ext");
+  if (ext->extension_scope()) {
+    output(
+        R"cc(
+          const ::protos::internal::ExtensionIdentifier<$0, $3> $4::$2(&$1);
+        )cc",
+        ContainingTypeName(ext), mini_table_name, ext->name(),
+        CppTypeParameterName(ext), ClassName(ext->extension_scope()));
+  } else {
+    output(
+        R"cc(
+          const ::protos::internal::ExtensionIdentifier<$0, $3> $2(&$1);
+        )cc",
+        ContainingTypeName(ext), mini_table_name, ext->name(),
+        CppTypeParameterName(ext));
+  }
+}
+
+void WriteExtensionIdentifiers(
+    const std::vector<const protobuf::FieldDescriptor*>& extensions,
+    Output& output) {
+  for (const auto* ext : extensions) {
+    if (!ext->extension_scope()) {
+      WriteExtensionIdentifier(ext, output);
+    }
+  }
+}
+
 }  // namespace protobuf
 }  // namespace google::hpb_generator
diff --git a/hpb_generator/gen_extensions.h b/hpb_generator/gen_extensions.h
index 1d2e932..e48f657 100644
--- a/hpb_generator/gen_extensions.h
+++ b/hpb_generator/gen_extensions.h
@@ -8,8 +8,8 @@
 #ifndef PROTOBUF_COMPILER_HBP_GEN_EXTENSIONS_H_
 #define PROTOBUF_COMPILER_HBP_GEN_EXTENSIONS_H_
 
-#include "google/protobuf/compiler/hpb/output.h"
 #include "google/protobuf/descriptor.h"
+#include "google/protobuf/compiler/hpb/output.h"
 
 namespace google::protobuf::hpb_generator {
 
@@ -20,6 +20,12 @@
     Output& output);
 void WriteExtensionIdentifierHeader(const protobuf::FieldDescriptor* ext,
                                     Output& output);
+void WriteExtensionIdentifiers(
+    const std::vector<const protobuf::FieldDescriptor*>& extensions,
+    Output& output);
+void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext,
+                              Output& output);
+
 }  // namespace protobuf
 }  // namespace google::hpb_generator
 
diff --git a/hpb_generator/gen_messages.cc b/hpb_generator/gen_messages.cc
index 7de1b21..2ea07e7 100644
--- a/hpb_generator/gen_messages.cc
+++ b/hpb_generator/gen_messages.cc
@@ -15,14 +15,15 @@
 #include "absl/strings/ascii.h"
 #include "absl/strings/str_cat.h"
 #include "absl/strings/string_view.h"
+#include "google/protobuf/descriptor.h"
 #include "google/protobuf/compiler/hpb/gen_accessors.h"
 #include "google/protobuf/compiler/hpb/gen_enums.h"
 #include "google/protobuf/compiler/hpb/gen_extensions.h"
 #include "google/protobuf/compiler/hpb/gen_utils.h"
 #include "google/protobuf/compiler/hpb/names.h"
 #include "google/protobuf/compiler/hpb/output.h"
-#include "google/protobuf/descriptor.h"
 #include "upb_generator/common.h"
+#include "upb_generator/file_layout.h"
 
 namespace google::protobuf::hpb_generator {
 
@@ -47,6 +48,10 @@
     const protobuf::Descriptor* message, Output& output);
 void WriteDefaultInstanceHeader(const protobuf::Descriptor* message,
                                 Output& output);
+void WriteExtensionIdentifiersImplementation(
+    const protobuf::Descriptor* message,
+    const std::vector<const protobuf::FieldDescriptor*>& file_exts,
+    Output& output);
 void WriteUsingEnumsInHeader(
     const protobuf::Descriptor* message,
     const std::vector<const protobuf::EnumDescriptor*>& file_enums,
@@ -451,6 +456,8 @@
           }
         )cc",
         ClassName(descriptor));
+
+    WriteExtensionIdentifiersImplementation(descriptor, file_exts, output);
   }
 }
 
@@ -478,6 +485,18 @@
   }
 }
 
+void WriteExtensionIdentifiersImplementation(
+    const protobuf::Descriptor* message,
+    const std::vector<const protobuf::FieldDescriptor*>& file_exts,
+    Output& output) {
+  for (auto* ext : file_exts) {
+    if (ext->extension_scope() &&
+        ext->extension_scope()->full_name() == message->full_name()) {
+      WriteExtensionIdentifier(ext, output);
+    }
+  }
+}
+
 void WriteUsingEnumsInHeader(
     const protobuf::Descriptor* message,
     const std::vector<const protobuf::EnumDescriptor*>& file_enums,
diff --git a/hpb_generator/protoc-gen-upb-protos.cc b/hpb_generator/protoc-gen-upb-protos.cc
index 5bf9b8f..d946fb2 100644
--- a/hpb_generator/protoc-gen-upb-protos.cc
+++ b/hpb_generator/protoc-gen-upb-protos.cc
@@ -13,14 +13,14 @@
 
 #include "google/protobuf/descriptor.pb.h"
 #include "google/protobuf/compiler/code_generator.h"
+#include "google/protobuf/compiler/plugin.h"
+#include "google/protobuf/descriptor.h"
 #include "google/protobuf/compiler/hpb/gen_enums.h"
 #include "google/protobuf/compiler/hpb/gen_extensions.h"
 #include "google/protobuf/compiler/hpb/gen_messages.h"
 #include "google/protobuf/compiler/hpb/gen_utils.h"
 #include "google/protobuf/compiler/hpb/names.h"
 #include "google/protobuf/compiler/hpb/output.h"
-#include "google/protobuf/compiler/plugin.h"
-#include "google/protobuf/descriptor.h"
 
 namespace google::protobuf::hpb_generator {
 namespace {
@@ -211,6 +211,7 @@
   WriteMessageImplementations(file, output);
   const std::vector<const protobuf::FieldDescriptor*> this_file_exts =
       SortedExtensions(file);
+  WriteExtensionIdentifiers(this_file_exts, output);
   WriteEndNamespace(file, output);
 
   output("#include \"upb/port/undef.inc\"\n\n");
diff --git a/hpb_generator/tests/test_generated.cc b/hpb_generator/tests/test_generated.cc
index 3cb51a1..b097875 100644
--- a/hpb_generator/tests/test_generated.cc
+++ b/hpb_generator/tests/test_generated.cc
@@ -1241,8 +1241,6 @@
 }
 
 TEST(CppGeneratedCode, ExtensionFieldNumberConstant) {
-  static_assert(::protos::ExtensionNumber(ThemeExtension::theme_extension) ==
-                12003);
   EXPECT_EQ(12003, ::protos::ExtensionNumber(ThemeExtension::theme_extension));
 }