Debian Codegraph @

gcc/testsuite/g++.dg/contracts/contracts-constexpr1.C

raw
1 // ensure that passing pre/post do not affect constexpr functions
2 // ensure that failing pre/post generate an error at runtime in constexpr funcs
3 // { dg-do run }
4 // { dg-options "-std=c++20 -fcontracts -fcontract-continuation-mode=on" }
5
6 constexpr int wfun(int a)
7 [[ pre: a > 0 ]]
8 [[ post r: r > 0 ]]
9 {
10 return a;
11 }
12
13 constexpr int ffun(int a)
14 [[ pre: a > 0 ]]
15 [[ post r: r > 0 ]]
16 {
17 return a;
18 }
19
20 template<typename T>
21 constexpr int tfun(T a)
22 [[ pre: a > 0 ]]
23 [[ post r: r > 0 ]]
24 {
25 return a;
26 }
27
28 template<typename T>
29 constexpr int wtfun(T a)
30 [[ pre: a > 0 ]]
31 [[ post r: r > 0 ]]
32 {
33 return a;
34 }
35
36 template<typename T>
37 constexpr int ftfun(T a)
38 [[ pre: a > 0 ]]
39 [[ post r: r > 0 ]]
40 {
41 return a;
42 }
43
44 constexpr int explicitfn(int a)
45 [[ pre ignore: a > 0 ]]
46 [[ pre check_maybe_continue: a > 0 ]]
47 [[ post ignore r: r > 0 ]]
48 [[ post check_maybe_continue r: r > 0 ]]
49 {
50 return a;
51 }
52
53 int main(int, char **) {
54 constexpr int a = wfun(10);
55 int b = ffun(-10);
56 constexpr int c = wtfun(10);
57 int d = ftfun(-10);
58
59 int e = explicitfn(-10);
60
61 int z = ftfun(-10.0);
62
63 return 0;
64 }
65
66 // { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
67 // { dg-output "contract violation in function ffun at .*.C:14: .*(\n|\r\n|\r)" }
68 // { dg-output "contract violation in function ffun at .*.C:15: .*(\n|\r\n|\r)" }
69 // { dg-output "contract violation in function ftfun<int> at .*.C:38: .*(\n|\r\n|\r)" }
70 // { dg-output "contract violation in function ftfun<int> at .*.C:39: .*(\n|\r\n|\r)" }
71 // { dg-output "contract violation in function explicitfn at .*.C:46: .*(\n|\r\n|\r)" }
72 // { dg-output "contract violation in function explicitfn at .*.C:48: .*(\n|\r\n|\r)" }
73 // { dg-output "contract violation in function ftfun<double> at .*.C:38: .*(\n|\r\n|\r)" }
74 // { dg-output "contract violation in function ftfun<double> at .*.C:39: .*(\n|\r\n|\r)" }
75