1 /* Test for compound literals: in C99 only. Test for invalid uses. */
2 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
3 /* { dg-do compile } */
4 /* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
5
6 struct s { int a; int b; };
7 union u { int c; int d; };
8
9 struct si;
10 union ui;
11
12 void
foo(int a)13 foo (int a)
14 {
15 /* The type name must not be incomplete (apart from arrays of unknown
16 size), or a function type, or a VLA type. */
17 (void) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
18 /* { dg-error "init" "void type" { target *-*-* } 17 } */
19 &(struct si) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
20 /* { dg-error "init" "incomplete struct type" { target *-*-* } 19 } */
21 /* { dg-error "invalid use of undefined type" "" { target *-*-* } 19 } */
22 &(union ui) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
23 /* { dg-error "init" "incomplete union type" { target *-*-* } 22 } */
24 /* { dg-error "invalid use of undefined type" "" { target *-*-* } 22 } */
25 (void (void)) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
26 /* { dg-error "init" "function type" { target *-*-* } 25 } */
27 (int [a]) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
28 /* { dg-error "init|variable" "VLA type" { target *-*-* } 27 } */
29 /* Initializers must not attempt to initialize outside the object
30 declared. */
31 (int [1]) { [1] = 2 }; /* { dg-bogus "warning" "warning in place of error" } */
32 /* { dg-error "init" "value outside array" { target *-*-* } 31 } */
33 (int [1]) { [-1] = 2 }; /* { dg-bogus "warning" "warning in place of error" } */
34 /* { dg-error "init" "value outside array" { target *-*-* } 33 } */
35 (int [1]) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
36 /* { dg-error "init" "value outside array" { target *-*-* } 35 } */
37 }
38
39 int z;
40
41 /* Outside a function, initializers must be constant. */
42 struct s *s0 = &(struct s) { 0, z }; /* { dg-bogus "warning" "warning in place of error" } */
43 /* { dg-error "init" "non-const" { target *-*-* } 42 } */
44 int sz = sizeof((struct s) { 0, z }); /* { dg-bogus "warning" "warning in place of error" } */
45 /* { dg-error "init" "non-const" { target *-*-* } 44 } */
46
47 /* Compound literals aren't themselves constant expressions. */
48 int x = (int) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
49 /* { dg-error "init" "non-const" { target *-*-* } 48 } */
50
51 /* Nor are they suitable structure or union initializers
52 outside a function. */
53 struct s s1 = (struct s) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
54 /* { dg-error "init" "struct bad init" { target *-*-* } 53 } */
55 union u u1 = (union u) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
56 /* { dg-error "init" "union bad init" { target *-*-* } 55 } */
57
58 /* They aren't suitable for array initializers, either inside or outside
59 a function. */
60 int y[2] = (int [2]) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
61 /* { dg-error "init" "array bad init" { target *-*-* } 60 } */
62
63 void
bar(void)64 bar (void)
65 {
66 struct s s2 = (struct s) { 0, 1 };
67 union u u2 = (union u) { 0 };
68 int z[2] = (int [2]) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
69 /* { dg-error "init" "array bad init" { target *-*-* } 68 } */
70 }
71