summaryrefslogtreecommitdiff
path: root/Test/dafny0/ModifyStmt.dfy
blob: 00b39e7a1375dd7787e33514c8b0193af6dfedac (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

class MyClass {
  var x: int;
  var y: int;
  ghost var g: int;

  method M(S: set<MyClass>, mc: MyClass)
    requires this == mc;
    modifies this, S;
  {
    if mc == this {
      modify mc, this;
    }
    modify {this}, this, {this}, S;
    modify this;
    modify {:myAttribute} {:another true} {this}, this, S;
    modify {} ;
  }

  method N()
    modifies this;
  {
    x := 18;
    modify this;
    assert x == 18;  // error: cannot conclude this here
  }

  method P(other: MyClass)
    modifies this;
  {
    x := 18;
    var previous := if other == null then 0 else other.x;
    modify this;
    assert other != null && other != this ==> other.x == previous;
  }

  method FieldSpecific0()
    modifies `x;
  {
    modify this;  // error: this is a larger frame than what FieldSpecific0 is allowed to modify
  }

  method FieldSpecific1()
    modifies `x, `y;
  {
    modify this;  // error: this is a larger frame than what FieldSpecific0 is allowed to modify
  }

  method FieldSpecific2()
    modifies this;
  {
    modify `x;
  }

  method FieldSpecific3()
    modifies `x;
  {
    modify this`x;
    modify `y;  // error: this is a larger frame than what FieldSpecific3 is allowed to modify
  }

  method FieldSpecific4()
    modifies `x;
  {
    var xx, yy := x, y;
    modify this`x;
    assert y == yy;  // fine
    assert x == xx;  // error: x just changed
  }

  method GhostFrame4()
    modifies this;
  {
    var xx := x;
    ghost var gg := g;
    if g < 100 {
      // we're now in a ghost context
      var n := 0;
      while n < y
        modifies this;
      {
        g := g + 1;
        n := n + 1;
      }
    }
    assert x == xx;  // fine, since only ghost state is allowed to be modified inside the ghost if
    assert g == gg;  // error
  }

  ghost method Ghost0()
    modifies this;
  {
    var xx := x;
    var gg := g;
    modify this;  // since we're in the context of a ghost method, this will only modify ghost fields
    assert x == xx;  // fine
    assert g == gg;  // error
  }

  ghost method Ghost1()
    modifies this;
  {
    var xx := x;
    var gg := g;
    modify `g;  // since we're in the context of a ghost method, this will only modify
                // ghost fields, despite the specific mention of non-ghost fields
    assert x == xx;  // fine
    assert g == gg;  // error
  }

  method Ghost2()
    modifies this;
  {
    var xx := x;
    ghost var gg := g;
    if g < 100 {
      // the if guard mentions a ghost field, so we're now in a ghost context
      modify this;
      assert x == xx;  // fine, since the 'modify' statement only modified ghost fields
      assert g == gg;  // error: the 'modify' statement can affect the ghost field 'g'
    }
  }
}

class ModifyBody {
  var x: int;
  var y: int;
  method M0()
    modifies this;
  {
    modify {} {
      x := 3;  // error: violates modifies clause of the modify statement
    }
  }
  method M1()
    modifies this;
  {
    modify {} {
      var o := new ModifyBody;
      o.x := 3;  // fine
    }
  }
  method M2()
    modifies this;
  {
    modify this {
      x := 3;
    }
  }
  method M3(o: ModifyBody, p: ModifyBody)
    modifies this, o, p;
  {
    modify {this, o, p} {
      modify this, o;
      modify o, this {
        modify o {
          modify o;
          modify {} {
            modify {};
          }
        }
      }
    }
  }
  method P0()
    modifies this;
  {
   var xx := x;
    modify this;
    assert xx == x;  // error
  }
  method P1()
    modifies this;
  {
   var xx := x;
    modify this {
      y := 3;
    }
    assert xx == x;  // fine, because the modify body trumps the modify frame
  }
}