Skip to content

Commit

Permalink
checker: fix generic var inferring to be passed to []T (fix #23315) (
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 31, 2024
1 parent 4b6b852 commit 7b9b3dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1992,7 +1992,12 @@ fn (mut c Checker) resolve_comptime_args(func &ast.Fn, node_ ast.CallExpr, concr
}
}
} else if call_arg.expr.obj.ct_type_var == .generic_var {
mut ctyp := c.type_resolver.get_type(call_arg.expr)
mut ctyp := c.unwrap_generic(c.type_resolver.get_type(call_arg.expr))
cparam_type_sym := c.table.sym(c.unwrap_generic(ctyp))
param_typ_sym := c.table.sym(param_typ)
if param_typ_sym.kind == .array && cparam_type_sym.info is ast.Array {
ctyp = cparam_type_sym.info.elem_type
}
if node_.args[i].expr.is_auto_deref_var() {
ctyp = ctyp.deref()
}
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/generics/generic_fn_array_infer_var_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fn function_that_receives_an_array_of_generic_type[T](array []T) {
assert array.len == 0
}

fn function_that_returns_an_array_of_generic_type[T]() []T {
return []
}

fn func[T]() {
res := function_that_returns_an_array_of_generic_type[T]()
function_that_receives_an_array_of_generic_type(res)
assert res.len == 0
function_that_receives_an_array_of_generic_type[T](res)
}

fn test_main() {
func[string]()
func[int]()
func[f64]()
}

0 comments on commit 7b9b3dd

Please sign in to comment.