Debian Codegraph @

clang/test/SemaCXX/cxx23-invalid-constexpr.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected -std=c++23 %s
2
3 // This test covers modifications made by P2448R2.
4
5 // Check that there is no error when a constexpr function that never produces a
6 // constant expression, but still an error if such function is called from
7 // constexpr context.
8 constexpr int F(int N) {
9 double D = 2.0 / 0.0; // expected-note {{division by zero}}
10 return 1;
11 }
12
13 constexpr int F0(int N) {
14 if (N == 0)
15 double d2 = 2.0 / 0.0; // expected-note {{division by zero}}
16 return 1;
17 }
18
19 template <typename T>
20 constexpr int FT(T N) {
21 double D = 2.0 / 0.0; // expected-note {{division by zero}}
22 return 1;
23 }
24
25 class NonLiteral { // expected-note {{'NonLiteral' is not literal because it is not an aggregate and has no constexpr constructors}}
26 public:
27 NonLiteral() {}
28 ~NonLiteral() {}
29 };
30
31 constexpr NonLiteral F1() {
32 return NonLiteral{};
33 }
34
35 constexpr int F2(NonLiteral N) {
36 return 8;
37 }
38
39 class Derived : public NonLiteral {
40 constexpr ~Derived() {};
41 };
42
43 class Derived1 : public NonLiteral {
44 constexpr Derived1() : NonLiteral () {}
45 };
46
47
48 struct X {
49 X();
50 X(const X&);
51 X(X&&);
52 X& operator=(X&);
53 X& operator=(X&& other);
54 bool operator==(X const&) const;
55 };
56
57 template <typename T>
58 struct Wrapper {
59 constexpr Wrapper() = default;
60 constexpr Wrapper(Wrapper const&) = default;
61 constexpr Wrapper(T const& t) : t(t) { }
62 constexpr Wrapper(Wrapper &&) = default;
63 constexpr X get() const { return t; }
64 constexpr bool operator==(Wrapper const&) const = default;
65 private:
66 T t;
67 };
68
69 struct WrapperNonT {
70 constexpr WrapperNonT() = default;
71 constexpr WrapperNonT(WrapperNonT const&) = default;
72 constexpr WrapperNonT(X const& t) : t(t) { }
73 constexpr WrapperNonT(WrapperNonT &&) = default;
74 constexpr WrapperNonT& operator=(WrapperNonT &) = default;
75 constexpr WrapperNonT& operator=(WrapperNonT&& other) = default;
76 constexpr X get() const { return t; }
77 constexpr bool operator==(WrapperNonT const&) const = default;
78 private:
79 X t;
80 };
81
82 struct NonDefaultMembers {
83 constexpr NonDefaultMembers() {}; // expected-note {{non-literal type 'X' cannot be used in a constant expression}}
84 constexpr NonDefaultMembers(NonDefaultMembers const&) {};
85 constexpr NonDefaultMembers(NonDefaultMembers &&) {};
86 constexpr NonDefaultMembers& operator=(NonDefaultMembers &other) {this->t = other.t; return *this;}
87 constexpr NonDefaultMembers& operator=(NonDefaultMembers&& other) {this->t = other.t; return *this;}
88 constexpr bool operator==(NonDefaultMembers const& other) const {return this->t == other.t;}
89 X t;
90 };
91
92 int Glob = 0;
93 class C1 {
94 public:
95 constexpr C1() : D(Glob) {};
96 private:
97 int D;
98 };
99
100 void test() {
101
102 constexpr int A = F(3); // expected-error {{constexpr variable 'A' must be initialized by a constant expression}}
103 // expected-note@-1 {{in call}}
104 F(3);
105 constexpr int B = F0(0); // expected-error {{constexpr variable 'B' must be initialized by a constant expression}}
106 // expected-note@-1 {{in call}}
107 F0(0);
108 constexpr auto C = F1(); // expected-error {{constexpr variable cannot have non-literal type 'const NonLiteral'}}
109 F1();
110 NonLiteral L;
111 constexpr auto D = F2(L); // expected-error {{constexpr variable 'D' must be initialized by a constant expression}}
112 // expected-note@-1 {{non-literal type 'NonLiteral' cannot be used in a constant expression}}
113
114 constexpr auto E = FT(1); // expected-error {{constexpr variable 'E' must be initialized by a constant expression}}
115 // expected-note@-1 {{in call}}
116 F2(L);
117
118 Wrapper<X> x;
119 WrapperNonT x1;
120 NonDefaultMembers x2;
121
122 // TODO these produce notes with an invalid source location.
123 // static_assert((Wrapper<X>(), true));
124 // static_assert((WrapperNonT(), true),"");
125
126 static_assert((NonDefaultMembers(), true),""); // expected-error{{expression is not an integral constant expression}} \
127 // expected-note {{in call to}}
128 constexpr bool FFF = (NonDefaultMembers() == NonDefaultMembers()); // expected-error{{must be initialized by a constant expression}} \
129 // expected-note{{non-literal}}
130 }
131
132 struct A {
133 A ();
134 ~A();
135 };
136
137 template <class T>
138 struct opt
139 {
140 union {
141 char c;
142 T data;
143 };
144
145 constexpr opt() {}
146
147 constexpr ~opt() {
148 if (engaged)
149 data.~T();
150 }
151
152 bool engaged = false;
153 };
154
155 consteval void foo() {
156 opt<A> a;
157 }
158
159 void bar() { foo(); }