From 9b9edfc05dd5c41fb2b3de3120aef2ccb8a67e9c Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Wed, 22 Jan 2025 06:42:21 +0800 Subject: [PATCH] Update checker.v Need check `sum_type` and `function` also. And `function` type try to get the correct mod name. --- vlib/v/checker/checker.v | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c0ab0c47c54911..b292ef88b7377b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5293,11 +5293,25 @@ fn (mut c Checker) ensure_type_exists(typ ast.Type, pos token.Pos) bool { return false } sym := c.table.sym(typ) - if !c.is_builtin_mod && sym.kind in [.struct, .alias] && !sym.is_pub && sym.mod != c.mod - && c.comptime.comptime_for_field_var == '' && !c.inside_recheck { - c.error('${sym.kind} `${sym.name}` was declared as private to module `${sym.mod}`, so it can not be used inside module `${c.mod}`', - pos) - return false + if !c.is_builtin_mod && sym.kind in [.struct, .alias, .sum_type, .function]! && !sym.is_pub + && sym.mod != c.mod && c.comptime.comptime_for_field_var == '' && !c.inside_recheck { + if sym.kind == .function { + fn_info := sym.info as ast.FnType + // hack: recover fn mod from func name + mut fn_mod := fn_info.func.name.all_before_last('.') + if fn_mod == fn_info.func.name { + fn_mod = 'builtin' + } + if fn_mod != '' && fn_mod != c.mod && fn_info.func.name != '' && !fn_info.is_anon { + c.error('function type `${fn_info.func.name}` was declared as private to module `${fn_mod}`, so it can not be used inside module `${c.mod}`', + pos) + return false + } + } else { + c.error('${sym.kind} `${sym.name}` was declared as private to module `${sym.mod}`, so it can not be used inside module `${c.mod}`', + pos) + return false + } } match sym.kind { .placeholder {