From 79b068bd2e41b13c7ec4c75e4388f61fd44c46aa Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 19 Oct 2023 17:33:55 +0300 Subject: [PATCH] ast: check receivers of callexprs too in ast.Table.dependent_names_in_expr (used for finding the constants, that another constant depends on) (#19601) --- vlib/v/ast/table.v | 3 +++ vlib/v/tests/const_init_order_test.v | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 8e2ac86fee..bdda0bde9c 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -2342,6 +2342,9 @@ pub fn (t &Table) dependent_names_in_expr(expr Expr) []string { names << t.dependent_names_in_expr(expr.init_expr) } CallExpr { + if expr.is_method { + names << t.dependent_names_in_expr(expr.left) + } for arg in expr.args { names << t.dependent_names_in_expr(arg.expr) } diff --git a/vlib/v/tests/const_init_order_test.v b/vlib/v/tests/const_init_order_test.v index cf5ac18d0c..3e77d9fe14 100644 --- a/vlib/v/tests/const_init_order_test.v +++ b/vlib/v/tests/const_init_order_test.v @@ -1,3 +1,4 @@ +import os import rand const ( @@ -8,3 +9,18 @@ fn test_rand_is_initialized_before_main() { eprintln('random letter: ${my_random_letter_const.str()} | ASCII code: ${my_random_letter_const}') assert my_random_letter_const.is_capital() } + +// + +const ( + last_constant = fn_that_calls_a_method_on_a_constant() + a_constant = os.join_path(@VROOT, 'a') +) + +fn fn_that_calls_a_method_on_a_constant() string { + return a_constant.replace('\\', '/') +} + +fn test_consts_initialised_with_a_function_that_uses_other_consts_as_receivers_are_properly_ordered() { + assert last_constant != '' +}