1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00
ton/tolk-tester/tests/warnings-2.tolk
tolk-vm 7bcb8b895f
[Tolk] Smart casts and control flow graph
With the introduction of nullable types, we want the
compiler to be smart in cases like
> if (x == null) return;
> // x is int now
or
> if (x == null) x = 0;
> // x is int now

These are called smart casts: when the type of variable
at particular usage might differ from its declaration.

Implementing smart casts is very challenging. They are based
on building control-flow graph and handling every AST vertex
with care. Actually, I represent cfg not a as a "graph with
edges". Instead, it's a "structured DFS" for the AST:
1) at every point of inferring, we have "current flow facts"
2) when we see an `if (...)`, we create two derived contexts
3) after `if`, finalize them at the end and unify
4) if we detect unreachable code, we mark that context
In other words, we get the effect of a CFG but in a more direct
approach. That's enough for AST-level data-flow.

Smart casts work for local variables and tensor/tuple indices.
Compilation errors have been reworked and now are more friendly.
There are also compilation warnings for always true/false
conditions inside if, assert, etc.
2025-02-28 16:44:15 +03:00

26 lines
970 B
Text

fun main() {
var (a, b, c, d, e) = (1, beginCell(), beginCell().endCell().beginParse(), [1], true as bool?);
var alwaysInt = a != null ? 1 : null;
__expect_type(alwaysInt, "int");
if (!(c == null)) {
if (10 < 3) { assert(b == null, 100); }
}
while (d == null || false) {}
return e! != null;
}
/**
@testcase | 0 | | -1
@stderr warning: variable `a` of type `int` is always not null, this condition is always true
@stderr warning: condition of ternary operator is always true
@stderr warning: variable `c` of type `slice` is always not null, this condition is always false
@stderr warning: condition of `if` is always true
@stderr warning: variable `b` of type `builder` is always not null, this condition is always false
@stderr warning: condition of `assert` is always false
@stderr warning: condition of `while` is always false
@stderr warning: expression of type `bool` is always not null, this condition is always true
*/