Skip to content

Commit c08958e

Browse files
committed
[Tolk] Private fields in structs
1 parent a0e8316 commit c08958e

25 files changed

+269
-33
lines changed

crypto/smartcont/tolk-stdlib/common.tolk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type bytesN = builtin
9595
/// - a key must be fixed-width; valid: `int32`, `uint64`, `address`, `bits256`, `Point`; invalid: `int`, `coins`
9696
/// - a value must be serializable; valid: `int32`, `coins`, `AnyStruct`, `Cell<AnyStruct>`; invalid: `int`, `builder`
9797
struct map<K, V> {
98-
tvmDict: dict
98+
private tvmDict: dict
9999
}
100100

101101
/// `dict` is a low-level TVM dictionary.
@@ -123,7 +123,7 @@ type void = builtin
123123
/// Note, that `st = MyStorage.fromSlice(s)` does NOT deep-load any refs; `st.extra` is `Cell<T>`, not `T`;
124124
/// you should manually call `st.extra.load()` to get `T` (ExtraData in this example).
125125
struct Cell<T> {
126-
tvmCell: cell
126+
private readonly tvmCell: cell
127127
}
128128

129129

@@ -910,7 +910,7 @@ fun map<K, V>.iteratePrev(self, current: MapEntry<K, V>): MapEntry<K, V>
910910
/// }
911911
/// ```
912912
struct MapLookupResult<TValue> {
913-
rawSlice: slice? // holds encoded value, present if isFound
913+
private readonly rawSlice: slice? // holds encoded value, present if isFound
914914
isFound: bool
915915
}
916916

@@ -938,8 +938,8 @@ fun MapLookupResult<slice>.loadValue(self): slice {
938938
/// }
939939
/// ```
940940
struct MapEntry<K, V> {
941-
rawValue: slice? // holds encoded value, present if isFound
942-
key: K
941+
private readonly rawValue: slice? // holds encoded value, present if isFound
942+
private readonly key: K
943943
isFound: bool
944944
}
945945

tolk-tester/tests/dicts-demo.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fun test106() {
113113
dict.addIntToIDict(3, 103);
114114

115115
var m = createMapFromLowLevelDict<int32, int32>(dict);
116-
val fk = m.findFirst().key;
116+
val fk = m.findFirst().getKey();
117117
val fv = m.findFirst().loadValue();
118118
var t = createEmptyTuple();
119119
var r = m.findKeyLess(3);

tolk-tester/tests/generics-2.tolk

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,49 @@ fun test27() {
463463
__expect_type(pp2, "Pair<coins, coins?> | Wrapper<int>");
464464
}
465465

466+
struct Box<T> {
467+
private readonly item: T
468+
}
469+
470+
fun Box<T>.create(v: T) {
471+
return Box { item: v }
472+
}
473+
474+
fun Box<T>.itemEquals(self, cmp: T) {
475+
return self.item == cmp
476+
}
477+
478+
fun Box<T>.itemEqualsTo<U>(self, cmp: Box<U>) {
479+
return self.item == cmp.item
480+
}
481+
482+
fun Box<T>.clone<U>(self) {
483+
var b = Box { item: self.item as U };
484+
b.item;
485+
return b;
486+
}
487+
488+
fun Box<KKK>.areEqual(b1: Box<KKK>, b2: Box<KKK>) {
489+
return b1.item == b2.item
490+
}
491+
492+
fun Box<int32>.specifiedGetItem(self) {
493+
return self.item
494+
}
495+
496+
@method_id(128)
497+
fun test28() {
498+
var b1 = Box<int32>.create(5);
499+
var b2 = Box<int256>.create(4);
500+
var b3 = b2.clone<int>();
501+
__expect_type(b3, "Box<int>");
502+
return (
503+
b1.itemEquals(0), b2.itemEquals(4),
504+
b1.itemEqualsTo(b2), b2.itemEqualsTo(b3),
505+
Box<int32>.areEqual(b1, b1),
506+
);
507+
}
508+
466509

467510
fun main(c: Wrapper<slice>, d: WrappedInt) {
468511
__expect_type(c, "Wrapper<slice>");
@@ -507,6 +550,7 @@ fun main(c: Wrapper<slice>, d: WrappedInt) {
507550
@testcase | 120 | | (null) typeid-9 777 (null) (null) 0 typeid-10 -1 -1
508551
@testcase | 123 | | 10 10 10 10 10
509552
@testcase | 126 | | 0 0 0 (null) 777 0 typeid-12 (null) typeid-12 (null) 0
553+
@testcase | 128 | | 0 -1 0 -1 -1
510554

511555

512556
@fif_codegen
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fun m() {
2+
var c = (12 as int64).toCell();
3+
var s = c.tvmCell.beginParse();
4+
}
5+
6+
/**
7+
@compilation_should_fail
8+
@stderr field `Cell<int64>.tvmCell` is private
9+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Point { private x: int, private y: int }
2+
struct Point3d { x: int, y: int, z: int }
3+
4+
fun Point3d.create() {
5+
return Point { x: 10, y: 20 }
6+
}
7+
8+
/**
9+
@compilation_should_fail
10+
@stderr field `Point.x` is private
11+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Wrap<T> { private item: T }
2+
3+
fun m(w: Wrap<int32>) {
4+
w = w;
5+
return w.item!;
6+
}
7+
8+
/**
9+
@compilation_should_fail
10+
@stderr field `Wrap<int32>.item` is private
11+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct A {
2+
private readonly a: ()
3+
readonly b: ()
4+
private private e: ()
5+
}
6+
7+
/**
8+
@compilation_should_fail
9+
@stderr expected field name, got `private`
10+
@stderr private private e
11+
*/

tolk-tester/tests/maps-tests.tolk

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fun test14(fromV: int) {
204204
m.set({v: 1}, 10);
205205
m.set({v: 2}, 20);
206206
m.set({v: 3}, 30);
207-
return m.findKeyGreater({v: fromV}).key
207+
return m.findKeyGreater({v: fromV}).getKey()
208208
}
209209

210210
@method_id(115)
@@ -227,7 +227,7 @@ fun test16() {
227227
fun test17() {
228228
var m = createEmptyMap<bool, MInt32>();
229229
m.set(true, 123);
230-
return m.findFirst().key;
230+
return m.findFirst().getKey();
231231
}
232232

233233
@method_id(118)
@@ -274,7 +274,7 @@ fun test22() {
274274
m.set(3, Point{x:30,y:30}.toCell());
275275
m.set(2, Point{x:20,y:20}.toCell());
276276
m.set(1, Point{x:10,y:10}.toCell());
277-
return (m.findFirst().key, m.findLast().key, m.findKeyGreater(2).key, m.findKeyGreaterOrEqual(2).loadValue().load(), m.mustGet(2).load().x);
277+
return (m.findFirst().getKey(), m.findLast().getKey(), m.findKeyGreater(2).getKey(), m.findKeyGreaterOrEqual(2).loadValue().load(), m.mustGet(2).load().x);
278278
}
279279

280280
@method_id(123)
@@ -329,7 +329,7 @@ fun test26() {
329329
fun test27() {
330330
var m = createEmptyMap<int8, slice>();
331331
m.set(1, "");
332-
return (m.findFirst().key, m.findFirst().loadValue().remainingBitsCount());
332+
return (m.findFirst().getKey(), m.findFirst().loadValue().remainingBitsCount());
333333
}
334334

335335
@method_id(128)
@@ -359,7 +359,7 @@ fun test29() {
359359
DataWithCell.fromSlice(m.mustGet(2)).payload,
360360
m.get(3).loadValue().loadAny<DataWithCell>().p!.load(),
361361
m.get(3).loadValue().remainingRefsCount(),
362-
m.findLast().key,
362+
m.findLast().getKey(),
363363
m.findLast().loadValue().remainingRefsCount(),
364364
);
365365
}
@@ -406,7 +406,7 @@ fun test32() {
406406
m.set(stringHexToSlice("0304") as bits16, stringHexToSlice("bbbb") as bits16);
407407
val repl = m.replaceAndGetPrevious(stringHexToSlice("0304") as bits16, stringHexToSlice("0000") as bits16);
408408
var r = m.iteratePrev(m.findLast());
409-
return ((r.key as slice).remainingBitsCount(), (r.key as slice).loadAny<Point>(), (r.loadValue() as slice).remainingBitsCount(), repl.isFound, (repl.loadValue() as slice).loadUint(8));
409+
return ((r.getKey() as slice).remainingBitsCount(), (r.getKey() as slice).loadAny<Point>(), (r.loadValue() as slice).remainingBitsCount(), repl.isFound, (repl.loadValue() as slice).loadUint(8));
410410
}
411411

412412
@method_id(133)

tolk-tester/tests/pack-unpack-1.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun test1(value: int) {
3434
fun test2() {
3535
var t: JustInt32 = { value: 10 };
3636
var c = t.toCell();
37-
var s = c.tvmCell.beginParse();
37+
var s = c.beginParse();
3838
__expect_type(s.skipAny<JustInt32>(), "slice");
3939
s.assertEnd();
4040
return true;

tolk-tester/tests/pack-unpack-2.tolk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ fun test_TwoInts32And64() {
468468

469469
@method_id(205)
470470
fun test_TwoInts32AndRef64() {
471-
run<TwoInts32AndRef64>({ op: 123, query_id_ref: { tvmCell: beginCell().storeUint(255,64).endCell() } }, stringHexToSlice("0000007B").appendRef(stringHexToSlice("00000000000000FF")));
471+
run<TwoInts32AndRef64>({ op: 123, query_id_ref: (255 as uint64).toCell() }, stringHexToSlice("0000007B").appendRef(stringHexToSlice("00000000000000FF")));
472472
return true;
473473
}
474474

@@ -627,7 +627,7 @@ fun test_DifferentMix2() {
627627
assert(iae1.query_id_maybe_ref == null, 400);
628628
assert_slice_is_44_and_ref45(r1.rest);
629629

630-
run<DifferentMix2>({ iae: { tvmCell: IntAndEither32OrRef64{ op: 778, i32orRef: { tvmCell: Inner2{ i64_in_ref: 9919992 }.toCell() }, query_id_maybe_ref: Inner1{ query_id_ref: 889477 }.toCell() }.toCell() }, tic: { op: 123, amount: 500000 }, rest: beginCell().endCell().beginParse() },
630+
run<DifferentMix2>({ iae: IntAndEither32OrRef64{ op: 778, i32orRef: Inner2{ i64_in_ref: 9919992 }.toCell(), query_id_maybe_ref: Inner1{ query_id_ref: 889477 }.toCell() }.toCell(), tic: { op: 123, amount: 500000 }, rest: beginCell().endCell().beginParse() },
631631
stringHexToSlice("0000007B307A120").appendRef(stringHexToSlice("0000030AE_").appendRef(stringHexToSlice("0000000000975DF8")).appendRef(stringHexToSlice("00000000000D9285"))));
632632
var r2 = DifferentMix2.fromSlice(stringHexToSlice("0000007B307A120").appendRef(stringHexToSlice("0000030AE_").appendRef(stringHexToSlice("0000000000975DF8")).appendRef(stringHexToSlice("00000000000D9285"))));
633633
val iae2 = r2.iae.load();

0 commit comments

Comments
 (0)