diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 286c35d34abfaa..39f59572d732a9 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -326,6 +326,11 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { c.pop_comptime_info() } } else if node.kind == .params { + if !(sym.kind == .function || sym.name == 'FunctionData') { + c.error('iterating over `.params` is supported only for functions, and `${sym.name}` is not a function', + node.typ_pos) + return + } c.push_new_comptime_info() c.comptime.inside_comptime_for = true c.comptime.comptime_for_method_param_var = node.val_var diff --git a/vlib/v/checker/tests/comptime_param_not_fn_err.out b/vlib/v/checker/tests/comptime_param_not_fn_err.out new file mode 100644 index 00000000000000..ab993165dd97cb --- /dev/null +++ b/vlib/v/checker/tests/comptime_param_not_fn_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/comptime_param_not_fn_err.vv:5:12: error: iterating over `.params` is supported only for functions, and `Test` is not a function + 3 | + 4 | fn main() { + 5 | $for f in Test.params { + | ~~~~ + 6 | println(f) + 7 | } diff --git a/vlib/v/checker/tests/comptime_param_not_fn_err.vv b/vlib/v/checker/tests/comptime_param_not_fn_err.vv new file mode 100644 index 00000000000000..4991ae57d90e29 --- /dev/null +++ b/vlib/v/checker/tests/comptime_param_not_fn_err.vv @@ -0,0 +1,8 @@ +struct Test { +} + +fn main() { + $for f in Test.params { + println(f) + } +}