Debian Codegraph @

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

raw
1 // test that contracts are matched on friend decls when the type is complete
2 // { dg-do compile }
3 // { dg-options "-std=c++2a -fcontracts" }
4
5 struct T;
6
7 int both(int x, T *t) [[ pre: x > 0 ]] { return 0; }
8 int both2(int x, T *t) [[ pre: x > 0 ]];
9
10 template<typename Z>
11 int fn(int x, Z *z) [[ pre: x > 0 ]];
12
13 template<typename Z>
14 int fn2(int x, Z *z);
15
16 template<typename Z>
17 int fn3(int x, Z *z) [[ pre: x > 0 ]];
18
19 template<>
20 int fn3<T>(int x, T *z) [[ pre: x > 1 ]];
21
22 struct T
23 {
24 friend int both2(int x, T *t) [[ pre: x > 1 ]] // { dg-error "mismatched" }
25 {
26 return 0;
27 }
28
29 friend int hidden(int x, T *t)
30 [[ pre: x > 1 ]] [[ pre: t->pri > 0 ]]
31 {
32 return x;
33 }
34
35 /* cannot define friend spec, so we never get to matching contracts
36 friend int fn<T>(int x, T *t)
37 [[ pre: t->pri > 0 ]] { return 0; } // error defining explicit spec friend
38 */
39
40 // bad, general contracts must match general
41 template<typename Z>
42 friend int fn(int x, Z *z)
43 [[ pre: z->pri > 1 ]] { return 0; } // { dg-error "mismatched" }
44
45 // fine, can add contracts
46 template<typename Z>
47 friend int fn2(int x, Z *z)
48 [[ pre: z->pri > 1 ]] { return 0; } // { dg-bogus "mismatched" }
49
50 /* cannot declare without definition, so dup friend can't occur:
51 friend int dup(int x, T *t)
52 [[ pre: t->pri > 0 ]]; // error non-defining friend with contracts
53 friend int dup(int x, T *t)
54 [[ pre: t->pri > 1 ]]; // error non-defining friend with contracts
55 */
56
57 int x{1};
58 private:
59 int pri{-10};
60 };
61
62 int hidden(int x, T *t)
63 [[ pre: x > 0 ]] [[ pre: t->pri > 1 ]]; // { dg-error "mismatched" }
64