xref: /aosp_15_r20/external/clang/test/SemaObjCXX/nullability-pragmas.mm (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -fsyntax-only -fblocks -I %S/Inputs %s -verify
2*67e74705SXin Li
3*67e74705SXin Li#include "nullability-pragmas-1.h"
4*67e74705SXin Li#include "nullability-pragmas-2.h"
5*67e74705SXin Li#include "nullability-pragmas-generics-1.h"
6*67e74705SXin Li
7*67e74705SXin Li#if !__has_feature(assume_nonnull)
8*67e74705SXin Li#  error assume_nonnull feature is not set
9*67e74705SXin Li#endif
10*67e74705SXin Li
11*67e74705SXin Li#if !__has_extension(assume_nonnull)
12*67e74705SXin Li#  error assume_nonnull extension is not set
13*67e74705SXin Li#endif
14*67e74705SXin Li
15*67e74705SXin Livoid test_pragmas_1(A * _Nonnull a, AA * _Nonnull aa) {
16*67e74705SXin Li  f1(0); // okay: no nullability annotations
17*67e74705SXin Li  f2(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
18*67e74705SXin Li  f3(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
19*67e74705SXin Li  f4(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
20*67e74705SXin Li  f5(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
21*67e74705SXin Li  f6(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
22*67e74705SXin Li  f7(0); // okay
23*67e74705SXin Li  f8(0); // okay
24*67e74705SXin Li  f9(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
25*67e74705SXin Li  f10(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
26*67e74705SXin Li  f11(0); // okay
27*67e74705SXin Li  f12(0); // okay
28*67e74705SXin Li  [a method1:0]; // expected-warning{{null passed to a callee that requires a non-null argument}}
29*67e74705SXin Li
30*67e74705SXin Li  f17(a); // expected-error{{no matching function for call to 'f17'}}
31*67e74705SXin Li  [a method3: a]; // expected-error{{cannot initialize a parameter of type 'NSError * _Nullable * _Nullable' with an lvalue of type 'A * _Nonnull'}}
32*67e74705SXin Li  [a method4: a]; // expected-error{{cannot initialize a parameter of type 'NSErrorPtr  _Nullable * _Nullable' (aka 'NSError **') with an lvalue of type 'A * _Nonnull'}}
33*67e74705SXin Li
34*67e74705SXin Li  float *ptr;
35*67e74705SXin Li  ptr = f13(); // expected-error{{assigning to 'float *' from incompatible type 'int_ptr _Nonnull' (aka 'int *')}}
36*67e74705SXin Li  ptr = f14(); // expected-error{{assigning to 'float *' from incompatible type 'A * _Nonnull'}}
37*67e74705SXin Li  ptr = [a method1:a]; // expected-error{{assigning to 'float *' from incompatible type 'A * _Nonnull'}}
38*67e74705SXin Li  ptr = a.aProp; // expected-error{{assigning to 'float *' from incompatible type 'A * _Nonnull'}}
39*67e74705SXin Li  ptr = global_int_ptr; // expected-error{{assigning to 'float *' from incompatible type 'int * _Nonnull'}}
40*67e74705SXin Li  ptr = f15(); // expected-error{{assigning to 'float *' from incompatible type 'int * _Null_unspecified'}}
41*67e74705SXin Li  ptr = f16(); // expected-error{{assigning to 'float *' from incompatible type 'A * _Null_unspecified'}}
42*67e74705SXin Li  ptr = [a method2]; // expected-error{{assigning to 'float *' from incompatible type 'A * _Null_unspecified'}}
43*67e74705SXin Li
44*67e74705SXin Li  ptr = aa->ivar1; // expected-error{{from incompatible type 'id'}}
45*67e74705SXin Li  ptr = aa->ivar2; // expected-error{{from incompatible type 'id _Nonnull'}}
46*67e74705SXin Li}
47*67e74705SXin Li
48*67e74705SXin Livoid test_pragmas_generics(void) {
49*67e74705SXin Li  float *fp;
50*67e74705SXin Li
51*67e74705SXin Li  NSGeneric<C *> *genC;
52*67e74705SXin Li  fp = [genC tee]; // expected-error{{from incompatible type 'C *'}}
53*67e74705SXin Li  fp = [genC maybeTee]; // expected-error{{from incompatible type 'C * _Nullable'}}
54*67e74705SXin Li
55*67e74705SXin Li  Generic_with_C genC2;
56*67e74705SXin Li  fp = genC2; // expected-error{{from incompatible type 'Generic_with_C' (aka 'NSGeneric<C *> *')}}
57*67e74705SXin Li}
58