forked from tact-lang/tact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantics.tact
1889 lines (1392 loc) · 60.1 KB
/
semantics.tact
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
struct SA {
a1: Int;
a2: SB;
}
struct SB {
b1: Bool;
b2: SC;
b3: Int;
}
struct SC {
c1: Int;
}
// Wrapper struct in order to pass maps to mutating functions. See issue #815.
// Also, it is useful for nesting maps inside maps.
struct MapWrapper {
m: map<Int,SA>
}
/**** Auxiliary functions for structs **********/
fun getNewStruct(s: SA): SA {
// Reassign the parameter value
// This does not change the struct passed as parameter to the function
s = SA {a1: 0, a2: SB {b1: false, b2: SC {c1: 0}, b3: 0}};
return s;
}
fun copyAndModifyStruct(s: SA): SA {
// Change some fields in the struct given as parameter
// Since structs are passed by value, this does not change the struct passed as parameter to the function
s.a1 += 100;
s.a2.b2.c1 += 1;
return s;
}
extends mutates fun reassignStruct(self: SA) {
// Reassigning the parameter will change the struct given as parameter
self = SA {a1: 0, a2: SB {b1: false, b2: SC {c1: 0}, b3: 0}};
}
extends mutates fun changeSomeFields(self: SA) {
// Change some fields in the struct given as parameter
// This will change the struct passed as parameter to the function
self.a1 += 100;
self.a2.b2.c1 += 1;
}
extends fun copyStruct(self: SA): SA {
// Since structs are passed by value, "self" will be a copy of the struct given as parameter
return self;
}
// Identity function, just to test chaining of mutating functions.
extends mutates fun reflectStruct(self: SA): SA {
return self;
}
extends mutates fun incrementIntFields(self: SA): SA {
self.a1 += 1;
self.a2.b2.c1 += 1;
self.a2.b3 += 1;
return self;
}
extends mutates fun flipBooleanFields(self: SA): SA {
self.a2.b1 = !self.a2.b1;
return self;
}
/**** Auxiliary functions for maps **********/
fun getNewMap(m: map<Int,SA>): map<Int,SA> {
// Reassign the parameter value
// This does not change the map passed as parameter to the function
m = emptyMap();
m.set(1, SA {a1: 0, a2: SB {b1: false, b2: SC {c1: 0}, b3: 0}});
return m;
}
fun copyAndModifyMap(m: map<Int,SA>): map<Int,SA> {
// Change some entries in the map given as parameter
// Since maps are passed by value, this does not change the map passed as parameter to the function
// Note that it is not possible to directly change the struct in entry 1 like
// m.get(1)!!.a2.b2.c1 += 40;
// because the compiler only allows path expressions on the left of assignments.
// So, we need to read m.get(1) and store it in a variable:
let c = m.get(1)!!;
// Then change the struct field here
c.a2.b2.c1 += 40;
// And then reassign entry 1
// If we do not reassign entry 1 in m, the change we did in the previous line
// to c will not be reflected in the map m, because struct c is a copy!!! (Recall that
// structs are assigned by value as well).
// To check the previous claim, this function will return an emptyMap()
// if m.get(1)!!.a2.b2.c1 == c.a2.b2.c1 at this point (which will not be true):
if (m.get(1)!!.a2.b2.c1 == c.a2.b2.c1) {
return emptyMap();
}
m.set(1, c);
// Now delete entries 2 and 3
m.del(2);
m.del(3);
return m;
}
/*
Currently, the compiler does not allow passing maps to extension (and hence mutating) functions
in their "self" argument.
One workaround is to wrap the map using a struct, since structs are allowed in extension functions
(see issue #815)
*/
extends mutates fun reassignMap(self: MapWrapper) {
// Reassigning the parameter will change the map given as parameter
self = MapWrapper {m: emptyMap()};
self.m.set(1, SA {a1: 0, a2: SB {b1: false, b2: SC {c1: 0}, b3: 0}});
}
extends mutates fun changeSomeEntries(self: MapWrapper) {
// Change some entries in the map given as parameter
// This will change the map passed as parameter to the function
// Similar to comments in function copyAndModifyMap, it is not possible
// to directly mutate the struct in entry 1 of the map.
// So, first make a local copy of the struct.
let c = self.m.get(1)!!;
// Modify the struct
c.a2.b2.c1 += 30;
// And assign it back into entry 1, because c is a copy so far.
// To check the previous claim, this function will immediately return
// if self.m.get(1)!!.a2.b2.c1 == c.a2.b2.c1 at this point (which will not be true):
if (self.m.get(1)!!.a2.b2.c1 == c.a2.b2.c1) {
return;
}
self.m.set(1, c);
// Now delete entries 2 and 3
self.m.del(2);
self.m.del(3);
}
extends fun unwrapAndCopyMap(self: MapWrapper): map<Int,SA> {
// Since structs are passed by value, "self" will be a copy of the wrapped map given as parameter
// Hence, the unwrapped map is a copy of the map.
return self.m;
}
extends mutates fun unwrapMap(self: MapWrapper): map<Int,SA> {
// Even though "self" is a copy, the mutates function will assign the copy back into "self" once the function finishes.
// Observe that the function will return a copy of the wrapped map as well.
// This means that there are actually TWO assignments when one does something like this:
// m = wm.unwrapMap();
// After "unwrapMap" finishes execution, wm will be reassigned with the copy of self (which in this case is identical
// to the input to "unwrapMap"), and m will be assigned with "self.m" (computed using the copy of "self").
return self.m;
}
// Identity function, just to test chaining of mutating functions.
extends mutates fun reflectMap(self: MapWrapper): MapWrapper {
return self;
}
/**** Auxiliary functions for contracts **********/
fun copyAndModifyContract(c: SemanticsTester): SemanticsTester {
// Since contracts are passed by value, this function creates a copy of the contract when called.
// In other words, c is a copy of the contract passed as parameter to the function.
// Let us modify some fields in the copy c
c.uB = SB {b1: true, b2: SC {c1: 99}, b3: 98};
c.mA.del(1);
// It is also possible to call contract functions on the copy
// This call will not change the state of c beyond those changes in the previous lines.
c.structAssign2();
return c;
}
extends mutates fun changeSomeContractFields(self: SemanticsTester) {
// Change some fields in the contract given as parameter
// This will change the contract passed as parameter to the function
self.uB = SB {b1: true, b2: SC {c1: 77}, b3: 88};
self.sA.a2.b2.c1 += 30;
}
// Identity function, but as a simple extends function.
extends fun copyContract(self: SemanticsTester): SemanticsTester {
return self;
}
// Identity function, just to test chaining of mutating functions.
extends mutates fun reflectContract(self: SemanticsTester): SemanticsTester {
return self;
}
extends mutates fun incrementIntFieldsInUB(self: SemanticsTester): SemanticsTester {
self.uB.b2.c1 += 1;
self.uB.b3 += 1;
return self;
}
extends mutates fun flipBooleanFieldsInUB(self: SemanticsTester): SemanticsTester {
self.uB.b1 = !self.uB.b1;
return self;
}
/**** Auxiliary functions for integers **********/
extends mutates fun multiplyBy2(self: Int): Int {
self *= 2;
return self;
}
extends mutates fun increment(self: Int): Int {
self += 1;
return self;
}
extends mutates fun doNothing(self: Int): Int {
return self;
}
/**** Auxiliary functions for boolean expressions **********/
fun infiniteLoop(): Bool {
while (true) {}
return true;
}
fun throwException(v: Int): Bool {
// I cannot use directly throw(v) because of issue #904
// So, I will generate a division by zero
return 1 / v > 0;
}
contract SemanticsTester {
// Currently, declaring fields like this:
//
// sC: SC = SC {c1: 5};
// sB: SB = SB {b1: true, b2: sC, b3: 10};
// sA: SA = SA {a1: 20, a2: sB};
//
// or like this:
//
// sC: SC = SC {c1: 5};
// sB: SB = SB {b1: true, b2: self.sC, b3: 10};
// sA: SA = SA {a1: 20, a2: self.sB};
//
// gives a compilation error. So, we need to define the fields like this:
sC: SC = SC {c1: 5};
sB: SB = SB {b1: true, b2: SC {c1: 5}, b3: 10};
sA: SA = SA {a1: 20, a2: SB {b1: true, b2: SC {c1: 5}, b3: 10}};
uB: SB;
// An alternative would be to assign them in the init() function, which allows the use of self.
// Declare some maps
mA: map<Int,SA>;
mB: map<Int,Bool>;
mC: map<Int,MapWrapper>; // Simulate nested maps by wrapping them in a struct,
// "Morally", mC has type map<Int,map<Int,SA>>.
// Flag storing the result of calling method mutateContractState.
// So that we can get the result using a getter method
mutateContractStateResult: Bool = false;
init() {
/***** Structs *****/
self.uB.b1 = false; // What is the meaning of this? self.uB is not initialized.
// Is this instantiating a partially constructed struct SB?
// Nevertheless, it is not possible to check if it is a partially initialized
// struct because the compiler will not allow referencing self.uB in an expression
// until self.uB is actually assigned.
self.uB.b2 = SC {c1: 40};
self.uB = SB {b1: false, b2: SC {c1: 0}, b3: 11};
self.uB.b3 = 14; // Getter structInitCheck later checks that init respects
// this last assignment
self.sB.b1 = false;
self.sB.b2 = SC {c1: 3};
/**** Maps *****/
// Initialize the mA map
self.mA.set(1, self.sA); // The compiler does not complain that self.mA is not initialized, because
// map fields in contracts are implicitly initialized as empty.
// Function checkMapInit will later check this.
// Make a copy of sA to assign a different key value pair in the map
let s = self.sA;
s.a2.b2.c1 = 100;
s.a2.b3 = 0;
self.mA.set(2, s);
// Modify struct again to insert another key-value pair
s.a2.b2.c1 = 150;
s.a1 = 5;
s.a2.b1 = false;
self.mA.set(3, s);
// Initialize the mC map. The nested map will contain a copy of self.sA.
let nestedMap = MapWrapper {m: emptyMap()};
nestedMap.m.set(10, self.sA);
self.mC.set(100, nestedMap);
/****** Contracts *****/
/* I commented out the following code because
reading the self variable inside init should not be allowed. See issue #816.
let contract_copy = self;
contract_copy.sC = SC {c1: 8};
*/
}
receive() { }
receive("mutate") {
self.mutateContractStateResult = self.mutateContractState();
}
// IMPORTANT: The operator == does not allow comparing two structs.
// So, to compare if two structs are equal using ==, one needs to compare each
// field of both structs manually.
get fun checkAllContractFieldsAreUnchanged(): Bool {
let result =
// self.uB is correctly initialized
self.uB.b1 == false &&
self.uB.b2.c1 == 0 &&
self.uB.b3 == 14 &&
// init does not modify default value of self.sA
self.sA.a1 == 20 &&
self.sA.a2.b1 == true &&
self.sA.a2.b2.c1 == 5 &&
self.sA.a2.b3 == 10 &&
// init modifies default value of self.sB
self.sB.b1 == false &&
self.sB.b2.c1 == 3 &&
self.sB.b3 == 10 &&
// init does not change default value of self.sC.
self.sC.c1 == 5 &&
// the map self.mB is empty
// (self.mB == emptyMap()) && // Commented out because it causes an internal compiler error (see issue #808)
self.mB == null && // Equivalent way of saying it is empty
self.mB.isEmpty() && // Another equivalent way of saying it is empty
// the map self.mA has these three key-value pairs:
self.mA.get(1)!!.a1 == 20 &&
self.mA.get(1)!!.a2.b1 == true &&
self.mA.get(1)!!.a2.b2.c1 == 5 &&
self.mA.get(1)!!.a2.b3 == 10 &&
self.mA.get(2)!!.a1 == 20 &&
self.mA.get(2)!!.a2.b1 == true &&
self.mA.get(2)!!.a2.b2.c1 == 100 &&
self.mA.get(2)!!.a2.b3 == 0 &&
self.mA.get(3)!!.a1 == 5 &&
self.mA.get(3)!!.a2.b1 == false &&
self.mA.get(3)!!.a2.b2.c1 == 150 &&
self.mA.get(3)!!.a2.b3 == 0;
// And no other entries
foreach (k, _ in self.mA) {
result &&= (k == 1 || k == 2 || k == 3);
}
// The map self.mC has a single entry 100 -> MapWrapper {m: 10 -> self.sA}.
foreach (k1, v1 in self.mC) {
foreach (k2, v2 in v1.m) {
result &&=
k1 == 100 &&
k2 == 10 &&
v2.a1 == 20 &&
v2.a2.b1 == true &&
v2.a2.b2.c1 == 5 &&
v2.a2.b3 == 10;
}
}
return result;
}
/*************** Structs ********************/
// Assigning a struct to a variable preserves fields
get fun structAssign1(): Bool {
let s = self.sA;
// The fields of s and self.sA are equal, and the values did not change
return s.a1 == self.sA.a1 &&
s.a2.b1 == self.sA.a2.b1 &&
s.a2.b2.c1 == self.sA.a2.b2.c1 &&
s.a2.b3 == self.sA.a2.b3 &&
s.a1 == 20 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 5 &&
s.a2.b3 == 10 &&
self.checkAllContractFieldsAreUnchanged();
}
// Assignment of structs is done by value
get fun structAssign2(): Bool {
// Make a local copy of the struct
let s = self.sA;
// Modify two fields in the local copy
s.a1 = 50;
s.a2.b2.c1 = 70;
// Make a copy of an internal struct of s
let t = s.a2;
// Modify the copy
t.b3 = 100;
// self.sA remains unchanged,
// the copy t inherits the value b2.c1 = 70 modified by s,
// the copy s does not change its b3 field.
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 50 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 70 &&
s.a2.b3 == 10 &&
t.b1 == true &&
t.b2.c1 == 70 &&
t.b3 == 100;
}
get fun paramStruct1(): Bool {
// This should not modify self.sA.
let s = getNewStruct(self.sA);
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 0 &&
s.a2.b1 == false &&
s.a2.b2.c1 == 0 &&
s.a2.b3 == 0;
}
get fun paramStruct2(): Bool {
// This should not modify self.sA.
let s = copyAndModifyStruct(self.sA);
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 120 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 6 &&
s.a2.b3 == 10;
}
get fun mutateParamStruct1(): Bool {
let s = self.sA;
// This should reassign s, but leave self.sA unchanged.
s.reassignStruct();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 0 &&
s.a2.b1 == false &&
s.a2.b2.c1 == 0 &&
s.a2.b3 == 0;
}
get fun mutateParamStruct2(): Bool {
let s = self.sA;
// This should mutate some fields of s, but leave self.sA unchanged.
s.changeSomeFields();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 120 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 6 &&
s.a2.b3 == 10;
}
get fun testReturnedStructs(): Bool {
// The call to the non-mutating extends function "copyStruct" always makes a copy
// of the parameter struct. Therefore, passing the result to the mutating function
// "changeSomeFields" will modify the copy, not the original struct.
self.sA.copyStruct().changeSomeFields();
let result =
self.checkAllContractFieldsAreUnchanged();
// For the following test, make a copy of self.sA.
let s = self.sA;
// What is the effect of executing the following line?
s.reflectStruct().changeSomeFields();
// First, reflectStruct makes a copy of s. When reflectStruct finalizes, it assigns the copy back into
// s. Additionally, reflectStruct returns the copy of s, call it s'.
// s' is then passed to changeSomeFields, which makes again a copy of s', call it s''.
// changeSomeFields changes some fields of s'' and reassigns s' with this changed s''.
// Additionally changeSomeFields returns the changed s''.
// Note that the only place where s is reassigned is after reflectStruct executes.
// This means that the above expression does NOT change s.
result &&=
self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 20 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 5 &&
s.a2.b3 == 10;
// Therefore, if we actually want to change s using changeSomeFields, we
// should split the chain steps:
s.reflectStruct(); // Each step changes s
s.changeSomeFields();
// Note that doing the trick of assigning back s
//
// s = s.reflectStruct().changeSomeFields();
//
// is not possible because changeSomeFields has void return type.
// s is now changed by changeSomeFields, and the rest remains the same
result &&=
self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 120 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 6 &&
s.a2.b3 == 10;
// For further examples of chaining of mutating functions, see the tests mutatesChainStruct
return result;
}
/*
The following functions exemplify how to properly chain mutating functions.
Each function in the chain returns a copy to the next function in the chain.
Hence, to change the original variable, we need to reassign the value returned
from the last mutating function.
*/
get fun mutatesChainStruct1(): Bool {
let s = self.sA;
s.reflectStruct().incrementIntFields().flipBooleanFields(); // s changed only by the first function
let t = self.sA;
t = t.reflectStruct().incrementIntFields().flipBooleanFields(); // Assign back to t the struct returned by the last function
// The less confusing solution is to simply break the chain.
// This solution even works when the last function in the chain has void return type.
// Each step changes z
let z = self.sA;
z.reflectStruct();
z.incrementIntFields();
z.flipBooleanFields();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 20 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 5 &&
s.a2.b3 == 10 &&
t.a1 == 21 &&
t.a2.b1 == false &&
t.a2.b2.c1 == 6 &&
t.a2.b3 == 11 &&
z.a1 == 21 &&
z.a2.b1 == false &&
z.a2.b2.c1 == 6 &&
z.a2.b3 == 11;
}
get fun mutatesChainStruct2(): Bool {
let s = self.sA;
s.reflectStruct().flipBooleanFields().incrementIntFields();
let t = self.sA;
t = t.reflectStruct().flipBooleanFields().incrementIntFields();
let z = self.sA;
z.reflectStruct();
z.flipBooleanFields();
z.incrementIntFields();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 20 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 5 &&
s.a2.b3 == 10 &&
t.a1 == 21 &&
t.a2.b1 == false &&
t.a2.b2.c1 == 6 &&
t.a2.b3 == 11 &&
z.a1 == 21 &&
z.a2.b1 == false &&
z.a2.b2.c1 == 6 &&
z.a2.b3 == 11;
}
get fun mutatesChainStruct3(): Bool {
let s = self.sA;
s.incrementIntFields().reflectStruct().flipBooleanFields();
let t = self.sA;
t = t.incrementIntFields().reflectStruct().flipBooleanFields();
let z = self.sA;
z.incrementIntFields();
z.reflectStruct();
z.flipBooleanFields();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 21 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 6 &&
s.a2.b3 == 11 &&
t.a1 == 21 &&
t.a2.b1 == false &&
t.a2.b2.c1 == 6 &&
t.a2.b3 == 11 &&
z.a1 == 21 &&
z.a2.b1 == false &&
z.a2.b2.c1 == 6 &&
z.a2.b3 == 11;
}
get fun mutatesChainStruct4(): Bool {
let s = self.sA;
s.incrementIntFields().flipBooleanFields().reflectStruct();
let t = self.sA;
t = t.incrementIntFields().flipBooleanFields().reflectStruct();
let z = self.sA;
z.incrementIntFields();
z.flipBooleanFields();
z.reflectStruct();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 21 &&
s.a2.b1 == true &&
s.a2.b2.c1 == 6 &&
s.a2.b3 == 11 &&
t.a1 == 21 &&
t.a2.b1 == false &&
t.a2.b2.c1 == 6 &&
t.a2.b3 == 11 &&
z.a1 == 21 &&
z.a2.b1 == false &&
z.a2.b2.c1 == 6 &&
z.a2.b3 == 11;
}
get fun mutatesChainStruct5(): Bool {
let s = self.sA;
s.flipBooleanFields().incrementIntFields().reflectStruct();
let t = self.sA;
t = t.flipBooleanFields().incrementIntFields().reflectStruct();
let z = self.sA;
z.flipBooleanFields();
z.incrementIntFields();
z.reflectStruct();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 20 &&
s.a2.b1 == false &&
s.a2.b2.c1 == 5 &&
s.a2.b3 == 10 &&
t.a1 == 21 &&
t.a2.b1 == false &&
t.a2.b2.c1 == 6 &&
t.a2.b3 == 11 &&
z.a1 == 21 &&
z.a2.b1 == false &&
z.a2.b2.c1 == 6 &&
z.a2.b3 == 11;
}
get fun mutatesChainStruct6(): Bool {
let s = self.sA;
s.flipBooleanFields().reflectStruct().incrementIntFields();
let t = self.sA;
t = t.flipBooleanFields().reflectStruct().incrementIntFields();
let z = self.sA;
z.flipBooleanFields();
z.reflectStruct();
z.incrementIntFields();
return self.checkAllContractFieldsAreUnchanged() &&
s.a1 == 20 &&
s.a2.b1 == false &&
s.a2.b2.c1 == 5 &&
s.a2.b3 == 10 &&
t.a1 == 21 &&
t.a2.b1 == false &&
t.a2.b2.c1 == 6 &&
t.a2.b3 == 11 &&
z.a1 == 21 &&
z.a2.b1 == false &&
z.a2.b2.c1 == 6 &&
z.a2.b3 == 11;
}
/*************** Maps ********************/
// Assigning a map to a variable preserves contents
get fun mapAssign1(): Bool {
let s = self.mA;
// The entries of s and self.mA are equal, and the values did not change
let result =
self.mA.get(1)!!.a1 == s.get(1)!!.a1 &&
self.mA.get(1)!!.a2.b1 == s.get(1)!!.a2.b1 &&
self.mA.get(1)!!.a2.b2.c1 == s.get(1)!!.a2.b2.c1 &&
self.mA.get(1)!!.a2.b3 == s.get(1)!!.a2.b3 &&
self.mA.get(2)!!.a1 == s.get(2)!!.a1 &&
self.mA.get(2)!!.a2.b1 == s.get(2)!!.a2.b1 &&
self.mA.get(2)!!.a2.b2.c1 == s.get(2)!!.a2.b2.c1 &&
self.mA.get(2)!!.a2.b3 == s.get(2)!!.a2.b3 &&
self.mA.get(3)!!.a1 == s.get(3)!!.a1 &&
self.mA.get(3)!!.a2.b1 == s.get(3)!!.a2.b1 &&
self.mA.get(3)!!.a2.b2.c1 == s.get(3)!!.a2.b2.c1 &&
self.mA.get(3)!!.a2.b3 == s.get(3)!!.a2.b3 &&
s.get(1)!!.a1 == 20 &&
s.get(1)!!.a2.b1 == true &&
s.get(1)!!.a2.b2.c1 == 5 &&
s.get(1)!!.a2.b3 == 10 &&
s.get(2)!!.a1 == 20 &&
s.get(2)!!.a2.b1 == true &&
s.get(2)!!.a2.b2.c1 == 100 &&
s.get(2)!!.a2.b3 == 0 &&
s.get(3)!!.a1 == 5 &&
s.get(3)!!.a2.b1 == false &&
s.get(3)!!.a2.b2.c1 == 150 &&
s.get(3)!!.a2.b3 == 0 &&
self.checkAllContractFieldsAreUnchanged();
// And no other entries
foreach (k, _ in s) {
result &&= (k == 1 || k == 2 || k == 3);
}
return result;
}
// Assignment of maps is done by value
get fun mapAssign2(): Bool {
// Make a local copy of the map
let s = self.mA;
// Modify an entry in the local copy
s.set(1, SA {a1: 0, a2: SB {b1: false, b2: self.sC, b3: 2}});
// The compiler does not allow directly changing the contents of a struct inside an entry:
// s.get(2)!!.a2.b2.c1 = 7;
// Only path expressions are allowed on the left of the assignment operator =
// self.mA remains unchanged (including the rest of fields)
// The rest of the entries in s remain identical to self.mA.
let result =
self.checkAllContractFieldsAreUnchanged() &&
s.get(1)!!.a1 == 0 &&
s.get(1)!!.a2.b1 == false &&
s.get(1)!!.a2.b2.c1 == 5 &&
s.get(1)!!.a2.b3 == 2 &&
s.get(2)!!.a1 == 20 &&
s.get(2)!!.a2.b1 == true &&
s.get(2)!!.a2.b2.c1 == 100 &&
s.get(2)!!.a2.b3 == 0 &&
s.get(3)!!.a1 == 5 &&
s.get(3)!!.a2.b1 == false &&
s.get(3)!!.a2.b2.c1 == 150 &&
s.get(3)!!.a2.b3 == 0;
// And no other entries
foreach (k, _ in s) {
result &&= (k == 1 || k == 2 || k == 3);
}
return result;
}
get fun paramMap1(): Bool {
// This should not modify self.mA.
let s = getNewMap(self.mA);
let result =
self.checkAllContractFieldsAreUnchanged() &&
s.get(1)!!.a1 == 0 &&
s.get(1)!!.a2.b1 == false &&
s.get(1)!!.a2.b2.c1 == 0 &&
s.get(1)!!.a2.b3 == 0;
// And no other entries
foreach (k, _ in s) {
result &&= k == 1;
}
return result;
}
get fun paramMap2(): Bool {
// This should not modify self.mA.
// In the copy: it will delete entries 2 and 3 and modify entry 1.
let s = copyAndModifyMap(self.mA);
let result =
self.checkAllContractFieldsAreUnchanged() &&
s.get(1)!!.a1 == 20 &&
s.get(1)!!.a2.b1 == true &&
s.get(1)!!.a2.b2.c1 == 45 &&
s.get(1)!!.a2.b3 == 10;
// And no other entries
foreach (k, _ in s) {
result &&= k == 1;
}
return result;
}
get fun mutateParamMap1(): Bool {
let s = MapWrapper {m: self.mA};
// This should reassign s.m, but leave self.mA unchanged.
s.reassignMap();
let result =
self.checkAllContractFieldsAreUnchanged() &&
s.m.get(1)!!.a1 == 0 &&
s.m.get(1)!!.a2.b1 == false &&
s.m.get(1)!!.a2.b2.c1 == 0 &&
s.m.get(1)!!.a2.b3 == 0;
// And no other entries
foreach (k, _ in s.m) {
result &&= k == 1;
}
return result;
}
get fun mutateParamMap2(): Bool {
let s = MapWrapper {m: self.mA};
// This should mutate entry 1 in s.m, and delete entries 2 and 3, but leave self.mA unchanged.
s.changeSomeEntries();
let result =
self.checkAllContractFieldsAreUnchanged() &&
s.m.get(1)!!.a1 == 20 &&
s.m.get(1)!!.a2.b1 == true &&
s.m.get(1)!!.a2.b2.c1 == 35 &&
s.m.get(1)!!.a2.b3 == 10;
// And no other entries
foreach (k, _ in s.m) {
result &&= k == 1;
}
return result;
}
get fun testReturnedMaps1(): Bool {
// The "get" function for maps always creates a copy of the input map
// because "get" is a non-mutating function. This means that
// the following expression will not change the struct in entry 2 of self.mA,
// even if "changeSomeFields" is a mutating function for structs.
// The mutating function "changeSomeFields" is changing the copy
// returned by "get".
self.mA.get(2)!!.changeSomeFields();
// Everything remains the same
return self.checkAllContractFieldsAreUnchanged();
}
/*
The following test cannot be carried out because FunC reports errors. See issue #866.
But the comments inside the test are good hypotheses of what I expect it will happen
when the issue is resolved, because this is what happens in the case of structs (see
function testReturnedStructs).
get fun testReturnedMaps2(): Bool {
// The call to the non-mutating extends function "unwrapAndCopyMap" always makes a copy
// of the parameter. Therefore, passing the result to the mutating function
// "del" will modify the copy, not the original.
let wm = MapWrapper {m: self.mA};
wm.unwrapAndCopyMap().del(1); // FunC error: issue #866
// The contract fields remain the same and also wm
let result =
self.checkAllContractFieldsAreUnchanged() &&
wm.m.get(1)!!.a1 == 20 &&
wm.m.get(1)!!.a2.b1 == true &&
wm.m.get(1)!!.a2.b2.c1 == 5 &&
wm.m.get(1)!!.a2.b3 == 10 &&
wm.m.get(2)!!.a1 == 20 &&
wm.m.get(2)!!.a2.b1 == true &&
wm.m.get(2)!!.a2.b2.c1 == 100 &&
wm.m.get(2)!!.a2.b3 == 0 &&