xref: /aosp_15_r20/external/abseil-cpp/absl/base/nullability.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2023 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // File: nullability.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This header file defines a set of "templated annotations" for designating the
20*9356374aSAndroid Build Coastguard Worker // expected nullability of pointers. These annotations allow you to designate
21*9356374aSAndroid Build Coastguard Worker // pointers in one of three classification states:
22*9356374aSAndroid Build Coastguard Worker //
23*9356374aSAndroid Build Coastguard Worker //  * "Non-null" (for pointers annotated `Nonnull<T>`), indicating that it is
24*9356374aSAndroid Build Coastguard Worker //    invalid for the given pointer to ever be null.
25*9356374aSAndroid Build Coastguard Worker //  * "Nullable" (for pointers annotated `Nullable<T>`), indicating that it is
26*9356374aSAndroid Build Coastguard Worker //    valid for the given pointer to be null.
27*9356374aSAndroid Build Coastguard Worker //  * "Unknown" (for pointers annotated `NullabilityUnknown<T>`), indicating
28*9356374aSAndroid Build Coastguard Worker //    that the given pointer has not been yet classified as either nullable or
29*9356374aSAndroid Build Coastguard Worker //    non-null. This is the default state of unannotated pointers.
30*9356374aSAndroid Build Coastguard Worker //
31*9356374aSAndroid Build Coastguard Worker // NOTE: unannotated pointers implicitly bear the annotation
32*9356374aSAndroid Build Coastguard Worker // `NullabilityUnknown<T>`; you should rarely, if ever, see this annotation used
33*9356374aSAndroid Build Coastguard Worker // in the codebase explicitly.
34*9356374aSAndroid Build Coastguard Worker //
35*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
36*9356374aSAndroid Build Coastguard Worker // Nullability and Contracts
37*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
38*9356374aSAndroid Build Coastguard Worker //
39*9356374aSAndroid Build Coastguard Worker // These nullability annotations allow you to more clearly specify contracts on
40*9356374aSAndroid Build Coastguard Worker // software components by narrowing the *preconditions*, *postconditions*, and
41*9356374aSAndroid Build Coastguard Worker // *invariants* of pointer state(s) in any given interface. It then depends on
42*9356374aSAndroid Build Coastguard Worker // context who is responsible for fulfilling the annotation's requirements.
43*9356374aSAndroid Build Coastguard Worker //
44*9356374aSAndroid Build Coastguard Worker // For example, a function may receive a pointer argument. Designating that
45*9356374aSAndroid Build Coastguard Worker // pointer argument as "non-null" tightens the precondition of the contract of
46*9356374aSAndroid Build Coastguard Worker // that function. It is then the responsibility of anyone calling such a
47*9356374aSAndroid Build Coastguard Worker // function to ensure that the passed pointer is not null.
48*9356374aSAndroid Build Coastguard Worker //
49*9356374aSAndroid Build Coastguard Worker // Similarly, a function may have a pointer as a return value. Designating that
50*9356374aSAndroid Build Coastguard Worker // return value as "non-null" tightens the postcondition of the contract of that
51*9356374aSAndroid Build Coastguard Worker // function. In this case, however, it is the responsibility of the function
52*9356374aSAndroid Build Coastguard Worker // itself to ensure that the returned pointer is not null.
53*9356374aSAndroid Build Coastguard Worker //
54*9356374aSAndroid Build Coastguard Worker // Clearly defining these contracts allows providers (and consumers) of such
55*9356374aSAndroid Build Coastguard Worker // pointers to have more confidence in their null state. If a function declares
56*9356374aSAndroid Build Coastguard Worker // a return value as "non-null", for example, the caller should not need to
57*9356374aSAndroid Build Coastguard Worker // check whether the returned value is `nullptr`; it can simply assume the
58*9356374aSAndroid Build Coastguard Worker // pointer is valid.
59*9356374aSAndroid Build Coastguard Worker //
60*9356374aSAndroid Build Coastguard Worker // Of course most interfaces already have expectations on the nullability state
61*9356374aSAndroid Build Coastguard Worker // of pointers, and these expectations are, in effect, a contract; often,
62*9356374aSAndroid Build Coastguard Worker // however, those contracts are either poorly or partially specified, assumed,
63*9356374aSAndroid Build Coastguard Worker // or misunderstood. These nullability annotations are designed to allow you to
64*9356374aSAndroid Build Coastguard Worker // formalize those contracts within the codebase.
65*9356374aSAndroid Build Coastguard Worker //
66*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
67*9356374aSAndroid Build Coastguard Worker // Using Nullability Annotations
68*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
69*9356374aSAndroid Build Coastguard Worker //
70*9356374aSAndroid Build Coastguard Worker // It is important to note that these annotations are not distinct strong
71*9356374aSAndroid Build Coastguard Worker // *types*. They are alias templates defined to be equal to the underlying
72*9356374aSAndroid Build Coastguard Worker // pointer type. A pointer annotated `Nonnull<T*>`, for example, is simply a
73*9356374aSAndroid Build Coastguard Worker // pointer of type `T*`. Each annotation acts as a form of documentation about
74*9356374aSAndroid Build Coastguard Worker // the contract for the given pointer. Each annotation requires providers or
75*9356374aSAndroid Build Coastguard Worker // consumers of these pointers across API boundaries to take appropriate steps
76*9356374aSAndroid Build Coastguard Worker // when setting or using these pointers:
77*9356374aSAndroid Build Coastguard Worker //
78*9356374aSAndroid Build Coastguard Worker // * "Non-null" pointers should never be null. It is the responsibility of the
79*9356374aSAndroid Build Coastguard Worker //   provider of this pointer to ensure that the pointer may never be set to
80*9356374aSAndroid Build Coastguard Worker //   null. Consumers of such pointers can treat such pointers as non-null.
81*9356374aSAndroid Build Coastguard Worker // * "Nullable" pointers may or may not be null. Consumers of such pointers
82*9356374aSAndroid Build Coastguard Worker //   should precede any usage of that pointer (e.g. a dereference operation)
83*9356374aSAndroid Build Coastguard Worker //   with a a `nullptr` check.
84*9356374aSAndroid Build Coastguard Worker // * "Unknown" pointers may be either "non-null" or "nullable" but have not been
85*9356374aSAndroid Build Coastguard Worker //   definitively determined to be in either classification state. Providers of
86*9356374aSAndroid Build Coastguard Worker //   such pointers across API boundaries should determine --  over time -- to
87*9356374aSAndroid Build Coastguard Worker //   annotate the pointer in either of the above two states. Consumers of such
88*9356374aSAndroid Build Coastguard Worker //   pointers across an API boundary should continue to treat such pointers as
89*9356374aSAndroid Build Coastguard Worker //   they currently do.
90*9356374aSAndroid Build Coastguard Worker //
91*9356374aSAndroid Build Coastguard Worker // Example:
92*9356374aSAndroid Build Coastguard Worker //
93*9356374aSAndroid Build Coastguard Worker // // PaySalary() requires the passed pointer to an `Employee` to be non-null.
94*9356374aSAndroid Build Coastguard Worker // void PaySalary(absl::Nonnull<Employee *> e) {
95*9356374aSAndroid Build Coastguard Worker //   pay(e->salary);  // OK to dereference
96*9356374aSAndroid Build Coastguard Worker // }
97*9356374aSAndroid Build Coastguard Worker //
98*9356374aSAndroid Build Coastguard Worker // // CompleteTransaction() guarantees the returned pointer to an `Account` to
99*9356374aSAndroid Build Coastguard Worker // // be non-null.
100*9356374aSAndroid Build Coastguard Worker // absl::Nonnull<Account *> balance CompleteTransaction(double fee) {
101*9356374aSAndroid Build Coastguard Worker // ...
102*9356374aSAndroid Build Coastguard Worker // }
103*9356374aSAndroid Build Coastguard Worker //
104*9356374aSAndroid Build Coastguard Worker // // Note that specifying a nullability annotation does not prevent someone
105*9356374aSAndroid Build Coastguard Worker // // from violating the contract:
106*9356374aSAndroid Build Coastguard Worker //
107*9356374aSAndroid Build Coastguard Worker // Nullable<Employee *> find(Map& employees, std::string_view name);
108*9356374aSAndroid Build Coastguard Worker //
109*9356374aSAndroid Build Coastguard Worker // void g(Map& employees) {
110*9356374aSAndroid Build Coastguard Worker //   Employee *e = find(employees, "Pat");
111*9356374aSAndroid Build Coastguard Worker //   // `e` can now be null.
112*9356374aSAndroid Build Coastguard Worker //   PaySalary(e); // Violates contract, but compiles!
113*9356374aSAndroid Build Coastguard Worker // }
114*9356374aSAndroid Build Coastguard Worker //
115*9356374aSAndroid Build Coastguard Worker // Nullability annotations, in other words, are useful for defining and
116*9356374aSAndroid Build Coastguard Worker // narrowing contracts; *enforcement* of those contracts depends on use and any
117*9356374aSAndroid Build Coastguard Worker // additional (static or dynamic analysis) tooling.
118*9356374aSAndroid Build Coastguard Worker //
119*9356374aSAndroid Build Coastguard Worker // NOTE: The "unknown" annotation state indicates that a pointer's contract has
120*9356374aSAndroid Build Coastguard Worker // not yet been positively identified. The unknown state therefore acts as a
121*9356374aSAndroid Build Coastguard Worker // form of documentation of your technical debt, and a codebase that adopts
122*9356374aSAndroid Build Coastguard Worker // nullability annotations should aspire to annotate every pointer as either
123*9356374aSAndroid Build Coastguard Worker // "non-null" or "nullable".
124*9356374aSAndroid Build Coastguard Worker //
125*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
126*9356374aSAndroid Build Coastguard Worker // Applicability of Nullability Annotations
127*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
128*9356374aSAndroid Build Coastguard Worker //
129*9356374aSAndroid Build Coastguard Worker // By default, nullability annotations are applicable to raw and smart
130*9356374aSAndroid Build Coastguard Worker // pointers. User-defined types can indicate compatibility with nullability
131*9356374aSAndroid Build Coastguard Worker // annotations by adding the ABSL_NULLABILITY_COMPATIBLE attribute.
132*9356374aSAndroid Build Coastguard Worker //
133*9356374aSAndroid Build Coastguard Worker // // Example:
134*9356374aSAndroid Build Coastguard Worker // struct ABSL_NULLABILITY_COMPATIBLE MyPtr {
135*9356374aSAndroid Build Coastguard Worker //   ...
136*9356374aSAndroid Build Coastguard Worker // };
137*9356374aSAndroid Build Coastguard Worker //
138*9356374aSAndroid Build Coastguard Worker // Note: For the time being, nullability-compatible classes should additionally
139*9356374aSAndroid Build Coastguard Worker // be marked with an `absl_nullability_compatible` nested type (this will soon
140*9356374aSAndroid Build Coastguard Worker // be deprecated). The actual definition of this inner type is not relevant as
141*9356374aSAndroid Build Coastguard Worker // it is used merely as a marker. It is common to use a using declaration of
142*9356374aSAndroid Build Coastguard Worker // `absl_nullability_compatible` set to void.
143*9356374aSAndroid Build Coastguard Worker //
144*9356374aSAndroid Build Coastguard Worker // // Example:
145*9356374aSAndroid Build Coastguard Worker // struct MyPtr {
146*9356374aSAndroid Build Coastguard Worker //   using absl_nullability_compatible = void;
147*9356374aSAndroid Build Coastguard Worker //   ...
148*9356374aSAndroid Build Coastguard Worker // };
149*9356374aSAndroid Build Coastguard Worker //
150*9356374aSAndroid Build Coastguard Worker // DISCLAIMER:
151*9356374aSAndroid Build Coastguard Worker // ===========================================================================
152*9356374aSAndroid Build Coastguard Worker // These nullability annotations are primarily a human readable signal about the
153*9356374aSAndroid Build Coastguard Worker // intended contract of the pointer. They are not *types* and do not currently
154*9356374aSAndroid Build Coastguard Worker // provide any correctness guarantees. For example, a pointer annotated as
155*9356374aSAndroid Build Coastguard Worker // `Nonnull<T*>` is *not guaranteed* to be non-null, and the compiler won't
156*9356374aSAndroid Build Coastguard Worker // alert or prevent assignment of a `Nullable<T*>` to a `Nonnull<T*>`.
157*9356374aSAndroid Build Coastguard Worker // ===========================================================================
158*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_NULLABILITY_H_
159*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_NULLABILITY_H_
160*9356374aSAndroid Build Coastguard Worker 
161*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
162*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/nullability_impl.h"
163*9356374aSAndroid Build Coastguard Worker 
164*9356374aSAndroid Build Coastguard Worker namespace absl {
165*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
166*9356374aSAndroid Build Coastguard Worker 
167*9356374aSAndroid Build Coastguard Worker // absl::Nonnull
168*9356374aSAndroid Build Coastguard Worker //
169*9356374aSAndroid Build Coastguard Worker // The indicated pointer is never null. It is the responsibility of the provider
170*9356374aSAndroid Build Coastguard Worker // of this pointer across an API boundary to ensure that the pointer is never
171*9356374aSAndroid Build Coastguard Worker // set to null. Consumers of this pointer across an API boundary may safely
172*9356374aSAndroid Build Coastguard Worker // dereference the pointer.
173*9356374aSAndroid Build Coastguard Worker //
174*9356374aSAndroid Build Coastguard Worker // Example:
175*9356374aSAndroid Build Coastguard Worker //
176*9356374aSAndroid Build Coastguard Worker // // `employee` is designated as not null.
177*9356374aSAndroid Build Coastguard Worker // void PaySalary(absl::Nonnull<Employee *> employee) {
178*9356374aSAndroid Build Coastguard Worker //   pay(*employee);  // OK to dereference
179*9356374aSAndroid Build Coastguard Worker // }
180*9356374aSAndroid Build Coastguard Worker template <typename T>
181*9356374aSAndroid Build Coastguard Worker using Nonnull = nullability_internal::NonnullImpl<T>;
182*9356374aSAndroid Build Coastguard Worker 
183*9356374aSAndroid Build Coastguard Worker // absl::Nullable
184*9356374aSAndroid Build Coastguard Worker //
185*9356374aSAndroid Build Coastguard Worker // The indicated pointer may, by design, be either null or non-null. Consumers
186*9356374aSAndroid Build Coastguard Worker // of this pointer across an API boundary should perform a `nullptr` check
187*9356374aSAndroid Build Coastguard Worker // before performing any operation using the pointer.
188*9356374aSAndroid Build Coastguard Worker //
189*9356374aSAndroid Build Coastguard Worker // Example:
190*9356374aSAndroid Build Coastguard Worker //
191*9356374aSAndroid Build Coastguard Worker // // `employee` may  be null.
192*9356374aSAndroid Build Coastguard Worker // void PaySalary(absl::Nullable<Employee *> employee) {
193*9356374aSAndroid Build Coastguard Worker //   if (employee != nullptr) {
194*9356374aSAndroid Build Coastguard Worker //     Pay(*employee);  // OK to dereference
195*9356374aSAndroid Build Coastguard Worker //   }
196*9356374aSAndroid Build Coastguard Worker // }
197*9356374aSAndroid Build Coastguard Worker template <typename T>
198*9356374aSAndroid Build Coastguard Worker using Nullable = nullability_internal::NullableImpl<T>;
199*9356374aSAndroid Build Coastguard Worker 
200*9356374aSAndroid Build Coastguard Worker // absl::NullabilityUnknown (default)
201*9356374aSAndroid Build Coastguard Worker //
202*9356374aSAndroid Build Coastguard Worker // The indicated pointer has not yet been determined to be definitively
203*9356374aSAndroid Build Coastguard Worker // "non-null" or "nullable." Providers of such pointers across API boundaries
204*9356374aSAndroid Build Coastguard Worker // should, over time, annotate such pointers as either "non-null" or "nullable."
205*9356374aSAndroid Build Coastguard Worker // Consumers of these pointers across an API boundary should treat such pointers
206*9356374aSAndroid Build Coastguard Worker // with the same caution they treat currently unannotated pointers. Most
207*9356374aSAndroid Build Coastguard Worker // existing code will have "unknown"  pointers, which should eventually be
208*9356374aSAndroid Build Coastguard Worker // migrated into one of the above two nullability states: `Nonnull<T>` or
209*9356374aSAndroid Build Coastguard Worker //  `Nullable<T>`.
210*9356374aSAndroid Build Coastguard Worker //
211*9356374aSAndroid Build Coastguard Worker // NOTE: Because this annotation is the global default state, unannotated
212*9356374aSAndroid Build Coastguard Worker // pointers are assumed to have "unknown" semantics. This assumption is designed
213*9356374aSAndroid Build Coastguard Worker // to minimize churn and reduce clutter within the codebase.
214*9356374aSAndroid Build Coastguard Worker //
215*9356374aSAndroid Build Coastguard Worker // Example:
216*9356374aSAndroid Build Coastguard Worker //
217*9356374aSAndroid Build Coastguard Worker // // `employee`s nullability state is unknown.
218*9356374aSAndroid Build Coastguard Worker // void PaySalary(absl::NullabilityUnknown<Employee *> employee) {
219*9356374aSAndroid Build Coastguard Worker //   Pay(*employee); // Potentially dangerous. API provider should investigate.
220*9356374aSAndroid Build Coastguard Worker // }
221*9356374aSAndroid Build Coastguard Worker //
222*9356374aSAndroid Build Coastguard Worker // Note that a pointer without an annotation, by default, is assumed to have the
223*9356374aSAndroid Build Coastguard Worker // annotation `NullabilityUnknown`.
224*9356374aSAndroid Build Coastguard Worker //
225*9356374aSAndroid Build Coastguard Worker // // `employee`s nullability state is unknown.
226*9356374aSAndroid Build Coastguard Worker // void PaySalary(Employee* employee) {
227*9356374aSAndroid Build Coastguard Worker //   Pay(*employee); // Potentially dangerous. API provider should investigate.
228*9356374aSAndroid Build Coastguard Worker // }
229*9356374aSAndroid Build Coastguard Worker template <typename T>
230*9356374aSAndroid Build Coastguard Worker using NullabilityUnknown = nullability_internal::NullabilityUnknownImpl<T>;
231*9356374aSAndroid Build Coastguard Worker 
232*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
233*9356374aSAndroid Build Coastguard Worker }  // namespace absl
234*9356374aSAndroid Build Coastguard Worker 
235*9356374aSAndroid Build Coastguard Worker // ABSL_NULLABILITY_COMPATIBLE
236*9356374aSAndroid Build Coastguard Worker //
237*9356374aSAndroid Build Coastguard Worker // Indicates that a class is compatible with nullability annotations.
238*9356374aSAndroid Build Coastguard Worker //
239*9356374aSAndroid Build Coastguard Worker // For example:
240*9356374aSAndroid Build Coastguard Worker //
241*9356374aSAndroid Build Coastguard Worker // struct ABSL_NULLABILITY_COMPATIBLE MyPtr {
242*9356374aSAndroid Build Coastguard Worker //   ...
243*9356374aSAndroid Build Coastguard Worker // };
244*9356374aSAndroid Build Coastguard Worker #if ABSL_HAVE_FEATURE(nullability_on_classes)
245*9356374aSAndroid Build Coastguard Worker #define ABSL_NULLABILITY_COMPATIBLE _Nullable
246*9356374aSAndroid Build Coastguard Worker #else
247*9356374aSAndroid Build Coastguard Worker #define ABSL_NULLABILITY_COMPATIBLE
248*9356374aSAndroid Build Coastguard Worker #endif
249*9356374aSAndroid Build Coastguard Worker 
250*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_BASE_NULLABILITY_H_
251