File: contracts-redecl5.C

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (101 lines) | stat: -rw-r--r-- 2,314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// generic error tests for generalized contract redecls
//   we also test for the warning diagnostic for strict redecl
// { dg-do compile }
// { dg-options "-std=c++2a -fcontracts -fcontract-strict-declarations=on" }

// allowed to repeat contracts or omit them
int g0(int a) [[ pre: a > 0 ]];
int g0(int a) [[ pre: a > 0 ]];

int g1(int a) [[ pre: a > 0 ]];
int g1(int a);

// allowed to add from none if generalized redecl is on (by default)
int g2(int a);
int g2(int a) [[ pre: a > 0 ]]; // { dg-warning "adds contracts" }
int g2(int a) [[ pre: a > 0 ]]; // { dg-bogus "adds contracts" }

// can add to non-virtual methods
struct G0
{
  int f(int a);
};

int G0::f(int a) [[ pre: a > 0 ]] // { dg-warning "adds contracts" }
{
  return -a;
}

struct G1
{
  int f(int a);
};

int G1::f(int a) [[ pre: a > 0 ]]; // { dg-warning "adds contracts" }
// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 }

int G1::f(int a);
// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 }

int G1::f(int a) [[ pre: a > 0 ]];
// { dg-warning "outside of class is not definition" "" { target *-*-* } .-1 }

int G1::f(int a)
{
  return -a;
}

// allowed to redeclare even without contracts
struct G2
{
  int f(int a);
};

int G2::f(int a); // { dg-warning "outside of class is not definition" }


int f0(int a) [[ pre: a > 0 ]];
int f0(int a) [[ pre: a > 0 ]] [[ pre: a > 10 ]]; // { dg-error "different number of contracts" }

int f1(int a) [[ pre: a > 0 ]];
int f1(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }

int f2(int a) { return a; }
int f2(int a) [[ pre: a < 0 ]]; // { dg-error "cannot add contracts after definition" }

struct Base
{
  virtual int f(int a) [[ pre: a > 0 ]];
};

struct Child : Base
{
  int f(int a) [[ pre: a < 0 ]]; // { dg-error "mismatched contract" }
};

// the initial decl of a guarded member must appear inside the class
struct F2
{
  int f(int a);
};

int F2::g(int a) [[ pre: a > 0 ]]; // { dg-error "no declaration matches" }
// FIXME if we move F2 down then a different error makes F2 undeclared

struct F0
{
  virtual int f(int a);
};

int F0::f(int a); // { dg-error "declaration.*is not definition" }

struct F1
{
  virtual int f(int a);
};

int F1::f(int a) [[ pre: a > 0 ]] // { dg-error "cannot add" }
{
  return -a;
}