From 58d4bd67b2c56bb918bfb8ede704c52c38ca0ccb Mon Sep 17 00:00:00 2001 From: shove Date: Mon, 14 Aug 2023 11:29:52 +0800 Subject: [PATCH] checker, fmt: fix static methods not recognized when imported from a module(fix #19127) (#19133) --- vlib/v/checker/fn.v | 16 ++++++++++++++++ vlib/v/fmt/fmt.v | 3 ++- .../import_symbol_static_method_test.v | 7 +++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/modules/methods_struct_another_module/import_symbol_static_method_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 8271aa476..1bac94bd6 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -841,6 +841,22 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. c.table.fns[fn_name].usages++ } } + // already imported symbol (static Foo.new() in another module) + if !found && fn_name.contains('__static__') && fn_name[0].is_capital() { + if index := fn_name.index('__static__') { + owner_name := fn_name#[..index] + for import_sym in c.file.imports.filter(it.syms.any(it.name == owner_name)) { + qualified_name := '${import_sym.mod}.${fn_name}' + if f := c.table.find_fn(qualified_name) { + found = true + func = f + node.name = qualified_name + c.table.fns[qualified_name].usages++ + break + } + } + } + } mut is_native_builtin := false if !found && c.pref.backend == .native { if fn_name in ast.native_builtins { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6040680b2..58d21eb98 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1876,8 +1876,8 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { f.write('${node.name.after_char(`.`)}') } else { name := f.short_module(node.name) - f.mark_import_as_used(name) if node.name.contains('__static__') { + f.mark_import_as_used(node.name.split('__static__')[0]) if name.contains('.') { indx := name.index('.') or { -1 } + 1 f.write(name[0..indx] + name[indx..].replace('__static__', '.').capitalize()) @@ -1885,6 +1885,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { f.write(name.replace('__static__', '.').capitalize()) } } else { + f.mark_import_as_used(name) f.write(name) } } diff --git a/vlib/v/tests/modules/methods_struct_another_module/import_symbol_static_method_test.v b/vlib/v/tests/modules/methods_struct_another_module/import_symbol_static_method_test.v new file mode 100644 index 000000000..5a67cdcec --- /dev/null +++ b/vlib/v/tests/modules/methods_struct_another_module/import_symbol_static_method_test.v @@ -0,0 +1,7 @@ +import time { Time, now } + +fn test_import_symbol_static_method() { + now := now() + now_2 := Time.new(now) + assert now == now_2 +} -- 2.39.5