xref: /llvm-project/clang/test/CXX/drs/cwg9xx.cpp (revision 14ba3f9d07ea1664497c5d117120fb243ca221aa)
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
8 
9 namespace std {
10   __extension__ typedef __SIZE_TYPE__ size_t;
11 
12   template<typename T> struct initializer_list {
13     const T *p; size_t n;
14     initializer_list(const T *p, size_t n);
15   };
16 } // namespace std
17 
18 namespace cwg930 { // cwg930: 2.7
19 #if __cplusplus >= 201103L
20 static_assert(alignof(int[]) == alignof(int), "");
21 static_assert(alignof(int[][2]) == alignof(int[2]), "");
22 #endif
23 } // namespace cwg930
24 
25 namespace cwg948 { // cwg948: 3.7
26 #if __cplusplus >= 201103L
27   class A {
28   public:
29      constexpr A(int v) : v(v) { }
30      constexpr operator int() const { return v; }
31   private:
32      int v;
33   };
34 
35   constexpr int id(int x)
36   {
37     return x;
38   }
39 
40   void f() {
41      if (constexpr int i = id(101)) { }
42      switch (constexpr int i = id(2)) { default: break; case 2: break; }
43      for (; constexpr int i = id(0); ) { }
44      while (constexpr int i = id(0)) { }
45 
46      if (constexpr A i = 101) { }
47      switch (constexpr A i = 2) { default: break; case 2: break; }
48      for (; constexpr A i = 0; ) { }
49      while (constexpr A i = 0) { }
50   }
51 #endif
52 } // namespace cwg948
53 
54 namespace cwg952 { // cwg952: 2.8
55 namespace example1 {
56 struct A {
57   typedef int I; // #cwg952-I
58 };
59 struct B : private A { // #cwg952-B
60 };
61 struct C : B {
62   void f() {
63     I i1;
64     // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
65     //   expected-note@#cwg952-B {{constrained by private inheritance here}}
66     //   expected-note@#cwg952-I {{member is declared here}}
67   }
68   I i2;
69   // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
70   //   expected-note@#cwg952-B {{constrained by private inheritance here}}
71   //   expected-note@#cwg952-I {{member is declared here}}
72   struct D {
73     I i3;
74     // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
75     //   expected-note@#cwg952-B {{constrained by private inheritance here}}
76     //   expected-note@#cwg952-I {{member is declared here}}
77     void g() {
78       I i4;
79       // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
80       //   expected-note@#cwg952-B {{constrained by private inheritance here}}
81       //   expected-note@#cwg952-I {{member is declared here}}
82     }
83   };
84 };
85 } // namespace example1
86 namespace example2 {
87 struct A {
88 protected:
89   static int x;
90 };
91 struct B : A {
92   friend int get(B) { return x; }
93 };
94 } // namespace example2
95 } // namespace cwg952
96 
97 namespace cwg960 { // cwg960: 3.0
98 struct a {};
99 class A {
100 #if __cplusplus >= 201103L
101   // Check lvalue ref vs rvalue ref vs pointer.
102   virtual a& rvalue_ref();
103   virtual a&& lvalue_ref();
104   virtual a& rvalue_vs_lvalue_ref(); // #cwg960-A-rvalue_vs_lvalue_ref
105   virtual a&& lvalue_vs_rvalue_ref(); // #cwg960-A-lvalue_vs_rvalue_ref
106   virtual a& rvalue_ref_vs_pointer(); // #cwg960-A-rvalue_ref_vs_pointer
107   virtual a* pointer_vs_rvalue_ref(); // #cwg960-A-pointer_vs_rvalue_ref
108   virtual a&& lvalue_ref_vs_pointer(); // #cwg960-A-lvalue_ref_vs_pointer
109   virtual a* pointer_vs_lvalue_ref(); // #cwg960-A-pointer_vs_lvalue_ref
110 #endif
111 };
112 
113 class B : A {
114 #if __cplusplus >= 201103L
115   // Check lvalue ref vs rvalue ref vs pointer.
116   a& rvalue_ref() override;
117   a&& lvalue_ref() override;
118 
119   a&& rvalue_vs_lvalue_ref() override;
120   // since-cxx11-error@-1 {{virtual function 'rvalue_vs_lvalue_ref' has a different return type ('a &&') than the function it overrides (which has return type 'a &')}}
121   //   since-cxx11-note@#cwg960-A-rvalue_vs_lvalue_ref {{overridden virtual function is here}}
122 
123   a& lvalue_vs_rvalue_ref() override;
124   // since-cxx11-error@-1 {{virtual function 'lvalue_vs_rvalue_ref' has a different return type ('a &') than the function it overrides (which has return type 'a &&')}}
125   //   since-cxx11-note@#cwg960-A-lvalue_vs_rvalue_ref {{overridden virtual function is here}}
126 
127   a* rvalue_ref_vs_pointer() override;
128   // since-cxx11-error@-1 {{virtual function 'rvalue_ref_vs_pointer' has a different return type ('a *') than the function it overrides (which has return type 'a &')}}
129   //   since-cxx11-note@#cwg960-A-rvalue_ref_vs_pointer {{overridden virtual function is here}}
130 
131   a& pointer_vs_rvalue_ref() override;
132   // since-cxx11-error@-1 {{virtual function 'pointer_vs_rvalue_ref' has a different return type ('a &') than the function it overrides (which has return type 'a *')}}
133   //   since-cxx11-note@#cwg960-A-pointer_vs_rvalue_ref {{overridden virtual function is here}}
134 
135   a* lvalue_ref_vs_pointer() override;
136   // since-cxx11-error@-1 {{virtual function 'lvalue_ref_vs_pointer' has a different return type ('a *') than the function it overrides (which has return type 'a &&')}}
137   //   since-cxx11-note@#cwg960-A-lvalue_ref_vs_pointer {{overridden virtual function is here}}
138 
139   a&& pointer_vs_lvalue_ref() override;
140   // since-cxx11-error@-1 {{virtual function 'pointer_vs_lvalue_ref' has a different return type ('a &&') than the function it overrides (which has return type 'a *')}}
141   //   since-cxx11-note@#cwg960-A-pointer_vs_lvalue_ref {{overridden virtual function is here}}
142 #endif
143 };
144 
145 } // namespace cwg960
146 
147 namespace cwg974 { // cwg974: 3.3
148 #if __cplusplus >= 201103L
149   void test() {
150     auto lam = [](int x = 42) { return x; };
151   }
152 #endif
153 } // namespace cwg974
154 
155 namespace cwg977 { // cwg977: 2.7
156 enum E { e = E() }; // #cwg977-E
157 #if !defined(_WIN32) || defined(__MINGW32__)
158 // expected-error@#cwg977-E {{invalid use of incomplete type 'E'}}
159 //   expected-note@#cwg977-E {{definition of 'cwg977::E' is not complete until the closing '}'}}
160 #endif
161 #if __cplusplus >= 201103L
162 enum E2 : int { e2 = E2() };
163 enum struct E3 { e = static_cast<int>(E3()) };
164 enum struct E4 : int { e = static_cast<int>(E4()) };
165 #endif
166 } // namespace cwg977
167 
168 namespace cwg990 { // cwg990: 3.5
169 #if __cplusplus >= 201103L
170   struct A { // #cwg990-A
171     A(std::initializer_list<int>); // #cwg990-A-init-list
172   };
173   struct B {
174     A a;
175   };
176   B b1 { };
177   B b2 { 1 };
178   // since-cxx11-error@-1 {{no viable conversion from 'int' to 'A'}}
179   //   since-cxx11-note@#cwg990-A {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const A &' for 1st argument}}
180   //   since-cxx11-note@#cwg990-A {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'A &&' for 1st argument}}
181   //   since-cxx11-note@#cwg990-A-init-list {{candidate constructor not viable: no known conversion from 'int' to 'std::initializer_list<int>' for 1st argument}}
182   B b3 { { 1 } };
183 
184   struct C {
185     C();
186     C(int);
187     C(std::initializer_list<int>) = delete; // #cwg990-deleted
188   };
189   C c1[3] { 1 }; // ok
190   C c2[3] { 1, {2} };
191   // since-cxx11-error@-1 {{call to deleted constructor of 'C'}}
192   //   since-cxx11-note@#cwg990-deleted {{'C' has been explicitly marked deleted here}}
193 
194   struct D {
195     D();
196     D(std::initializer_list<int>);
197     D(std::initializer_list<double>);
198   };
199   D d{};
200 #endif
201 } // namespace cwg990
202