mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[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.
This commit is contained in:
parent
f3e620f48c
commit
7bcb8b895f
47 changed files with 3057 additions and 833 deletions
|
@ -119,14 +119,14 @@ TypePtr GenericSubstitutionsDeduceForCall::replace_by_manually_specified(TypePtr
|
|||
return replace_genericT_with_deduced(param_type, fun_ref->genericTs, substitutionTs);
|
||||
}
|
||||
|
||||
TypePtr GenericSubstitutionsDeduceForCall::auto_deduce_from_argument(SrcLocation loc, TypePtr param_type, TypePtr arg_type) {
|
||||
TypePtr GenericSubstitutionsDeduceForCall::auto_deduce_from_argument(FunctionPtr cur_f, SrcLocation loc, TypePtr param_type, TypePtr arg_type) {
|
||||
try {
|
||||
if (!manually_specified) {
|
||||
consider_next_condition(param_type, arg_type);
|
||||
}
|
||||
return replace_genericT_with_deduced(param_type, fun_ref->genericTs, substitutionTs);
|
||||
} catch (const GenericDeduceError& ex) {
|
||||
throw ParseError(loc, ex.message + " for generic function `" + fun_ref->as_human_readable() + "`; instantiate it manually with `" + fun_ref->name + "<...>()`");
|
||||
throw ParseError(cur_f, loc, ex.message + " for generic function `" + fun_ref->as_human_readable() + "`; instantiate it manually with `" + fun_ref->name + "<...>()`");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -201,7 +201,6 @@ static void run_pipeline_for_instantiated_function(FunctionPtr inst_fun_ref) {
|
|||
// these pipes are exactly the same as in tolk.cpp — all preceding (and including) type inferring
|
||||
pipeline_resolve_identifiers_and_assign_symbols(inst_fun_ref);
|
||||
pipeline_calculate_rvalue_lvalue(inst_fun_ref);
|
||||
pipeline_detect_unreachable_statements(inst_fun_ref);
|
||||
pipeline_infer_types_and_calls_and_fields(inst_fun_ref);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue