diff --git a/src/google/protobuf/json/internal/untyped_message.cc b/src/google/protobuf/json/internal/untyped_message.cc index 805d0d8310..5c3c0e86b7 100644 --- a/src/google/protobuf/json/internal/untyped_message.cc +++ b/src/google/protobuf/json/internal/untyped_message.cc @@ -41,9 +41,6 @@ #include #include "google/protobuf/type.pb.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/dynamic_message.h" -#include "google/protobuf/message.h" #include "absl/container/flat_hash_map.h" #include "absl/log/absl_check.h" #include "absl/status/status.h" @@ -54,8 +51,8 @@ #include "absl/types/span.h" #include "absl/types/variant.h" #include "google/protobuf/io/coded_stream.h" +#include "google/protobuf/port.h" #include "google/protobuf/util/type_resolver.h" -#include "google/protobuf/wire_format.h" #include "google/protobuf/wire_format_lite.h" #include "utf8_validity.h" #include "google/protobuf/stubs/status_macros.h" @@ -536,10 +533,16 @@ absl::Status UntypedMessage::InsertField(const ResolverPool::Field& field, } else if (auto* extant = absl::get_if>(&slot)) { extant->push_back(std::move(value)); } else { + absl::optional name = + google::protobuf::internal::RttiTypeName(); + if (!name.has_value()) { + name = ""; + } + return absl::InvalidArgumentError( absl::StrFormat("inconsistent types for field number %d: tried to " - "insert %s, but index was %d", - number, typeid(T).name(), slot.index())); + "insert '%s', but index was %d", + number, *name, slot.index())); } return absl::OkStatus(); diff --git a/src/google/protobuf/port.h b/src/google/protobuf/port.h index 0abb04455d..9ca45d966b 100644 --- a/src/google/protobuf/port.h +++ b/src/google/protobuf/port.h @@ -42,8 +42,14 @@ #include #include +#if PROTOBUF_RTTI +#include +#endif + #include "absl/meta/type_traits.h" +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" // must be last #include "google/protobuf/port_def.inc" @@ -164,6 +170,16 @@ inline ToRef DownCast(From& f) { return *static_cast(&f); } +// Looks up the name of `T` via RTTI, if RTTI is available. +template +inline absl::optional RttiTypeName() { +#if PROTOBUF_RTTI + return typeid(T).name(); +#else + return absl::nullopt; +#endif +} + // Helpers for identifying our supported types. template struct is_supported_integral_type