1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
test1(void)3 void *test1(void) { return 0; }
4 
test2(const struct{int a;} * x)5 void test2 (const struct {int a;} *x) {
6   // expected-note@-1 {{variable 'x' declared const here}}
7 
8   x->a = 10;
9   // expected-error-re@-1 {{cannot assign to variable 'x' with const-qualified type 'const struct (anonymous struct at {{.*}}assign.c:5:19) *'}}
10 }
11 
12 typedef int arr[10];
test3()13 void test3() {
14   const arr b;      // expected-note {{variable 'b' declared const here}}
15   const int b2[10]; // expected-note {{variable 'b2' declared const here}}
16   b[4] = 1;         // expected-error {{cannot assign to variable 'b' with const-qualified type 'const arr' (aka 'int const[10]')}}
17   b2[4] = 1;        // expected-error {{cannot assign to variable 'b2' with const-qualified type 'const int [10]'}}
18 }
19 
20 typedef struct I {
21   const int a; // expected-note 4{{nested data member 'a' declared const here}} \
22                   expected-note 6{{data member 'a' declared const here}}
23 } I;
24 typedef struct J {
25   struct I i;
26 } J;
27 typedef struct K {
28   struct J *j;
29 } K;
30 
testI(struct I i1,struct I i2)31 void testI(struct I i1, struct I i2) {
32   i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}}
33 }
testJ1(struct J j1,struct J j2)34 void testJ1(struct J j1, struct J j2) {
35   j1 = j2; // expected-error {{cannot assign to variable 'j1' with nested const-qualified data member 'a'}}
36 }
testJ2(struct J j,struct I i)37 void testJ2(struct J j, struct I i) {
38   j.i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
39 }
testK1(struct K k,struct J j)40 void testK1(struct K k, struct J j) {
41   *(k.j) = j; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'a'}}
42 }
testK2(struct K k,struct I i)43 void testK2(struct K k, struct I i) {
44   k.j->i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
45 }
46 
testI_(I i1,I i2)47 void testI_(I i1, I i2) {
48   i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}}
49 }
testJ1_(J j1,J j2)50 void testJ1_(J j1, J j2) {
51   j1 = j2; // expected-error {{cannot assign to variable 'j1' with nested const-qualified data member 'a'}}
52 }
testJ2_(J j,I i)53 void testJ2_(J j, I i) {
54   j.i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
55 }
testK1_(K k,J j)56 void testK1_(K k, J j) {
57   *(k.j) = j; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'a'}}
58 }
testK2_(K k,I i)59 void testK2_(K k, I i) {
60   k.j->i = i; // expected-error {{cannot assign to non-static data member 'i' with const-qualified data member 'a'}}
61 }
62 
63 // PR39946: Recursive checking of hasConstFields caused stack overflow.
64 struct L { // expected-note {{definition of 'struct L' is not complete until the closing '}'}}
65   struct L field; // expected-error {{field has incomplete type 'struct L'}}
66 };
testL(struct L * l)67 void testL(struct L *l) {
68   *l = 0; // expected-error {{assigning to 'struct L' from incompatible type 'int'}}
69 }
70 
71 // Additionally, this example overflowed the stack when figuring out the field.
72 struct M1; // expected-note {{forward declaration of 'struct M1'}}
73 struct M2 {
74   //expected-note@+1 {{nested data member 'field' declared const here}}
75   const struct M1 field; // expected-error {{field has incomplete type 'const struct M1'}}
76 };
77 struct M1 {
78   struct M2 field;
79 };
80 
testM(struct M1 * l)81 void testM(struct M1 *l) {
82   *l = 0; // expected-error {{cannot assign to lvalue with nested const-qualified data member 'field'}}
83 }
84 
85 struct N1; // expected-note {{forward declaration of 'struct N1'}}
86 struct N2 {
87   struct N1 field; // expected-error {{field has incomplete type 'struct N1'}}
88 };
89 struct N1 {
90   struct N2 field;
91 };
92 
testN(struct N1 * l)93 void testN(struct N1 *l) {
94   *l = 0; // expected-error {{assigning to 'struct N1' from incompatible type 'int'}}
95 }
96