xref: /llvm-project/clang/test/C/C2y/n3346.c (revision 2b885f056585f82903f067840e54557a5b444b65)
1 // RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic -ffreestanding %s
2 // RUN: %clang_cc1 -std=c99 -verify=expected,ped -Wall -pedantic -ffreestanding %s
3 
4 /* WG14 N3346: Yes
5  * Slay Some Earthly Demons VIII
6  *
7  * Updates some undefined behavior during initialization to instead be a
8  * constraint violation.
9  */
10 
11 // The initializer for a scalar shall be a single expression, optionally
12 // enclosed in braces, or it shall be an empty initializer.
13 int i = 12, j = {12}, k = {}; // ped-warning {{use of an empty initializer is a C23 extension}}
14 
15 struct S {
16   int i;
17   float f;
18   int : 0;
19   char c;
20 };
21 
22 void test1(void) {
23   // The initializer for an object that has structure or union type shall be
24   // either a single expression that has compatible type or a brace-enclosed
25   // list of initializers for the elements or named members.
26   struct S s1 = { 1, 1.2f, 'a' };
27   struct S s2 = s1;
28 
29   // Despite being structurally identical to S, T is not compatible with S.
30   struct T { int i; float f; int : 0; char c; } t;
31   struct S s3 = t; // expected-error {{initializing 'struct S' with an expression of incompatible type 'struct T'}}
32 }
33 
34 void test2(void) {
35   typedef __WCHAR_TYPE__ wchar_t;
36 
37   // The initializer for an array shall be either a string literal, optionally
38   // enclosed in braces, or a brace-enclosed list of initializers for the
39   // elements. An array initialized by character string literal or UTF-8 string
40   // literal shall have a character type as element type. An array initialized
41   // with a wide string literal shall have element type compatible with a
42   // qualified or unqualified wchar_t, char16_t, or char32_t, and the string
43   // literal shall have the corresponding encoding prefix (L, u, or U,
44   // respectively).
45   char str1[] = "string literal";
46   char str2[] = { "string literal" };
47 
48   float str5[] = "this doesn't work";          // expected-error {{array initializer must be an initializer list}}
49   float str6[] = { "this also doesn't work" }; // expected-error {{initializing 'float' with an expression of incompatible type 'char[23]'}}
50 
51   wchar_t str7[] = L"string literal";
52   wchar_t str8[] = { L"string literal" };
53 
54 #if __STDC_VERSION__ >= 201112L
55   typedef __CHAR16_TYPE__ char16_t;
56   typedef __CHAR32_TYPE__ char32_t;
57 
58   char str3[] = u8"string literal";
59   char str4[] = { u8"string literal" };
60 
61   char16_t str9[] = u"string literal";
62   char16_t str10[] = { u"string literal" };
63   char32_t str11[] = U"string literal";
64   char32_t str12[] = { U"string literal" };
65 
66   char16_t str15[] = "nope";     // expected-error {{initializing wide char array with non-wide string literal}}
67   char16_t str16[] = { "nope" }; // expected-error-re {{incompatible pointer to integer conversion initializing 'char16_t' (aka '{{.*}}') with an expression of type 'char[5]'}}
68   char32_t str17[] = "nope";     // expected-error {{initializing wide char array with non-wide string literal}}
69   char32_t str18[] = { "nope" }; // expected-error-re {{incompatible pointer to integer conversion initializing 'char32_t' (aka '{{.*}}') with an expression of type 'char[5]'}}
70 #endif
71 
72   wchar_t str13[] = "nope";      // expected-error {{initializing wide char array with non-wide string literal}}
73   wchar_t str14[] = { "nope" };  // expected-error-re {{incompatible pointer to integer conversion initializing 'wchar_t' (aka '{{.*}}') with an expression of type 'char[5]'}}
74 }
75