From 667a07c289da92359f8830ae98c12700b2cf3476 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 25 Dec 2024 00:41:13 +0800 Subject: [PATCH] ast: fix const map init order (fix #23255) (#23256) --- vlib/v/ast/table.v | 3 +++ .../tests/consts/const_map_init_order_test.v | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 vlib/v/tests/consts/const_map_init_order_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index b485007c59098d..4371f689a9cace 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -2552,6 +2552,9 @@ pub fn (t &Table) dependent_names_in_expr(expr Expr) []string { names << t.dependent_names_in_expr(expr.right) } MapInit { + for key in expr.keys { + names << t.dependent_names_in_expr(key) + } for val in expr.vals { names << t.dependent_names_in_expr(val) } diff --git a/vlib/v/tests/consts/const_map_init_order_test.v b/vlib/v/tests/consts/const_map_init_order_test.v new file mode 100644 index 00000000000000..bbdafe34565e5c --- /dev/null +++ b/vlib/v/tests/consts/const_map_init_order_test.v @@ -0,0 +1,26 @@ +const bit = f64(1) +const nibble = bit * 4 +const bytes = bit * 8 +const kb = bytes * 1000 +const mb = kb * 1000 + +const units_map = { + bit: 'bit' + nibble: 'nibble' + bytes: 'byte' + kb: 'kB' + mb: 'MB' +} + +fn test_const_map_init_order() { + println(units_map.len) + println(units_map) + assert units_map.len == 5 + assert units_map == { + 1.0: 'bit' + 4.0: 'nibble' + 8.0: 'byte' + 8000.0: 'kB' + 8.e+06: 'MB' + } +}