diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 6f35f779d3..3f77c785bf 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1397,6 +1397,7 @@ pub fn (mut t Table) resolve_generic_to_concrete(generic_type Type, generic_name } } } + func.name = '' idx := t.find_or_register_fn_type('', func, true, false) return new_type(idx).derive_add_muls(generic_type).clear_flag(.generic) } diff --git a/vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.out b/vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.out new file mode 100644 index 0000000000..52a6d8dcb3 --- /dev/null +++ b/vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.vv:24:37: error: cannot use `fn (int) f64` as `fn (int) Point3D` in argument 2 to `new_array` + 22 | println(good_cloud) + 23 | // this should be a compilation error, because the function signature does not match: + 24 | bad_cloud := new_array(3, fn (idx int) f64 { + | ~~~~~~~~~~~~~~~~~~ + 25 | return 12345 + 26 | }) diff --git a/vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.vv b/vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.vv new file mode 100644 index 0000000000..18f2811cc8 --- /dev/null +++ b/vlib/v/checker/tests/generics_fn_called_fntype_arg_mismatch.vv @@ -0,0 +1,28 @@ +pub type FnArrayInit = fn (idx int) T + +pub fn new_array(len int, initfn FnArrayInit) []T { + mut res := []T{len: len} + for idx in 0 .. res.len { + res[idx] = initfn(idx) + } + return res +} + +struct Point3D { + x f32 + y f32 + z f32 +} + +fn main() { + // this works as expected: + good_cloud := new_array(3, fn (idx int) Point3D { + return Point3D{idx, idx * 10, idx * -10} + }) + println(good_cloud) + // this should be a compilation error, because the function signature does not match: + bad_cloud := new_array(3, fn (idx int) f64 { + return 12345 + }) + println(bad_cloud) +}