Debian Codegraph @

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

raw
1 // Tests to ensure that deduced return types work with postconditions using
2 // the return value on defining declarations.
3 // { dg-do compile }
4 // { dg-options "-std=c++2a -fcontracts --param ggc-min-heapsize=0 --param ggc-min-expand=0" }
5
6 auto undeduced(int z)
7 {
8 if (!(sizeof(decltype(undeduced(5))) > 4)) // { dg-error "before deduction" }
9 return 5;
10 return 4;
11 }
12
13 // defining declaration, fine
14 auto g0(int a) [[ pre: a < 0 ]] [[ post r: r > 0 ]]
15 {
16 return -a * 1.2;
17 }
18
19 // non defining post using nondeduced identifier, fine
20 int g1(int m) [[ post r: r == m ]];
21
22 int g1(int n) [[ post s: s == n ]]
23 {
24 return -n;
25 }
26
27 int g2(int z)
28 [[ pre: sizeof(decltype(g2(5))) > 4 ]]; // { dg-error "not declared" }
29
30 int g3(int z)
31 [[ pre: sizeof(decltype(g2(5))) > 4 ]]
32 {
33 return -z;
34 }
35
36 // deduced that doesn't use return, good
37 auto g4(int m) [[ post: m ]];
38
39 auto g4(int m) [[ post: m ]]
40 {
41 return -m;
42 }
43
44 auto g5(int m) [[ pre: m ]];
45
46 auto g5(int m) [[ pre: m ]]
47 {
48 return -m;
49 }
50
51 template<typename T>
52 auto g6(T t) [[ post r: r == t ]];
53
54 template<typename S>
55 auto g6(S s) [[ post q: q == s ]]
56 {
57 return s;
58 }
59
60 template<typename T>
61 T g7(T t) [[ post r: r == t ]];
62
63 template<typename S>
64 S g7(S s) [[ post q: q == s ]]
65 {
66 return s;
67 }
68
69 template<typename T>
70 auto g8(T t) [[ post r: r == t && sizeof(decltype(::g8(t))) > 2 ]]; // { dg-error "not been declared" }
71
72 // This failure is related to the fact that we've invalidated the previous
73 // contract.
74 template<typename S>
75 auto g8(S s) [[ post q: q == s && sizeof(decltype(::g8(s))) > 2 ]] // { dg-error "mismatched" }
76 {
77 return s;
78 }
79
80 // non defining pre, bad
81 auto f0(int z)
82 [[ pre: sizeof(decltype(f0(5))) > 4 ]]; // { dg-error "not declared" }
83
84 // defining pre, still ill formed
85 auto f1(int z)
86 [[ pre: sizeof(decltype(f1(5))) > 4 ]] // { dg-error "not declared" }
87 {
88 return '5';
89 }
90
91 // undeduced using postcon, OK
92 auto f2(int m) [[ post r: r == m ]];
93
94 auto f2(int n) [[ post s: s == n ]]
95 {
96 return n;
97 }
98
99 template<typename T>
100 void f3(T t) [[ post r: false ]] // { dg-error "function does not return a value" }
101 {
102 }
103
104 int main(int, char**)
105 {
106 return 0;
107 }
108