Skip to content

Commit 81196ac

Browse files
Fix a bug in handling of implicit-presence string_view fields. (#20403)
PiperOrigin-RevId: 728814630
1 parent 4b34c96 commit 81196ac

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

src/google/protobuf/compiler/cpp/helpers.h

+5
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ inline bool IsString(const FieldDescriptor* field) {
358358
}
359359

360360

361+
inline bool IsArenaStringPtr(const FieldDescriptor* field) {
362+
return field->cpp_string_type() == FieldDescriptor::CppStringType::kString ||
363+
field->cpp_string_type() == FieldDescriptor::CppStringType::kView;
364+
}
365+
361366
bool IsProfileDriven(const Options& options);
362367

363368
// Returns true if `field` is unlikely to be present based on PDProto profile.

src/google/protobuf/compiler/cpp/message.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ void EmitNonDefaultCheckForString(io::Printer* p, absl::string_view prefix,
292292
const FieldDescriptor* field, bool split,
293293
absl::AnyInvocable<void()> emit_body) {
294294
ABSL_DCHECK(field->cpp_type() == FieldDescriptor::CPPTYPE_STRING);
295-
ABSL_DCHECK(field->cpp_string_type() ==
296-
FieldDescriptor::CppStringType::kString);
295+
ABSL_DCHECK(IsArenaStringPtr(field));
297296
p->Emit(
298297
{
299298
{"condition", [&] { EmitNonDefaultCheck(p, prefix, field); }},
@@ -392,7 +391,7 @@ void MayEmitMutableIfNonDefaultCheck(io::Printer* p, absl::string_view prefix,
392391
bool with_enclosing_braces_always) {
393392
if (ShouldEmitNonDefaultCheck(field)) {
394393
if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
395-
field->cpp_string_type() == FieldDescriptor::CppStringType::kString) {
394+
IsArenaStringPtr(field)) {
396395
// If a field is backed by std::string, when default initialized it will
397396
// point to a global empty std::string instance. We prefer to spend some
398397
// extra cycles here to create a local string instance in the else branch,

src/google/protobuf/compiler/cpp/tracker.cc

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "absl/log/absl_check.h"
1515
#include "absl/strings/str_cat.h"
16-
#include "absl/strings/str_format.h"
1716
#include "absl/strings/string_view.h"
1817
#include "absl/strings/substitute.h"
1918
#include "absl/types/optional.h"

src/google/protobuf/string_view_test.cc

+7
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ TEST(StringViewFieldTest, RepeatedSetAndGetByReflection) {
311311
StrEq("222222222222"));
312312
}
313313

314+
TEST(StringViewFieldTest, MergeAndClearEmptyImplicitPresence) {
315+
TestStringView message, other;
316+
other.set_implicit_presence("");
317+
message.MergeFrom(other);
318+
message.Clear();
319+
}
320+
314321
} // namespace
315322
} // namespace protobuf
316323
} // namespace google

src/google/protobuf/unittest_string_view.proto

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ option java_multiple_files = true;
88
option optimize_for = SPEED;
99
option features.(pb.cpp).string_type = VIEW;
1010

11-
// NEXT_TAG = 5;
11+
// NEXT_TAG = 6;
1212
message TestStringView {
1313
string singular_string = 1;
1414
bytes singular_bytes = 2;
15+
string implicit_presence = 5 [features.field_presence = IMPLICIT];
1516

1617
repeated string repeated_string = 3;
1718
repeated bytes repeated_bytes = 4;

0 commit comments

Comments
 (0)