Add static asserts to container classes.

This will prevent unsupported uses of these classes downstream, which can behave surprisingly and increase the friction of changes to protobuf.  Specifically RepeatedField, RepeatedPtrField, and Map will only be allowed to hold the types listed in https://protobuf.dev/programming-guides/proto.  The old behavior allowed them to hold just about anything, including custom user types.

PiperOrigin-RevId: 502672732
pull/11550/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 8ea499d662
commit 5a8abe1c20
  1. 4
      src/google/protobuf/map.h
  2. 4
      src/google/protobuf/port_def.inc
  3. 4
      src/google/protobuf/repeated_field.h
  4. 4
      src/google/protobuf/repeated_ptr_field.h

@ -1088,7 +1088,6 @@ class Map : private internal::KeyMapBase<internal::KeyForBase<Key>> {
}
private:
#ifdef PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static_assert(!std::is_const<mapped_type>::value &&
!std::is_const<key_type>::value,
"We do not support const types.");
@ -1101,9 +1100,7 @@ class Map : private internal::KeyMapBase<internal::KeyForBase<Key>> {
static_assert(!std::is_reference<mapped_type>::value &&
!std::is_reference<key_type>::value,
"We do not support reference types.");
#endif // PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static constexpr PROTOBUF_ALWAYS_INLINE void StaticValidityCheck() {
#ifdef PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static_assert(alignof(internal::NodeBase) >= alignof(mapped_type),
"Alignment of mapped type is too high.");
static_assert(
@ -1119,7 +1116,6 @@ class Map : private internal::KeyMapBase<internal::KeyForBase<Key>> {
internal::is_internal_map_value_type<mapped_type>>::value,
"We only support scalar, Message, and designated internal "
"mapped types.");
#endif // PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
}
template <typename P>

@ -204,10 +204,6 @@ static_assert(PROTOBUF_CPLUSPLUS_MIN(201402L), "Protobuf only supports C++14 and
#ifdef PROTOBUF_FUTURE_BREAKING_CHANGES
// Include static asserts to lock down assumptions about our containers.
// Owner: mkruskal@
#define PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS 1
// Used to upgrade google::protobuf::MapPair<K, V> to std::pair<const K, V>.
// Owner: mordberg@
#define PROTOBUF_FUTURE_MAP_PAIR_UPGRADE 1

@ -149,7 +149,6 @@ class RepeatedField final {
static_assert(
alignof(Arena) >= alignof(Element),
"We only support types that have an alignment smaller than Arena");
#ifndef PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static_assert(!std::is_const<Element>::value,
"We do not support const value types.");
static_assert(!std::is_volatile<Element>::value,
@ -158,16 +157,13 @@ class RepeatedField final {
"We do not support pointer value types.");
static_assert(!std::is_reference<Element>::value,
"We do not support reference value types.");
#endif // !PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static constexpr PROTOBUF_ALWAYS_INLINE void StaticValidityCheck() {
#ifdef PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static_assert(
absl::disjunction<internal::is_supported_integral_type<Element>,
internal::is_supported_floating_point_type<Element>,
std::is_same<absl::Cord, Element>,
is_proto_enum<Element>>::value,
"We only support non-string scalars in RepeatedField.");
#endif // PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
}
public:

@ -902,7 +902,6 @@ class StringTypeHandler {
// Messages.
template <typename Element>
class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
#ifndef PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static_assert(!std::is_const<Element>::value,
"We do not support const value types.");
static_assert(!std::is_volatile<Element>::value,
@ -911,15 +910,12 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
"We do not support pointer value types.");
static_assert(!std::is_reference<Element>::value,
"We do not support reference value types.");
#endif // !PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static constexpr PROTOBUF_ALWAYS_INLINE void StaticValidityCheck() {
#ifdef PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
static_assert(
absl::disjunction<
internal::is_supported_string_type<Element>,
internal::is_supported_message_type<Element>>::value,
"We only support string and Message types in RepeatedPtrField.");
#endif // PROTOBUF_FUTURE_CONTAINER_STATIC_ASSERTS
}
public:

Loading…
Cancel
Save