mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner. This is a common approach for functional languages, where types are inferred from usage through unification. As a result, type declarations are not necessary: () f(a,b) { return a+b; } // a and b now int, since `+` (int, int) While this approach works for now, problems arise with the introduction of new types like bool, where `!x` must handle both int and bool. It will also become incompatible with int32 and other strict integers. This will clash with structure methods, struggle with proper generics, and become entirely impractical for union types. This PR completely rewrites the type system targeting the future. 1) type of any expression is inferred and never changed 2) this is available because dependent expressions already inferred 3) forall completely removed, generic functions introduced (they work like template functions actually, instantiated while inferring) 4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)` 5) `as` keyword, for example `t.tupleAt(0) as int` 6) methods binding is done along with type inferring, not before ("before", as worked previously, was always a wrong approach)
This commit is contained in:
parent
3540424aa1
commit
799e2d1265
101 changed files with 5402 additions and 2713 deletions
|
@ -33,31 +33,27 @@ static void fire_error_impure_operation_inside_pure_function(AnyV v) {
|
|||
|
||||
class CheckImpureOperationsInPureFunctionVisitor final : public ASTVisitorFunctionBody {
|
||||
static void fire_if_global_var(AnyExprV v) {
|
||||
if (auto v_ident = v->try_as<ast_identifier>()) {
|
||||
if (auto v_ident = v->try_as<ast_reference>()) {
|
||||
if (v_ident->sym->try_as<GlobalVarData>()) {
|
||||
fire_error_impure_operation_inside_pure_function(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void visit(V<ast_local_var> v) override {
|
||||
if (v->marked_as_redef) {
|
||||
fire_if_global_var(v->get_identifier());
|
||||
}
|
||||
void visit(V<ast_assign> v) override {
|
||||
fire_if_global_var(v->get_lhs());
|
||||
parent::visit(v);
|
||||
}
|
||||
|
||||
void visit(V<ast_binary_operator> v) override {
|
||||
if (v->is_set_assign() || v->is_assign()) {
|
||||
fire_if_global_var(v->get_lhs());
|
||||
}
|
||||
|
||||
void visit(V<ast_set_assign> v) override {
|
||||
fire_if_global_var(v->get_lhs());
|
||||
parent::visit(v);
|
||||
}
|
||||
|
||||
void visit(V<ast_function_call> v) override {
|
||||
// most likely it's a global function, but also may be `some_var(args)` or even `getF()(args)`
|
||||
// v is `globalF(args)` / `globalF<int>(args)` / `obj.method(args)` / `local_var(args)` / `getF()(args)`
|
||||
if (!v->fun_maybe) {
|
||||
// calling variables is always impure, no considerations about what's there at runtime
|
||||
// `local_var(args)` is always impure, no considerations about what's there at runtime
|
||||
fire_error_impure_operation_inside_pure_function(v);
|
||||
}
|
||||
|
||||
|
@ -68,14 +64,6 @@ class CheckImpureOperationsInPureFunctionVisitor final : public ASTVisitorFuncti
|
|||
parent::visit(v);
|
||||
}
|
||||
|
||||
void visit(V<ast_dot_method_call> v) override {
|
||||
if (!v->fun_ref->is_marked_as_pure()) {
|
||||
fire_error_impure_operation_inside_pure_function(v);
|
||||
}
|
||||
|
||||
parent::visit(v);
|
||||
}
|
||||
|
||||
void visit(V<ast_argument> v) override {
|
||||
if (v->passed_as_mutate) {
|
||||
fire_if_global_var(v->get_expr());
|
||||
|
@ -93,15 +81,13 @@ class CheckImpureOperationsInPureFunctionVisitor final : public ASTVisitorFuncti
|
|||
}
|
||||
|
||||
public:
|
||||
void start_visiting_function(V<ast_function_declaration> v_function) override {
|
||||
if (v_function->marked_as_pure) {
|
||||
parent::visit(v_function->get_body());
|
||||
}
|
||||
bool should_visit_function(const FunctionData* fun_ref) override {
|
||||
return fun_ref->is_code_function() && !fun_ref->is_generic_function() && fun_ref->is_marked_as_pure();
|
||||
}
|
||||
};
|
||||
|
||||
void pipeline_check_pure_impure_operations(const AllSrcFiles& all_src_files) {
|
||||
visit_ast_of_all_functions<CheckImpureOperationsInPureFunctionVisitor>(all_src_files);
|
||||
void pipeline_check_pure_impure_operations() {
|
||||
visit_ast_of_all_functions<CheckImpureOperationsInPureFunctionVisitor>();
|
||||
}
|
||||
|
||||
} // namespace tolk
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue