1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s 2 3 const int global = 5; // expected-note{{variable 'global' declared const here}} 4 void test1() { 5 global = 2; // expected-error{{cannot assign to variable 'global' with const-qualified type 'const int'}} 6 } 7 8 void test2 () { 9 const int local = 5; // expected-note{{variable 'local' declared const here}} 10 local = 0; // expected-error{{cannot assign to variable 'local' with const-qualified type 'const int'}} 11 } 12 13 void test2 (const int parameter) { // expected-note{{variable 'parameter' declared const here}} 14 parameter = 2; // expected-error{{cannot assign to variable 'parameter' with const-qualified type 'const int'}} 15 } 16 17 class test3 { 18 int field; 19 const int const_field = 1; // expected-note 2{{non-static data member 'const_field' declared const here}} 20 static const int static_const_field = 1; // expected-note 2{{variable 'static_const_field' declared const here}} 21 void test() { 22 const_field = 4; // expected-error{{cannot assign to non-static data member 'const_field' with const-qualified type 'const int'}} 23 static_const_field = 4; // expected-error{{cannot assign to variable 'static_const_field' with const-qualified type 'const int'}} 24 } 25 void test_const() const { // expected-note 2{{member function 'test3::test_const' is declared const here}} 26 field = 4; // expected-error{{cannot assign to non-static data member within const member function 'test_const'}} 27 const_field = 4 ; // expected-error{{cannot assign to non-static data member 'const_field' with const-qualified type 'const int'}} 28 static_const_field = 4; // expected-error{{cannot assign to variable 'static_const_field' with const-qualified type 'const int'}} 29 } 30 }; 31 32 const int &return_const_ref(); // expected-note{{function 'return_const_ref' which returns const-qualified type 'const int &' declared here}} 33 34 void test4() { 35 return_const_ref() = 10; // expected-error{{cannot assign to return value because function 'return_const_ref' returns a const value}} 36 } 37 38 struct S5 { 39 int field; 40 const int const_field = 4; // expected-note {{non-static data member 'const_field' declared const here}} 41 }; 42 43 void test5() { 44 S5 s5; 45 s5.field = 5; 46 s5.const_field = 5; // expected-error{{cannot assign to non-static data member 'const_field' with const-qualified type 'const int'}} 47 } 48 49 struct U1 { 50 int a = 5; 51 }; 52 53 struct U2 { 54 U1 u1; 55 }; 56 57 struct U3 { 58 const U2 u2 = U2(); // expected-note{{non-static data member 'u2' declared const here}} 59 }; 60 61 struct U4 { 62 U3 u3; 63 }; 64 65 void test6() { 66 U4 u4; 67 u4.u3.u2.u1.a = 5; // expected-error{{cannot assign to non-static data member 'u2' with const-qualified type 'const U2'}} 68 } 69 70 struct A { 71 int z; 72 }; 73 struct B { 74 A a; 75 }; 76 struct C { 77 B b; 78 C(); 79 }; 80 const C &getc(); // expected-note{{function 'getc' which returns const-qualified type 'const C &' declared here}} 81 void test7() { 82 const C c; // expected-note{{variable 'c' declared const here}} 83 c.b.a.z = 5; // expected-error{{cannot assign to variable 'c' with const-qualified type 'const C'}} 84 85 getc().b.a.z = 5; // expected-error{{cannot assign to return value because function 'getc' returns a const value}} 86 } 87 88 struct D { const int n; }; // expected-note 2{{non-static data member 'n' declared const here}} 89 struct E { D *const d = 0; }; 90 void test8() { 91 extern D *const d; 92 d->n = 0; // expected-error{{cannot assign to non-static data member 'n' with const-qualified type 'const int'}} 93 94 E e; 95 e.d->n = 0; // expected-error{{cannot assign to non-static data member 'n' with const-qualified type 'const int'}} 96 } 97 98 struct F { int n; }; 99 struct G { const F *f; }; // expected-note{{non-static data member 'f' declared const here}} 100 void test10() { 101 const F *f; // expected-note{{variable 'f' declared const here}} 102 f->n = 0; // expected-error{{cannot assign to variable 'f' with const-qualified type 'const F *'}} 103 104 G g; 105 g.f->n = 0; // expected-error{{cannot assign to non-static data member 'f' with const-qualified type 'const F *'}} 106 } 107 108 void test11( 109 const int x, // expected-note{{variable 'x' declared const here}} 110 const int& y // expected-note{{variable 'y' declared const here}} 111 ) { 112 x = 5; // expected-error{{cannot assign to variable 'x' with const-qualified type 'const int'}} 113 y = 5; // expected-error{{cannot assign to variable 'y' with const-qualified type 'const int &'}} 114 } 115 116 struct H { 117 const int a = 0; // expected-note{{non-static data member 'a' declared const here}} 118 const int &b = a; // expected-note{{non-static data member 'b' declared const here}} 119 }; 120 121 void test12(H h) { 122 h.a = 1; // expected-error {{cannot assign to non-static data member 'a' with const-qualified type 'const int'}} 123 h.b = 2; // expected-error {{cannot assign to non-static data member 'b' with const-qualified type 'const int &'}} 124 } 125 126 void test() { 127 typedef const int &Func(); 128 129 Func &bar(); 130 bar()() = 0; // expected-error {{read-only variable is not assignable}} 131 } 132