Debian Codegraph @

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

raw
1 // generic error tests for generalized contract redecls
2 // { dg-do compile }
3 // { dg-options "-std=c++2a -fcontracts" }
4
5 // allowed to repeat contracts or omit them
6 int g0(int a) [[ pre: a > 0 ]];
7 int g0(int a) [[ pre: a > 0 ]];
8
9 int g1(int a) [[ pre: a > 0 ]];
10 int g1(int a);
11
12 // allowed to add from none if generalized redecl is on (by default)
13 int g2(int a);
14 int g2(int a) [[ pre: a > 0 ]];
15
16 // can add to non-virtual methods
17 struct G0
18 {
19 int f(int a);
20 };
21
22 int G0::f(int a) [[ pre: a > 0 ]]
23 {
24 return -a;
25 }
26
27 struct G1
28 {
29 int f(int a);
30 };
31
32 int G1::f(int a) [[ pre: a > 0 ]];
33
34 int G1::f(int a)
35 {
36 return -a;
37 }
38
39 // allowed to redeclare even without contracts
40 struct G2
41 {
42 int f(int a);
43 };
44
45 int G2::f(int a);
46
47
48 int f0(int a) [[ pre: a > 0 ]];
49 int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" }
50
51 int f1(int a) [[ pre: a > 0 ]];
52 int f1(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
53
54 int f2(int a) { return a; }
55 int f2(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts after definition" }
56
57 struct Base
58 {
59 virtual int f(int a) [[ pre: a > 0 ]];
60 };
61
62 struct Child : Base
63 {
64 int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
65 };
66
67 // the initial decl of a guarded member must appear inside the class
68 struct F2
69 {
70 int f(int a);
71 };
72
73 int F2::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" }
74 // FIXME if we move F2 down then a different error makes F2 undeclared
75
76 struct F0
77 {
78 virtual int f(int a);
79 };
80
81 int F0::f(int a); // { dg-error "declaration.*is not definition" }
82
83 struct F1
84 {
85 virtual int f(int a);
86 };
87
88 int F1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" }
89 {
90 return -a;
91 }
92
93 // cannot "re"declare members of a forward declared class
94 struct F2;
95 int F2::test(); // { dg-error "no declaration matches" }
96 int F2::test2() [[ pre: true ]]; // { dg-error "no declaration matches" }
97
98 // can only redeclare member functions
99 struct F3
100 {
101 int x;
102 typedef int my_int;
103
104 struct Inner0;
105 struct Inner1;
106 enum my_enum0; // { dg-error "use of enum.*without previous decl" }
107 enum my_enum1 { E1, E2 };
108
109 int test0();
110 int test1();
111 int test2();
112 };
113
114 int F3::x{-1}; // { dg-error "is not a static data member" }
115 typedef double F3::my_int; // { dg-error "typedef name may not be a nested-name-specifier" }
116 struct F3::Inner0; // { dg-warning "declaration.*does not declare anything" }
117
118 struct F3::Inner1 { };
119
120 enum F3::my_enum1 { E0, E1, END }; // { dg-error "multiple definition" }
121
122 struct F4
123 {
124 int test0();
125
126 int F3::test0() [[ pre: true ]]; // { dg-error "cannot declare member function" }
127 friend int F3::test1();
128 friend int F3::test2();
129 };
130 int F3::test2() [[ pre: true ]] { return -1; }
131
132 void dummy0()
133 {
134 int F4::test0() [[ pre: true ]]; // { dg-error "qualified-id in declaration" }
135 }
136
137 namespace ns0
138 {
139 typedef int value;
140 struct X
141 {
142 int test1(value);
143 typedef double value;
144 int test2(value);
145 };
146 int X::test1(value); // { dg-error "no declaration matches" }
147 int X::test2(value);
148 }
149