mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:54:47 +01:00
Rename Ptr<T> to Nonnull<T*> (#832)
Note that ptr.h also includes an enable_if change, to help avoid https://bugs.llvm.org/show_bug.cgi?id=51881
This commit is contained in:
@@ -16,10 +16,10 @@ class Arena {
|
||||
public:
|
||||
// Allocates an object in the arena, returning a pointer to it.
|
||||
template <typename T, typename... Args>
|
||||
auto New(Args&&... args) -> Ptr<T> {
|
||||
auto New(Args&&... args) -> Nonnull<T*> {
|
||||
auto smart_ptr =
|
||||
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
|
||||
Ptr<T> ptr = smart_ptr->Instance();
|
||||
Nonnull<T*> ptr = smart_ptr->Instance();
|
||||
arena.push_back(std::move(smart_ptr));
|
||||
return ptr;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ class Arena {
|
||||
explicit ArenaEntryTyped(Args&&... args)
|
||||
: instance(std::forward<Args>(args)...) {}
|
||||
|
||||
auto Instance() -> Ptr<T> { return Ptr<T>(&instance); }
|
||||
auto Instance() -> Nonnull<T*> { return Nonnull<T*>(&instance); }
|
||||
|
||||
private:
|
||||
T instance;
|
||||
|
||||
@@ -5,11 +5,18 @@
|
||||
#ifndef EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
#define EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// A non-nullable pointer. Written as `Ptr<T>` instead of `T*`.
|
||||
template <typename T>
|
||||
using Ptr = T* _Nonnull __attribute__((nonnull));
|
||||
// A non-nullable pointer. Written as `Nonnull<T*>` instead of `T*`.
|
||||
//
|
||||
// Note LLVM primarily enforces the attribute on function calls that can be
|
||||
// proven to be called with nullptr; in other places, this is essentially a
|
||||
// comment.
|
||||
template <typename T,
|
||||
typename std::enable_if_t<std::is_pointer_v<T>>* = nullptr>
|
||||
using Nonnull = T _Nonnull __attribute__((nonnull));
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
|
||||
Reference in New Issue
Block a user