From caac89d6ca2cb291c34cf7bad1696dbdf7534247 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 16 Dec 2021 12:03:49 +0200 Subject: [PATCH] checker: fix `function x is private` error in main, for `pub const abc = x()` in a (sub)module --- vlib/v/checker/checker.v | 4 ++++ .../bin/a_program_under_bin_can_find_mod1_test.v | 5 +++++ .../mod1/submodule/m.v | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index bd51f91487..de26147eaf 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3818,9 +3818,13 @@ pub fn (mut c Checker) ident(mut node ast.Ident) ast.Type { } mut typ := obj.typ if typ == 0 { + old_c_mod := c.mod + c.mod = obj.mod c.inside_const = true typ = c.expr(obj.expr) c.inside_const = false + c.mod = old_c_mod + if obj.expr is ast.CallExpr { if obj.expr.or_block.kind != .absent { typ = typ.clear_flag(.optional) diff --git a/vlib/v/tests/project_with_modules_having_submodules/bin/a_program_under_bin_can_find_mod1_test.v b/vlib/v/tests/project_with_modules_having_submodules/bin/a_program_under_bin_can_find_mod1_test.v index e2045a652d..b5f99407a9 100644 --- a/vlib/v/tests/project_with_modules_having_submodules/bin/a_program_under_bin_can_find_mod1_test.v +++ b/vlib/v/tests/project_with_modules_having_submodules/bin/a_program_under_bin_can_find_mod1_test.v @@ -4,6 +4,11 @@ fn test_mod1_can_still_be_found_through_parent_project_vmod() { assert 1051 == m.f() } +fn test_pub_const_using_private_fn_for_its_initialisation() { + println(m.my_version) + assert m.my_version == '0.0.1' +} + /* NB: this main program is under bin/ , but it still can find mod1, because the parent project has v.mod, diff --git a/vlib/v/tests/project_with_modules_having_submodules/mod1/submodule/m.v b/vlib/v/tests/project_with_modules_having_submodules/mod1/submodule/m.v index e241849fb7..2467717b40 100644 --- a/vlib/v/tests/project_with_modules_having_submodules/mod1/submodule/m.v +++ b/vlib/v/tests/project_with_modules_having_submodules/mod1/submodule/m.v @@ -13,3 +13,9 @@ import v.tests.project_with_modules_having_submodules.mod1.mod14 pub fn f() int { return 1000 + mod11.f() + mod12.f() + mod13.f() + mod14.f() } + +pub const my_version = get_version() + +fn get_version() string { + return '0.0.1' +}