From f083530a0655338b5a1b8dfa0d46727cf1048dd9 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 1 Nov 2023 10:26:00 -0700 Subject: [PATCH] Allow string + bytes fields in oneofs (now that the underyling accessors match the expected shape) PiperOrigin-RevId: 578561161 --- rust/test/shared/accessors_proto3_test.rs | 5 ++--- rust/test/shared/accessors_test.rs | 5 ++--- src/google/protobuf/compiler/rust/accessors/accessors.cc | 4 ++-- src/google/protobuf/compiler/rust/oneof.cc | 7 +++++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/rust/test/shared/accessors_proto3_test.rs b/rust/test/shared/accessors_proto3_test.rs index 3260c0d11d..3eb6e5df61 100644 --- a/rust/test/shared/accessors_proto3_test.rs +++ b/rust/test/shared/accessors_proto3_test.rs @@ -200,9 +200,8 @@ fn test_oneof_accessors() { assert_that!(msg.oneof_field(), matches_pattern!(not_set(_))); msg.oneof_uint32_set(Some(7)); - msg.oneof_bytes_mut().set(b""); + msg.oneof_bytes_mut().set(b"123"); assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0))); - // This should show it set to the OneofBytes but its not supported yet. - assert_that!(msg.oneof_field(), matches_pattern!(not_set(_))); + assert_that!(msg.oneof_field(), matches_pattern!(OneofBytes(eq(b"123")))); } diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index f1f55f663d..2b207ac11c 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -690,11 +690,10 @@ fn test_oneof_accessors() { assert_that!(msg.oneof_field(), matches_pattern!(not_set(_))); msg.oneof_uint32_set(Some(7)); - msg.oneof_bytes_mut().set(b""); + msg.oneof_bytes_mut().set(b"123"); assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0))); - // This should show it set to the OneofBytes but its not supported yet. - assert_that!(msg.oneof_field(), matches_pattern!(not_set(_))); + assert_that!(msg.oneof_field(), matches_pattern!(OneofBytes(eq(b"123")))); } macro_rules! generate_repeated_numeric_test { diff --git a/src/google/protobuf/compiler/rust/accessors/accessors.cc b/src/google/protobuf/compiler/rust/accessors/accessors.cc index fa5c876bf9..807eb478b3 100644 --- a/src/google/protobuf/compiler/rust/accessors/accessors.cc +++ b/src/google/protobuf/compiler/rust/accessors/accessors.cc @@ -23,8 +23,8 @@ namespace { std::unique_ptr AccessorGeneratorFor( const FieldDescriptor& desc) { - // We do not support [ctype=FOO] (used to set the field type in C++ to - // cord or string_piece) in V0 API. + // TODO: We do not support [ctype=FOO] (used to set the field + // type in C++ to cord or string_piece) in V0.6 API. if (desc.options().has_ctype()) { return std::make_unique(); } diff --git a/src/google/protobuf/compiler/rust/oneof.cc b/src/google/protobuf/compiler/rust/oneof.cc index 2cdeeac203..41f5bb9a1c 100644 --- a/src/google/protobuf/compiler/rust/oneof.cc +++ b/src/google/protobuf/compiler/rust/oneof.cc @@ -92,12 +92,15 @@ std::string oneofCaseEnumName(const OneofDescriptor& desc) { // TODO: Promote up to naming.h once all types can be spelled. std::string RsTypeName(const FieldDescriptor& desc) { + // TODO: Fields with a ctype set not supported in v0.6 api. + if (desc.options().has_ctype()) { + return ""; + } + switch (desc.type()) { case FieldDescriptor::TYPE_MESSAGE: case FieldDescriptor::TYPE_ENUM: case FieldDescriptor::TYPE_GROUP: - case FieldDescriptor::TYPE_STRING: - case FieldDescriptor::TYPE_BYTES: return ""; default: return PrimitiveRsTypeName(desc);