summaryrefslogtreecommitdiff
path: root/absl/strings
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2024-04-02 09:03:50 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2024-04-02 09:04:52 -0700
commit5953a4885ae8dc4baa3f66292d6519e0ec254313 (patch)
tree1e93b3aa12a6d30806e0f7ba5d3ba61f562dee73 /absl/strings
parentd1dd9cd60a7167bc6e2f34877660e747e5b92842 (diff)
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
Diffstat (limited to 'absl/strings')
-rw-r--r--absl/strings/BUILD.bazel1
-rw-r--r--absl/strings/CMakeLists.txt1
-rw-r--r--absl/strings/string_view.h1
-rw-r--r--absl/strings/string_view_test.cc9
4 files changed, 12 insertions, 0 deletions
diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel
index 8b307832..d93a78a9 100644
--- a/absl/strings/BUILD.bazel
+++ b/absl/strings/BUILD.bazel
@@ -359,6 +359,7 @@ cc_test(
"//absl/base:config",
"//absl/base:core_headers",
"//absl/base:dynamic_annotations",
+ "//absl/meta:type_traits",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt
index 9258e553..53e85188 100644
--- a/absl/strings/CMakeLists.txt
+++ b/absl/strings/CMakeLists.txt
@@ -274,6 +274,7 @@ absl_cc_test(
absl::config
absl::core_headers
absl::dynamic_annotations
+ absl::type_traits
GTest::gmock_main
)
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index b393c6fc..b1fb7a10 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -173,6 +173,7 @@ class string_view {
using reverse_iterator = const_reverse_iterator;
using size_type = size_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);
diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc
index 251f1842..e978fc3f 100644
--- a/absl/strings/string_view_test.cc
+++ b/absl/strings/string_view_test.cc
@@ -32,6 +32,7 @@
#include "gtest/gtest.h"
#include "absl/base/config.h"
+#include "absl/meta/type_traits.h"
#if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__)
// We don't control the death messaging when using std::string_view.
@@ -46,6 +47,14 @@
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().
template <typename T>
struct Mallocator {