Skip to content

Commit

Permalink
Fix a bug in handling of implicit-presence string_view fields. (#20403)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 728814630
  • Loading branch information
mkruskal-google authored Feb 19, 2025
1 parent 4b34c96 commit 81196ac
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ inline bool IsString(const FieldDescriptor* field) {
}


inline bool IsArenaStringPtr(const FieldDescriptor* field) {
return field->cpp_string_type() == FieldDescriptor::CppStringType::kString ||
field->cpp_string_type() == FieldDescriptor::CppStringType::kView;
}

bool IsProfileDriven(const Options& options);

// Returns true if `field` is unlikely to be present based on PDProto profile.
Expand Down
5 changes: 2 additions & 3 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ void EmitNonDefaultCheckForString(io::Printer* p, absl::string_view prefix,
const FieldDescriptor* field, bool split,
absl::AnyInvocable<void()> emit_body) {
ABSL_DCHECK(field->cpp_type() == FieldDescriptor::CPPTYPE_STRING);
ABSL_DCHECK(field->cpp_string_type() ==
FieldDescriptor::CppStringType::kString);
ABSL_DCHECK(IsArenaStringPtr(field));
p->Emit(
{
{"condition", [&] { EmitNonDefaultCheck(p, prefix, field); }},
Expand Down Expand Up @@ -392,7 +391,7 @@ void MayEmitMutableIfNonDefaultCheck(io::Printer* p, absl::string_view prefix,
bool with_enclosing_braces_always) {
if (ShouldEmitNonDefaultCheck(field)) {
if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
field->cpp_string_type() == FieldDescriptor::CppStringType::kString) {
IsArenaStringPtr(field)) {
// If a field is backed by std::string, when default initialized it will
// point to a global empty std::string instance. We prefer to spend some
// extra cycles here to create a local string instance in the else branch,
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/compiler/cpp/tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "absl/log/absl_check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "absl/types/optional.h"
Expand Down
7 changes: 7 additions & 0 deletions src/google/protobuf/string_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ TEST(StringViewFieldTest, RepeatedSetAndGetByReflection) {
StrEq("222222222222"));
}

TEST(StringViewFieldTest, MergeAndClearEmptyImplicitPresence) {
TestStringView message, other;
other.set_implicit_presence("");
message.MergeFrom(other);
message.Clear();
}

} // namespace
} // namespace protobuf
} // namespace google
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/unittest_string_view.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ option java_multiple_files = true;
option optimize_for = SPEED;
option features.(pb.cpp).string_type = VIEW;

// NEXT_TAG = 5;
// NEXT_TAG = 6;
message TestStringView {
string singular_string = 1;
bytes singular_bytes = 2;
string implicit_presence = 5 [features.field_presence = IMPLICIT];

repeated string repeated_string = 3;
repeated bytes repeated_bytes = 4;
Expand Down

0 comments on commit 81196ac

Please sign in to comment.