Debian Codegraph @

clang/test/SemaCXX/cxx23-assume.cpp

raw
1 // RUN: %clang_cc1 -std=c++23 -x c++ %s -verify
2 // RUN: %clang_cc1 -std=c++20 -pedantic -x c++ %s -verify=ext,expected
3 // RUN: %clang_cc1 -std=c++23 -x c++ %s -verify -fexperimental-new-constant-interpreter
4 // RUN: %clang_cc1 -std=c++20 -pedantic -x c++ %s -verify=ext,expected -fexperimental-new-constant-interpreter
5
6 struct A{};
7 struct B{ explicit operator bool() { return true; } };
8
9 template <bool cond>
10 void f() {
11 [[assume(cond)]]; // ext-warning {{C++23 extension}}
12 }
13
14 template <bool cond>
15 struct S {
16 void f() {
17 [[assume(cond)]]; // ext-warning {{C++23 extension}}
18 }
19
20 template <typename T>
21 constexpr bool g() {
22 [[assume(cond == sizeof(T))]]; // expected-note {{assumption evaluated to false}} ext-warning {{C++23 extension}}
23 return true;
24 }
25 };
26
27 bool f2();
28
29 template <typename T>
30 constexpr void f3() {
31 [[assume(T{})]]; // expected-error {{not contextually convertible to 'bool'}} expected-warning {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
32 }
33
34 void g(int x) {
35 f<true>();
36 f<false>();
37 S<true>{}.f();
38 S<false>{}.f();
39 S<true>{}.g<char>();
40 S<true>{}.g<int>();
41 [[assume(f2())]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
42
43 [[assume((x = 3))]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
44 [[assume(x++)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
45 [[assume(++x)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
46 [[assume([]{ return true; }())]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
47 [[assume(B{})]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
48 [[assume((1, 2))]]; // expected-warning {{has no effect}} // ext-warning {{C++23 extension}}
49
50 f3<A>(); // expected-note {{in instantiation of}}
51 f3<B>(); // expected-note {{in instantiation of}}
52 [[assume]]; // expected-error {{takes one argument}}
53 [[assume(z)]]; // expected-error {{undeclared identifier}}
54 [[assume(A{})]]; // expected-error {{not contextually convertible to 'bool'}}
55 [[assume(true)]] if (true) {} // expected-error {{only applies to empty statements}}
56 [[assume(true)]] {} // expected-error {{only applies to empty statements}}
57 [[assume(true)]] for (;false;) {} // expected-error {{only applies to empty statements}}
58 [[assume(true)]] while (false) {} // expected-error {{only applies to empty statements}}
59 [[assume(true)]] label:; // expected-error {{cannot be applied to a declaration}}
60 [[assume(true)]] goto label; // expected-error {{only applies to empty statements}}
61
62 // Also check variant spellings.
63 __attribute__((__assume__(true))); // Should not issue a warning because it doesn't use the [[]] spelling.
64 __attribute__((assume(true))) {}; // expected-error {{only applies to empty statements}}
65 [[clang::assume(true)]] {}; // expected-error {{only applies to empty statements}}
66 }
67
68 // Check that 'x' is ODR-used here.
69 constexpr int h(int x) { return sizeof([=] { [[assume(x)]]; }); } // ext-warning {{C++23 extension}}
70 static_assert(h(4) == sizeof(int));
71
72 static_assert(__has_cpp_attribute(assume) == 202207L);
73 static_assert(__has_attribute(assume));
74
75 constexpr bool i() { // ext-error {{never produces a constant expression}}
76 [[assume(false)]]; // ext-note {{assumption evaluated to false}} expected-note {{assumption evaluated to false}} ext-warning {{C++23 extension}}
77 return true;
78 }
79
80 constexpr bool j(bool b) {
81 [[assume(b)]]; // expected-note {{assumption evaluated to false}} ext-warning {{C++23 extension}}
82 return true;
83 }
84
85 static_assert(i()); // expected-error {{not an integral constant expression}} expected-note {{in call to}}
86 static_assert(j(true));
87 static_assert(j(false)); // expected-error {{not an integral constant expression}} expected-note {{in call to}}
88 static_assert(S<true>{}.g<char>());
89 static_assert(S<false>{}.g<A>()); // expected-error {{not an integral constant expression}} expected-note {{in call to}}
90
91
92 template <typename T>
93 constexpr bool f4() {
94 [[assume(!T{})]]; // expected-error {{invalid argument type 'D'}} // expected-warning 2 {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
95 return sizeof(T) == sizeof(int);
96 }
97
98 template <typename T>
99 concept C = f4<T>(); // expected-note 3 {{in instantiation of}}
100 // expected-note@-1 3 {{while substituting}}
101 // expected-error@-2 2 {{resulted in a non-constant expression}}
102
103 struct D {
104 int x;
105 };
106
107 struct E {
108 int x;
109 constexpr explicit operator bool() { return false; }
110 };
111
112 struct F {
113 int x;
114 int y;
115 constexpr explicit operator bool() { return false; }
116 };
117
118 template <typename T>
119 constexpr int f5() requires C<T> { return 1; } // expected-note {{while checking the satisfaction}}
120 // expected-note@-1 {{while substituting template arguments}}
121 // expected-note@-2 {{candidate template ignored}}
122
123 template <typename T>
124 constexpr int f5() requires (!C<T>) { return 2; } // expected-note 4 {{while checking the satisfaction}}
125 // expected-note@-1 4 {{while substituting template arguments}}
126 // expected-note@-2 {{candidate template ignored}}
127
128 static_assert(f5<int>() == 1);
129 static_assert(f5<D>() == 1); // expected-note 3 {{while checking constraint satisfaction}}
130 // expected-note@-1 3 {{in instantiation of}}
131 // expected-error@-2 {{no matching function for call}}
132
133 static_assert(f5<double>() == 2);
134 static_assert(f5<E>() == 1); // expected-note {{while checking constraint satisfaction}} expected-note {{in instantiation of}}
135 static_assert(f5<F>() == 2); // expected-note {{while checking constraint satisfaction}} expected-note {{in instantiation of}}
136
137 // Do not validate assumptions whose evaluation would have side-effects.
138 constexpr int foo() {
139 int a = 0;
140 [[assume(a++)]] [[assume(++a)]]; // expected-warning 2 {{assumption is ignored because it contains (potential) side-effects}} ext-warning 2 {{C++23 extension}}
141 [[assume((a+=1))]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
142 return a;
143 }
144
145 static_assert(foo() == 0);
146
147 template <bool ...val>
148 void f() {
149 [[assume(val)]]; // expected-error {{expression contains unexpanded parameter pack}}
150 }
151
152 namespace gh71858 {
153 int
154 foo (int x, int y)
155 {
156 __attribute__((assume(x == 42)));
157 __attribute__((assume(++y == 43))); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
158 return x + y;
159 }
160 }
161
162 // Do not crash when assumptions are unreachable.
163 namespace gh106898 {
164 int foo () {
165 while(1);
166 int a = 0, b = 1;
167 __attribute__((assume (a < b)));
168 }
169 }