1 /* Test wrong usage of C23 nullptr. */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c2x -pedantic-errors -Wall -Wextra -Wno-unused-variable" } */
4
5 typedef __typeof__(nullptr) nullptr_t;
6
7 void g (nullptr_t); /* { dg-message "expected .nullptr_t. but argument is of type .int." } */
8 nullptr_t cmp (void);
9
10 void
11 test1 (int *p)
12 {
13 (void) (p > nullptr); /* { dg-error "ordered comparison" } */
14 (void) (p >= nullptr); /* { dg-error "ordered comparison" } */
15 (void) (p < nullptr); /* { dg-error "ordered comparison" } */
16 (void) (p <= nullptr); /* { dg-error "ordered comparison" } */
17 (void) (nullptr == 1); /* { dg-error "invalid operands" } */
18 (void) (1 == nullptr); /* { dg-error "invalid operands" } */
19 (void) (nullptr != 1); /* { dg-error "invalid operands" } */
20 (void) (1 != nullptr); /* { dg-error "invalid operands" } */
21 (void) (1 > nullptr); /* { dg-error "invalid operands" } */
22 }
23
24 void
25 test2 (void)
26 {
27 const nullptr_t nptr = nullptr;
28 int p = nullptr; /* { dg-error "incompatible types" } */
29 float d = nullptr; /* { dg-error "incompatible types" } */
30 char arr[10] = { nullptr }; /* { dg-error "incompatible types" } */
31
32 /* Unary '+' is not valid for nullptr. */
33 +nullptr; /* { dg-error "wrong type argument to unary plus" } */
34
35 g (0.0); /* { dg-error "incompatible type" } */
36 g (1); /* { dg-error "incompatible type" } */
37 g ((int) (float) 0.0); /* { dg-error "incompatible type" } */
38 int i = 42 + nullptr; /* { dg-error "invalid operands" } */
39
40 /* The assignment of an object of type nullptr_t with a value of another
41 type, other than a null pointer constant, is a constraint
42 violation. */
43 nullptr_t m;
44 m = 1; /* { dg-error "incompatible types" } */
45 m = 0.0; /* { dg-error "incompatible types" } */
46 (void) m;
47 nullptr_t o = 1; /* { dg-error "incompatible types" } */
48 (nullptr_t) 0.0; /* { dg-error "conversion" } */
49 (nullptr_t) (int) (float) 0.0; /* { dg-error "conversion" } */
50
51 switch (nullptr); /* { dg-error "switch quantity not an integer" } */
52 }
53
54 /* If a second or third operand of type nullptr_t is used that is not a null
55 pointer constant and the other operand is not a pointer or does not have
56 itself nullptr_t, a constraint is violated even if that other operand is
57 a null pointer constant such as 0. */
58 void
59 test3 (_Bool b, int i)
60 {
61 const nullptr_t nptr = nullptr;
62 __auto_type a1 = b ? nptr : i; /* { dg-error "type mismatch" } */
63 __auto_type a2 = b ? i : nptr; /* { dg-error "type mismatch" } */
64 __auto_type a3 = b ? nptr : 0; /* { dg-error "type mismatch" } */
65 __auto_type a4 = b ? 0 : nptr; /* { dg-error "type mismatch" } */
66 __auto_type a5 = b ? 0 : nullptr; /* { dg-error "type mismatch" } */
67 __auto_type a6 = b ? nullptr : 0; /* { dg-error "type mismatch" } */
68 }