From 0b9134bb4eb281c3ed1446e8acf1aa354e0fe67e Mon Sep 17 00:00:00 2001
From: Mike Kruskal <mkruskal@google.com>
Date: Mon, 6 Mar 2023 21:17:53 -0800
Subject: [PATCH] Deprecate SupportsUnknownEnumValues on Message reflection. 
 Use FieldDescriptor or EnumDescriptor instead.

PiperOrigin-RevId: 514618684
---
 .github/workflows/codespell.yml                           | 2 +-
 python/google/protobuf/pyext/map_container.cc             | 5 ++++-
 python/google/protobuf/pyext/message.cc                   | 2 +-
 python/google/protobuf/pyext/repeated_scalar_container.cc | 4 ++--
 src/google/protobuf/generated_message_reflection.cc       | 6 +-----
 src/google/protobuf/message.h                             | 2 ++
 src/google/protobuf/text_format.cc                        | 2 +-
 7 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/.github/workflows/codespell.yml b/.github/workflows/codespell.yml
index 2c329e6548..84b07067e7 100644
--- a/.github/workflows/codespell.yml
+++ b/.github/workflows/codespell.yml
@@ -24,4 +24,4 @@ jobs:
         with:
           check_filenames: true
           skip: ./.git,./third_party,./conformance/third_party,*.snk,*.pb,*.pb.cc,*.pb.h,./src/google/protobuf/testdata,./objectivec/Tests,./python/compatibility_tests/v2.5.0/tests/google/protobuf/internal,./.github/workflows/codespell.yml
-          ignore_words_list: "alow,alse,atleast,ba,chec,cleare,copyable,cloneable,crate,dedup,dur,errorprone,falsy,files',fo,fundementals,hel,importd,inout,leapyear,nd,nin,ois,ons,parseable,process',ro,te,testof,ue,unparseable,wasn,wee,gae,keyserver,objext,od,optin,streem,sur,falsy"
+          ignore_words_list: "alow,alse,atleast,ba,chec,cleare,copyable,cloneable,crate,dedup,dur,errorprone,falsy,files',fo,fundementals,hel,importd,inout,leapyear,nd,nin,ois,ons,parseable,process',ro,te,testof,ue,unparseable,wasn,wee,gae,keyserver,objext,od,optin,streem,sur,falsy,cleary"
diff --git a/python/google/protobuf/pyext/map_container.cc b/python/google/protobuf/pyext/map_container.cc
index 7d690c1e91..d2784fdcf1 100644
--- a/python/google/protobuf/pyext/map_container.cc
+++ b/python/google/protobuf/pyext/map_container.cc
@@ -437,7 +437,10 @@ int MapReflectionFriend::ScalarMapSetItem(PyObject* _self, PyObject* key,
       self->version++;
     }
 
-    if (!PythonToMapValueRef(self, v, reflection->SupportsUnknownEnumValues(),
+    if (!PythonToMapValueRef(self, v,
+                             !self->parent_field_descriptor->message_type()
+                                  ->map_value()
+                                  ->legacy_enum_field_treated_as_closed(),
                              &value)) {
       return -1;
     }
diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc
index 3ff31e2670..d5693bb3bc 100644
--- a/python/google/protobuf/pyext/message.cc
+++ b/python/google/protobuf/pyext/message.cc
@@ -2300,7 +2300,7 @@ int InternalSetNonOneofScalar(
     }
     case FieldDescriptor::CPPTYPE_ENUM: {
       PROTOBUF_CHECK_GET_INT32(arg, value, -1);
-      if (reflection->SupportsUnknownEnumValues()) {
+      if (!field_descriptor->legacy_enum_field_treated_as_closed()) {
         reflection->SetEnumValue(message, field_descriptor, value);
       } else {
         const EnumDescriptor* enum_descriptor = field_descriptor->enum_type();
diff --git a/python/google/protobuf/pyext/repeated_scalar_container.cc b/python/google/protobuf/pyext/repeated_scalar_container.cc
index b19f00491d..ba24f0ffdc 100644
--- a/python/google/protobuf/pyext/repeated_scalar_container.cc
+++ b/python/google/protobuf/pyext/repeated_scalar_container.cc
@@ -151,7 +151,7 @@ static int AssignItem(PyObject* pself, Py_ssize_t index, PyObject* arg) {
     }
     case FieldDescriptor::CPPTYPE_ENUM: {
       PROTOBUF_CHECK_GET_INT32(arg, value, -1);
-      if (reflection->SupportsUnknownEnumValues()) {
+      if (!field_descriptor->legacy_enum_field_treated_as_closed()) {
         reflection->SetRepeatedEnumValue(message, field_descriptor, index,
                                          value);
       } else {
@@ -376,7 +376,7 @@ PyObject* Append(RepeatedScalarContainer* self, PyObject* item) {
     }
     case FieldDescriptor::CPPTYPE_ENUM: {
       PROTOBUF_CHECK_GET_INT32(item, value, nullptr);
-      if (reflection->SupportsUnknownEnumValues()) {
+      if (!field_descriptor->legacy_enum_field_treated_as_closed()) {
         reflection->AddEnumValue(message, field_descriptor, value);
       } else {
         const EnumDescriptor* enum_descriptor = field_descriptor->enum_type();
diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc
index 7df03b316e..6fab21493d 100644
--- a/src/google/protobuf/generated_message_reflection.cc
+++ b/src/google/protobuf/generated_message_reflection.cc
@@ -1540,10 +1540,6 @@ bool IsIndexInHasBitSet(const uint32_t* has_bit_set, uint32_t has_bit_index) {
           static_cast<uint32_t>(1)) != 0;
 }
 
-bool CreateUnknownEnumValues(const FileDescriptor* file) {
-  return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
-}
-
 void CheckInOrder(const FieldDescriptor* field, uint32_t* last) {
   *last = *last <= static_cast<uint32_t>(field->number())
               ? static_cast<uint32_t>(field->number())
@@ -2486,7 +2482,7 @@ const FieldDescriptor* Reflection::FindKnownExtensionByNumber(
 }
 
 bool Reflection::SupportsUnknownEnumValues() const {
-  return CreateUnknownEnumValues(descriptor_->file());
+  return descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
 }
 
 // ===================================================================
diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h
index fac5c24383..645dc3db25 100644
--- a/src/google/protobuf/message.h
+++ b/src/google/protobuf/message.h
@@ -118,6 +118,7 @@
 #include "google/protobuf/stubs/common.h"
 #include "google/protobuf/arena.h"
 #include "google/protobuf/port.h"
+#include "absl/base/attributes.h"
 #include "absl/base/call_once.h"
 #include "absl/base/casts.h"
 #include "absl/functional/function_ref.h"
@@ -961,6 +962,7 @@ class PROTOBUF_EXPORT Reflection final {
   //       reflection->SetEnumValue(message, field, new_value);
   //     }
   //   }
+  ABSL_DEPRECATED("Use EnumDescriptor::is_closed instead.")
   bool SupportsUnknownEnumValues() const;
 
   // Returns the MessageFactory associated with this message.  This can be
diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc
index 9d213a361c..de994f4582 100644
--- a/src/google/protobuf/text_format.cc
+++ b/src/google/protobuf/text_format.cc
@@ -918,7 +918,7 @@ class TextFormat::Parser::ParserImpl {
 
         if (enum_value == nullptr) {
           if (int_value != kint64max &&
-              reflection->SupportsUnknownEnumValues()) {
+              !field->legacy_enum_field_treated_as_closed()) {
             SET_FIELD(EnumValue, int_value);
             return true;
           } else if (!allow_unknown_enum_) {