Debian Codegraph @

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

raw
1 // generic error tests for contract redecls with generalized redecl
2 // { dg-do compile }
3 // { dg-options "-std=c++2a -fcontracts" }
4
5 // OK if equivalent -- even through renames.
6 int g0(int a) [[ pre: a > 0 ]];
7 int g0(int a) [[ pre: a > 0 ]];
8
9 int g0b(int a) [[ pre: a > 0 ]];
10 int g0b(int b) [[ pre: b > 0 ]];
11 int g0b(int c) [[ pre: c > 0 ]]
12 {
13 return 0;
14 }
15
16 // OK if specified before.
17 int g1(int a) [[ pre: a > 0 ]];
18 int g1(int a);
19
20 // OK if specified after.
21 int g2(int a);
22 int g2(int a) [[ pre: a > 0 ]];
23
24 int g2b(int a);
25 int g2b(int b) [[ pre: b > 0 ]];
26
27 // can add to non-virtual methods
28 struct G0
29 {
30 int f(int a);
31 };
32
33 // OK to add contracts at the point of definition.
34 int G0::f(int a) [[ pre: a > 0 ]]
35 {
36 return -a;
37 }
38
39 struct G1
40 {
41 int f1(int a);
42 };
43
44 // OK to redeclare functions and add constraints...
45 int G1::f1(int a) [[ pre: a > 0 ]];
46
47 // ...and leave them off later.
48 int G1::f1(int a)
49 {
50 return -a;
51 }
52
53 int f0(int a) [[ pre: a > 0 ]];
54 int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" }
55
56 int f1(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]];
57 int f1(int a) [[ pre: a > 0 ]]; // { dg-error "different number of contracts" }
58
59 int f2(int a) [[ pre: a > 0 ]];
60 int f2(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
61
62 int f3(int a) { return a; }
63 int f3(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts" }
64
65 struct Base
66 {
67 virtual int f(int a) [[ pre: a > 0 ]];
68 };
69
70 struct Child : Base
71 {
72 int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
73 };
74
75 struct S1
76 {
77 virtual int f(int a); // contracts are inherited at the point of declarations
78 };
79
80 int S1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" }
81 {
82 return -a;
83 }
84
85 struct S2
86 {
87 int f() { return 0; }
88 };
89
90 int S2::f(); // OK?
91
92
93 struct S3
94 {
95 int f() { return 0; }
96 };
97
98 int S3::f() [[pre: true]]; // { dg-error "cannot add contracts" }
99
100
101 // The initial decl of a guarded member must appear inside the class.
102 struct S4
103 {
104 int f(int a);
105 };
106
107 int S4::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" }
108
109
110 struct S5
111 {
112 template<typename T>
113 S5(T a);
114 };
115
116 template<typename T>
117 S5::S5(T a) [[ pre: a > 0 ]]
118 {
119 }
120
121 struct S6
122 {
123 template<typename T>
124 S6(T a);
125 };
126
127 template<typename T>
128 S6::S6(T a) [[ pre: a > 0 ]];
129
130 template<typename T>
131 S6::S6(T a)
132 {
133 }
134
135 int p0(int n)
136 [[ post r: r > 0 && r == n ]]
137 [[ post r: r > 1 && r == n ]]
138 [[ post r: r > 2 && r == n ]]
139 [[ post r: r > 3 && r == n ]];
140
141 int p0(int z)
142 [[ post r: r > 0 && r == z ]]
143 [[ post r1: r1 > 1 && r1 == z ]]
144 [[ post r2: r2 > 2 && r2 == z ]]
145 [[ post r3: r3 > 3 && r3 == z ]]
146 {
147 return z;
148 }
149