| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify %s |
| 2 |
|
| 3 |
void *test1(void) { return 0; } |
| 4 |
|
| 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 (unnamed at {{.*}}assign.c:5:19) *'}} |
| 10 |
} |
| 11 |
|
| 12 |
typedef int arr[10]; |
| 13 |
void test3(void) { |
| 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 'const int[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 |
|
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 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 |
|
| 47 |
void testI_(I i1, I i2) { |
| 48 |
i1 = i2; // expected-error {{cannot assign to variable 'i1' with const-qualified data member 'a'}} |
| 49 |
} |
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 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 |
}; |
| 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 |
|
| 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 |
|
| 93 |
void testN(struct N1 *l) { |
| 94 |
*l = 0; // expected-error {{assigning to 'struct N1' from incompatible type 'int'}} |
| 95 |
} |