Commit 5953a488 by Abseil Team Committed by Copybara-Service

Add internal traits to absl::string_view for lifetimebound detection

This helps compilers that understand `ABSL_ATTRIBUTE_LIFETIME_BOUND` flag constructs such as
`absl::StatusOr<std::string_view> str = std::string(...)`
as error-prone.

PiperOrigin-RevId: 621196818
Change-Id: I78d4c66854cde9d06eaaec05869d956376109d9c
parent d1dd9cd6
...@@ -359,6 +359,7 @@ cc_test( ...@@ -359,6 +359,7 @@ cc_test(
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/base:dynamic_annotations", "//absl/base:dynamic_annotations",
"//absl/meta:type_traits",
"@com_google_googletest//:gtest", "@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
......
...@@ -274,6 +274,7 @@ absl_cc_test( ...@@ -274,6 +274,7 @@ absl_cc_test(
absl::config absl::config
absl::core_headers absl::core_headers
absl::dynamic_annotations absl::dynamic_annotations
absl::type_traits
GTest::gmock_main GTest::gmock_main
) )
......
...@@ -173,6 +173,7 @@ class string_view { ...@@ -173,6 +173,7 @@ class string_view {
using reverse_iterator = const_reverse_iterator; using reverse_iterator = const_reverse_iterator;
using size_type = size_t; using size_type = size_t;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using absl_internal_is_view = std::true_type;
static constexpr size_type npos = static_cast<size_type>(-1); static constexpr size_type npos = static_cast<size_type>(-1);
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/meta/type_traits.h"
#if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__) #if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__)
// We don't control the death messaging when using std::string_view. // We don't control the death messaging when using std::string_view.
...@@ -46,6 +47,14 @@ ...@@ -46,6 +47,14 @@
namespace { namespace {
static_assert(!absl::type_traits_internal::IsOwner<absl::string_view>::value &&
absl::type_traits_internal::IsView<absl::string_view>::value,
"string_view is a view, not an owner");
static_assert(absl::type_traits_internal::IsLifetimeBoundAssignment<
absl::string_view, std::string>::value,
"lifetimebound assignment not detected");
// A minimal allocator that uses malloc(). // A minimal allocator that uses malloc().
template <typename T> template <typename T>
struct Mallocator { struct Mallocator {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment