Skip to content

Commit 38a95cb

Browse files
committed
[Tolk] Better error messages on type mismatch
For example, when passing `bits8` to `slice`, the compiler now suggests to use `as` operator.
1 parent 83ca804 commit 38a95cb

18 files changed

+132
-14
lines changed

tolk-tester/tests/assignment-tests.tolk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ fun test118(x: int) {
249249
return (i1, i2, st.owner.remainingBitsCount(), i3.0);
250250
}
251251

252+
@method_id(119)
253+
fun test119(a: int, b: int) {
254+
var t: tuple = [a + 1, b * 2] as tuple;
255+
val l = t.size();
256+
val [c,d:int] = [t.0 as int, t.1];
257+
return (l, c, d);
258+
}
259+
252260

253261

254262
fun main(value: int, ) {
@@ -279,6 +287,7 @@ fun main(value: int, ) {
279287
@testcase | 116 | | 1 2 3 4 [ 1 2 3 4 ]
280288
@testcase | 117 | | [ 20 ]
281289
@testcase | 118 | 3 | 10 3 0 1
290+
@testcase | 119 | 1 2 | 2 2 4
282291

283292

284293
@fif_codegen
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fun analyze(t: slice) {}
2+
3+
fun main(t: slice?) {
4+
analyze(t);
5+
}
6+
7+
/**
8+
@compilation_should_fail
9+
@stderr can not pass `slice?` to `slice`
10+
@stderr hint: probably, you should check on null
11+
@stderr hint: alternatively, use `!` operator to bypass nullability checks: `<some_expr>!`
12+
*/

tolk-tester/tests/invalid-typing/err-6087.tolk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ fun cantAutoCastBytesNToSlice() {
66
/**
77
@compilation_should_fail
88
@stderr method `loadInt` not found for type `bits32`
9+
@stderr but it exists for type `slice`
910
*/

tolk-tester/tests/invalid-typing/err-6135.tolk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ fun cantAssignIntNToCoins(n: int8, c: coins) {
88
@compilation_should_fail
99
@stderr can not assign `coins` to variable of type `int8`
1010
@stderr n = c;
11+
@stderr hint: use `as` operator for unsafe casting: `<some_expr> as int8`
1112
*/

tolk-tester/tests/invalid-typing/err-6249.tolk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ fun cantUnifyBytesNAndSlice(n: slice, c: bytes16) {
77
/**
88
@compilation_should_fail
99
@stderr types of ternary branches are incompatible: `slice` and `bytes16`
10+
@stderr hint: maybe, you should use `<some_expr> as <type>` to make them identical
1011
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fun main(k: (int, slice)) {
2+
var (a, b, c: int?) = k;
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr can not assign `(int, slice)`, sizes mismatch
8+
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fun main() {
2+
blockchain.configParam(true);
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr can not pass `bool` to `int`
8+
@stderr hint: use `as` operator for unsafe casting: `<some_expr> as int`
9+
@stderr caution! in TVM, bool TRUE is -1, not 1
10+
*/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct A {
2+
kk: tuple
3+
dd: [int, int]
4+
}
5+
6+
fun main(a: A) {
7+
a.kk = a.dd
8+
}
9+
10+
/**
11+
@compilation_should_fail
12+
@stderr can not assign `[int, int]` to field of type `tuple`
13+
@stderr hint: use `as` operator for unsafe casting: `<some_expr> as tuple`
14+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fun main(c: cell) {
2+
var sender_address: slice = c.beginParse().loadAddress();
3+
}
4+
5+
/**
6+
@compilation_should_fail
7+
@stderr can not assign `address` to variable of type `slice`
8+
@stderr hint: unlike FunC, Tolk has a special type `address` (which is slice at the TVM level);
9+
@stderr most likely, you just need `address` everywhere
10+
@stderr hint: alternatively, use `as` operator for unsafe casting: `<some_expr> as slice`
11+
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fun f(d: bits8 = "") {}
2+
3+
/**
4+
@compilation_should_fail
5+
@stderr can not assign `slice` to `bits8`
6+
@stderr hint: use `as` operator for unsafe casting: `<some_expr> as bits8`
7+
*/

0 commit comments

Comments
 (0)