From 3348823027477f9c93efe142b0bbef1083668bc3 Mon Sep 17 00:00:00 2001 From: Denis Vaksman Date: Tue, 2 Jul 2024 19:03:30 +0300 Subject: [PATCH] check that global var type and its init value are the same (#1032) --- compiler/code-gen/files/global-vars-reset.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compiler/code-gen/files/global-vars-reset.cpp b/compiler/code-gen/files/global-vars-reset.cpp index 1f1fdf4092..8c0bbbc392 100644 --- a/compiler/code-gen/files/global-vars-reset.cpp +++ b/compiler/code-gen/files/global-vars-reset.cpp @@ -10,6 +10,9 @@ #include "compiler/code-gen/namespace.h" #include "compiler/code-gen/vertex-compiler.h" #include "compiler/data/src-file.h" +#include "compiler/inferring/public.h" +#include "compiler/inferring/type-data.h" +#include "compiler/kphp_assert.h" GlobalVarsReset::GlobalVarsReset(const GlobalsBatchedMem &all_globals_in_mem) : all_globals_in_mem(all_globals_in_mem) {} @@ -41,6 +44,17 @@ void GlobalVarsReset::compile_globals_reset_part(CodeGenerator &W, const Globals W << "// " << var->as_human_readable() << NL; W << "hard_reset_var(" << GlobalVarInPhpGlobals(var); if (var->init_val) { + // TODO: fix unstable type inferring + const TypeData *global_var_type = tinf::get_type(var); + const TypeData *init_val_type = tinf::get_type(var->init_val); + // a -> b <=> !a || b + kphp_error(!(global_var_type->get_real_ptype() == tp_array && + init_val_type->get_real_ptype() == tp_array && + init_val_type->lookup_at_any_key()->get_real_ptype() != tp_any) + || are_equal_types(global_var_type, init_val_type), + fmt_format("Types of global variable and its init value differ: {} and {}.\n" + "Probably because of unstable type inferring, try rerun compilation", + global_var_type->as_human_readable(), init_val_type->as_human_readable())); W << ", " << var->init_val; } W << ");" << NL;