diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e0d0ba0555..1b8fafd03d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2135,6 +2135,10 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type { concrete_types << concrete_type } } + if c.cur_fn.cur_concrete_types.len == 0 && has_generic { + c.error('generic fn using generic types cannot be called outside of generic fn', + call_expr.pos) + } if has_generic { mut no_exists := true if c.mod != '' && !fn_name.contains('.') { diff --git a/vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.out b/vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.out new file mode 100644 index 0000000000..ec84b67171 --- /dev/null +++ b/vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.vv:4:2: error: generic fn using generic types cannot be called outside of generic fn + 2 | + 3 | fn main() { + 4 | foo() + | ~~~~~~~~ + 5 | } diff --git a/vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.vv b/vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.vv new file mode 100644 index 0000000000..c8b72c2f86 --- /dev/null +++ b/vlib/v/checker/tests/generics_fn_called_outside_of_generic_fn.vv @@ -0,0 +1,5 @@ +fn foo() {} + +fn main() { + foo() +}