xref: /aosp_15_r20/external/clang/test/SemaCXX/default-assignment-operator.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li class Base { // expected-error {{cannot define the implicit copy assignment operator for 'Base', because non-static reference member 'ref' cannot use copy assignment operator}} \
4*67e74705SXin Li   // expected-warning{{class 'Base' does not declare any constructor to initialize its non-modifiable members}}
5*67e74705SXin Li   int &ref;  // expected-note {{declared here}} \
6*67e74705SXin Li   // expected-note{{reference member 'ref' will never be initialized}}
7*67e74705SXin Li };
8*67e74705SXin Li 
9*67e74705SXin Li class X  : Base {  // // expected-error {{cannot define the implicit copy assignment operator for 'X', because non-static const member 'cint' cannot use copy assignment operator}} \
10*67e74705SXin Li // expected-note{{assignment operator for 'Base' first required here}}
11*67e74705SXin Li public:
12*67e74705SXin Li   X();
13*67e74705SXin Li   const int cint;  // expected-note {{declared here}}
14*67e74705SXin Li };
15*67e74705SXin Li 
16*67e74705SXin Li struct Y  : X {
17*67e74705SXin Li   Y();
18*67e74705SXin Li   Y& operator=(const Y&);
19*67e74705SXin Li   Y& operator=(volatile Y&);
20*67e74705SXin Li   Y& operator=(const volatile Y&);
21*67e74705SXin Li   Y& operator=(Y&);
22*67e74705SXin Li };
23*67e74705SXin Li 
24*67e74705SXin Li class Z : Y {};
25*67e74705SXin Li 
26*67e74705SXin Li Z z1;
27*67e74705SXin Li Z z2;
28*67e74705SXin Li 
29*67e74705SXin Li // Test1
f(X x,const X cx)30*67e74705SXin Li void f(X x, const X cx) {
31*67e74705SXin Li   x = cx; // expected-note{{assignment operator for 'X' first required here}}
32*67e74705SXin Li   x = cx;
33*67e74705SXin Li   z1 = z2;
34*67e74705SXin Li }
35*67e74705SXin Li 
36*67e74705SXin Li // Test2
37*67e74705SXin Li class T {};
38*67e74705SXin Li T t1;
39*67e74705SXin Li T t2;
40*67e74705SXin Li 
g()41*67e74705SXin Li void g() {
42*67e74705SXin Li   t1 = t2;
43*67e74705SXin Li }
44*67e74705SXin Li 
45*67e74705SXin Li // Test3
46*67e74705SXin Li class V {
47*67e74705SXin Li public:
48*67e74705SXin Li   V();
49*67e74705SXin Li   V &operator = (V &b);
50*67e74705SXin Li };
51*67e74705SXin Li 
52*67e74705SXin Li class W : V {};
53*67e74705SXin Li W w1, w2;
54*67e74705SXin Li 
h()55*67e74705SXin Li void h() {
56*67e74705SXin Li   w1 = w2;
57*67e74705SXin Li }
58*67e74705SXin Li 
59*67e74705SXin Li // Test4
60*67e74705SXin Li 
61*67e74705SXin Li class B1 {
62*67e74705SXin Li public:
63*67e74705SXin Li   B1();
64*67e74705SXin Li   B1 &operator = (B1 b);
65*67e74705SXin Li };
66*67e74705SXin Li 
67*67e74705SXin Li class D1 : B1 {};
68*67e74705SXin Li D1 d1, d2;
69*67e74705SXin Li 
i()70*67e74705SXin Li void i() {
71*67e74705SXin Li   d1 = d2;
72*67e74705SXin Li }
73*67e74705SXin Li 
74*67e74705SXin Li // Test5
75*67e74705SXin Li 
76*67e74705SXin Li class E1 { // expected-error{{cannot define the implicit copy assignment operator for 'E1', because non-static const member 'a' cannot use copy assignment operator}}
77*67e74705SXin Li 
78*67e74705SXin Li public:
79*67e74705SXin Li   const int a; // expected-note{{declared here}}
E1()80*67e74705SXin Li   E1() : a(0) {}
81*67e74705SXin Li 
82*67e74705SXin Li };
83*67e74705SXin Li 
84*67e74705SXin Li E1 e1, e2;
85*67e74705SXin Li 
j()86*67e74705SXin Li void j() {
87*67e74705SXin Li   e1 = e2; // expected-note{{assignment operator for 'E1' first required here}}
88*67e74705SXin Li }
89*67e74705SXin Li 
90*67e74705SXin Li namespace ProtectedCheck {
91*67e74705SXin Li   struct X {
92*67e74705SXin Li   protected:
93*67e74705SXin Li     X &operator=(const X&); // expected-note{{declared protected here}}
94*67e74705SXin Li   };
95*67e74705SXin Li 
96*67e74705SXin Li   struct Y : public X { };
97*67e74705SXin Li 
f(Y y)98*67e74705SXin Li   void f(Y y) { y = y; }
99*67e74705SXin Li 
100*67e74705SXin Li   struct Z { // expected-error{{'operator=' is a protected member of 'ProtectedCheck::X'}}
101*67e74705SXin Li     X x;
102*67e74705SXin Li   };
103*67e74705SXin Li 
f(Z z)104*67e74705SXin Li   void f(Z z) { z = z; }  // expected-note{{implicit copy assignment operator}}
105*67e74705SXin Li 
106*67e74705SXin Li }
107*67e74705SXin Li 
108*67e74705SXin Li namespace MultiplePaths {
109*67e74705SXin Li   struct X0 {
110*67e74705SXin Li     X0 &operator=(const X0&);
111*67e74705SXin Li   };
112*67e74705SXin Li 
113*67e74705SXin Li   struct X1 : public virtual X0 { };
114*67e74705SXin Li 
115*67e74705SXin Li   struct X2 : X0, X1 { }; // expected-warning{{direct base 'MultiplePaths::X0' is inaccessible due to ambiguity:\n    struct MultiplePaths::X2 -> struct MultiplePaths::X0\n    struct MultiplePaths::X2 -> struct MultiplePaths::X1 -> struct MultiplePaths::X0}}
116*67e74705SXin Li 
f(X2 x2)117*67e74705SXin Li   void f(X2 x2) { x2 = x2; }
118*67e74705SXin Li }
119