Skip to content

Commit

Permalink
checker: fix static methods not recognized when imported from a module(
Browse files Browse the repository at this point in the history
…fix #19127)
  • Loading branch information
shove70 committed Aug 13, 2023
1 parent 11a8a46 commit 54f6fcb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
23 changes: 22 additions & 1 deletion vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,23 @@ 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[0].is_capital() && fn_name.contains('__static__') {
if index := fn_name.index('__static__') {
owner_name := fn_name#[..index]
for import_sym in c.file.imports.filter(it.syms.len > 0
&& 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 {
Expand Down Expand Up @@ -936,7 +953,11 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
// a same module constant?
if !found {
// allow for `const abc = myfunc`, then calling `abc()`
qualified_const_name := if fn_name.contains('.') { fn_name } else { '${c.mod}.${fn_name}' }
mut qualified_const_name := if fn_name.contains('.') {
fn_name
} else {
'${c.mod}.${fn_name}'
}
if mut obj := c.table.global_scope.find_const(qualified_const_name) {
if obj.typ == 0 {
obj.typ = c.expr(mut obj.expr)
Expand Down
1 change: 1 addition & 0 deletions vlib/v/checker/tests/modules/import_static_fn.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
amod.SomeStruct{}
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/modules/import_static_fn/src/amod/amod.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module amod

pub struct SomeStruct {}

pub fn SomeStruct.new() SomeStruct {
return SomeStruct{}
}
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/modules/import_static_fn/src/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module main

import amod { SomeStruct }

fn main() {
_ = SomeStruct{}

result := SomeStruct.new()
println(result)
}
Empty file.

0 comments on commit 54f6fcb

Please sign in to comment.