Skip to content

Commit

Permalink
Move the throw statement to the template in the header.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 690650749
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Oct 28, 2024
1 parent 9e10b7c commit 24ef3d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 0 additions & 3 deletions src/google/protobuf/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ void MessageLite::LogInitializationErrorMessage() const {
namespace internal {

void FailDynamicCast(const MessageLite& from, const MessageLite& to) {
#if defined(ABSL_HAVE_EXCEPTIONS)
throw std::bad_cast();
#endif
const auto to_name = to.GetTypeName();
if (internal::GetClassData(from)->is_dynamic) {
ABSL_LOG(FATAL)
Expand Down
7 changes: 6 additions & 1 deletion src/google/protobuf/message_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ std::string Utf8Format(const MessageLite& message_lite);
//
// `DynamicCastMessage` is similar to `dynamic_cast`, returns `nullptr` when the
// input is not an instance of `T`. The overloads that take a reference will
// terminate on mismatch.
// throw std::bad_cast on mismatch, or terminate if compiled without exceptions.
//
// `DownCastMessage` is a lightweight function for downcasting base
// `MessageLite` pointer to derived type, where it only does type checking if
Expand Down Expand Up @@ -1364,6 +1364,11 @@ template <typename T>
const T& DynamicCastMessage(const MessageLite& from) {
const T* destination_message = DynamicCastMessage<T>(&from);
if (ABSL_PREDICT_FALSE(destination_message == nullptr)) {
// If exceptions are enabled, throw.
// Otherwise, log a fatal error.
#if defined(ABSL_HAVE_EXCEPTIONS)
throw std::bad_cast();
#endif
// Move the logging into an out-of-line function to reduce bloat in the
// caller.
internal::FailDynamicCast(from, T::default_instance());
Expand Down

0 comments on commit 24ef3d6

Please sign in to comment.