diff options
author | Lawrence Wolf-Sonkin <lawrencews@google.com> | 2024-02-29 15:42:07 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-02-29 15:43:05 -0800 |
commit | e7fe9ec9ebfc6607765d489b76c9954e0a88c5d4 (patch) | |
tree | 054ca6fa08a0a081a7914c1e879857ba1eaa95dd | |
parent | 55d28d4b3b82f9a47b3fa9b811b675a032820621 (diff) |
[absl] Mark `absl::NoDestructor` methods with `absl::Nonnull` as appropriate
* It is stated in prose that the methods on `absl::NoDestructor` can never return a null pointer
* Now this is annotated via nullability annotations as well
PiperOrigin-RevId: 611619075
Change-Id: I078a5628430b7b221c009a5dcb849efe30409c28
-rw-r--r-- | absl/base/BUILD.bazel | 5 | ||||
-rw-r--r-- | absl/base/CMakeLists.txt | 1 | ||||
-rw-r--r-- | absl/base/no_destructor.h | 19 |
3 files changed, 15 insertions, 10 deletions
diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index 1eb8f098..f0e3e63f 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel @@ -74,7 +74,10 @@ cc_library( hdrs = ["no_destructor.h"], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, - deps = [":config"], + deps = [ + ":config", + ":nullability", + ], ) cc_library( diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt index 4cfc2285..09c622ab 100644 --- a/absl/base/CMakeLists.txt +++ b/absl/base/CMakeLists.txt @@ -62,6 +62,7 @@ absl_cc_library( "no_destructor.h" DEPS absl::config + absl::nullability COPTS ${ABSL_DEFAULT_COPTS} ) diff --git a/absl/base/no_destructor.h b/absl/base/no_destructor.h index ab689133..7b46456c 100644 --- a/absl/base/no_destructor.h +++ b/absl/base/no_destructor.h @@ -41,6 +41,7 @@ #include <utility> #include "absl/base/config.h" +#include "absl/base/nullability.h" namespace absl { ABSL_NAMESPACE_BEGIN @@ -140,11 +141,11 @@ class NoDestructor { // Pretend to be a smart pointer to T with deep constness. // Never returns a null pointer. T& operator*() { return *get(); } - T* operator->() { return get(); } - T* get() { return impl_.get(); } + absl::Nonnull<T*> operator->() { return get(); } + absl::Nonnull<T*> get() { return impl_.get(); } const T& operator*() const { return *get(); } - const T* operator->() const { return get(); } - const T* get() const { return impl_.get(); } + absl::Nonnull<const T*> operator->() const { return get(); } + absl::Nonnull<const T*> get() const { return impl_.get(); } private: class DirectImpl { @@ -152,8 +153,8 @@ class NoDestructor { template <typename... Args> explicit constexpr DirectImpl(Args&&... args) : value_(std::forward<Args>(args)...) {} - const T* get() const { return &value_; } - T* get() { return &value_; } + absl::Nonnull<const T*> get() const { return &value_; } + absl::Nonnull<T*> get() { return &value_; } private: T value_; @@ -165,14 +166,14 @@ class NoDestructor { explicit PlacementImpl(Args&&... args) { new (&space_) T(std::forward<Args>(args)...); } - const T* get() const { + absl::Nonnull<const T*> get() const { return Launder(reinterpret_cast<const T*>(&space_)); } - T* get() { return Launder(reinterpret_cast<T*>(&space_)); } + absl::Nonnull<T*> get() { return Launder(reinterpret_cast<T*>(&space_)); } private: template <typename P> - static P* Launder(P* p) { + static absl::Nonnull<P*> Launder(absl::Nonnull<P*> p) { #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L return std::launder(p); #elif ABSL_HAVE_BUILTIN(__builtin_launder) |