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

parser, checker: fix generic fn variable assignment in generic fn (fix #7287) #18180

Merged
merged 1 commit into from
May 17, 2023
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
4 changes: 4 additions & 0 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
if right.op == .amp && right.right is ast.StructInit {
right_type = c.expr(right)
}
} else if mut right is ast.Ident {
if right.kind == .function {
c.expr(right)
}
}
if right.is_auto_deref_var() {
left_type = ast.mktyp(right_type.deref())
Expand Down
11 changes: 9 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3234,6 +3234,12 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
return typ
} else if node.kind == .function {
info := node.info as ast.IdentFn
if func := c.table.find_fn(node.name) {
if func.generic_names.len > 0 {
concrete_types := node.concrete_types.map(c.unwrap_generic(it))
c.table.register_fn_concrete_types(func.fkey(), concrete_types)
}
}
return info.typ
} else if node.kind == .unresolved {
// first use
Expand Down Expand Up @@ -3390,11 +3396,12 @@ fn (mut c Checker) ident(mut node ast.Ident) 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,
node.concrete_types)
concrete_types)
{
fn_type = typ_
c.table.register_fn_concrete_types(func.fkey(), node.concrete_types)
c.table.register_fn_concrete_types(func.fkey(), concrete_types)
}
}
node.name = name
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ fn (mut p Parser) is_following_concrete_types() bool {
} else if cur_tok.kind == .rsbr {
break
} else if cur_tok.kind == .name {
if !(cur_tok.lit.len > 1 && p.is_typename(cur_tok)) {
if !(p.is_typename(cur_tok) && !(cur_tok.lit.len == 1 && !cur_tok.lit[0].is_capital())) {
return false
}
} else if cur_tok.kind != .comma {
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/generics_fn_variable_2_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn g[T]() u32 {
return sizeof(T)
}

fn f[T]() u32 {
p := g[T]
return p()
}

fn test_generic_fn_variable() {
r1 := f[int]()
println(r1)
assert r1 == 4

r2 := f[string]()
println(r2)
assert r2 == 16

r3 := f[f64]()
println(r3)
assert r3 == 8

r4 := f[bool]()
println(r4)
assert r4 == 1
}