Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen,checker: fix builtin fn var resolve #21899

Merged
merged 4 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3605,6 +3605,25 @@ struct ACFieldMethod {
typ string
}

fn (mut c Checker) resolve_var_fn(func ast.Fn, mut node ast.Ident, name string) ast.Type {
mut fn_type := ast.new_type(c.table.find_or_register_fn_type(func, false, true))
if func.generic_names.len > 0 {
concrete_types := node.concrete_types.map(c.unwrap_generic(it))
if typ_ := c.table.resolve_generic_to_concrete(fn_type, func.generic_names, concrete_types) {
fn_type = typ_
if concrete_types.all(!it.has_flag(.generic)) {
c.table.register_fn_concrete_types(func.fkey(), concrete_types)
}
}
}
node.name = name
node.kind = .function
node.info = ast.IdentFn{
typ: fn_type
}
return fn_type
}

fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
if c.pref.linfo.is_running {
// Mini LS hack (v -line-info "a.v:16")
Expand Down Expand Up @@ -3850,25 +3869,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
}
// Non-anon-function object (not a call), e.g. `onclick(my_click)`
if func := c.table.find_fn(name) {
mut fn_type := ast.new_type(c.table.find_or_register_fn_type(func, false,
true))
if func.generic_names.len > 0 {
concrete_types := node.concrete_types.map(c.unwrap_generic(it))
if typ_ := c.table.resolve_generic_to_concrete(fn_type, func.generic_names,
concrete_types)
{
fn_type = typ_
if concrete_types.all(!it.has_flag(.generic)) {
c.table.register_fn_concrete_types(func.fkey(), concrete_types)
}
}
}
node.name = name
node.kind = .function
node.info = ast.IdentFn{
typ: fn_type
}
return fn_type
return c.resolve_var_fn(func, mut node, name)
}
}
if node.language == .c {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4756,7 +4756,7 @@ fn (mut g Gen) ident(node ast.Ident) {
g.write(util.no_dots(node.name[2..]))
return
}
mut name := c_name(node.name)
mut name := if node.kind == .function { c_fn_name(node.name) } else { c_name(node.name) }
felipensp marked this conversation as resolved.
Show resolved Hide resolved
if node.kind == .constant {
if g.pref.translated && !g.is_builtin_mod
&& !util.module_is_builtin(node.name.all_before_last('.')) {
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/tests/buitlin_fn_var_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn test_main() {
a := error
assert dump(a('foo')) == error('foo')
assert dump(error) == error
}
Loading