| 1 |
// generic error tests for generalized contract redecls |
| 2 |
// we also test for the warning diagnostic for strict redecl |
| 3 |
// { dg-do compile } |
| 4 |
// { dg-options "-std=c++2a -fcontracts -fcontract-strict-declarations=on" } |
| 5 |
|
| 6 |
// allowed to repeat contracts or omit them |
| 7 |
int g0(int a) [[ pre: a > 0 ]]; |
| 8 |
int g0(int a) [[ pre: a > 0 ]]; |
| 9 |
|
| 10 |
int g1(int a) [[ pre: a > 0 ]]; |
| 11 |
int g1(int a); |
| 12 |
|
| 13 |
// allowed to add from none if generalized redecl is on (by default) |
| 14 |
int g2(int a); |
| 15 |
int g2(int a) [[ pre: a > 0 ]]; // { dg-warning "adds contracts" } |
| 16 |
int g2(int a) [[ pre: a > 0 ]]; // { dg-bogus "adds contracts" } |
| 17 |
|
| 18 |
// can add to non-virtual methods |
| 19 |
struct G0 |
| 20 |
{ |
| 21 |
int f(int a); |
| 22 |
}; |
| 23 |
|
| 24 |
int G0::f(int a) [[ pre: a > 0 ]] // { dg-warning "adds contracts" } |
| 25 |
{ |
| 26 |
return -a; |
| 27 |
} |
| 28 |
|
| 29 |
struct G1 |
| 30 |
{ |
| 31 |
int f(int a); |
| 32 |
}; |
| 33 |
|
| 34 |
int G1::f(int a) [[ pre: a > 0 ]]; // { dg-warning "adds contracts" } |
| 35 |
// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 } |
| 36 |
|
| 37 |
int G1::f(int a); |
| 38 |
// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 } |
| 39 |
|
| 40 |
int G1::f(int a) [[ pre: a > 0 ]]; |
| 41 |
// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 } |
| 42 |
|
| 43 |
int G1::f(int a) |
| 44 |
{ |
| 45 |
return -a; |
| 46 |
} |
| 47 |
|
| 48 |
// allowed to redeclare even without contracts |
| 49 |
struct G2 |
| 50 |
{ |
| 51 |
int f(int a); |
| 52 |
}; |
| 53 |
|
| 54 |
int G2::f(int a); // { dg-warning "outside of class is not definition" } |
| 55 |
|
| 56 |
|
| 57 |
int f0(int a) [[ pre: a > 0 ]]; |
| 58 |
int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" } |
| 59 |
|
| 60 |
int f1(int a) [[ pre: a > 0 ]]; |
| 61 |
int f1(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" } |
| 62 |
|
| 63 |
int f2(int a) { return a; } |
| 64 |
int f2(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts after definition" } |
| 65 |
|
| 66 |
struct Base |
| 67 |
{ |
| 68 |
virtual int f(int a) [[ pre: a > 0 ]]; |
| 69 |
}; |
| 70 |
|
| 71 |
struct Child : Base |
| 72 |
{ |
| 73 |
int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" } |
| 74 |
}; |
| 75 |
|
| 76 |
// the initial decl of a guarded member must appear inside the class |
| 77 |
struct F2 |
| 78 |
{ |
| 79 |
int f(int a); |
| 80 |
}; |
| 81 |
|
| 82 |
int F2::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" } |
| 83 |
// FIXME if we move F2 down then a different error makes F2 undeclared |
| 84 |
|
| 85 |
struct F0 |
| 86 |
{ |
| 87 |
virtual int f(int a); |
| 88 |
}; |
| 89 |
|
| 90 |
int F0::f(int a); // { dg-error "declaration.*is not definition" } |
| 91 |
|
| 92 |
struct F1 |
| 93 |
{ |
| 94 |
virtual int f(int a); |
| 95 |
}; |
| 96 |
|
| 97 |
int F1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" } |
| 98 |
{ |
| 99 |
return -a; |
| 100 |
} |
| 101 |
|