diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 286c35d34abfaa..2fc0da096ce766 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -326,11 +326,17 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { c.pop_comptime_info() } } else if node.kind == .params { - c.push_new_comptime_info() - c.comptime.inside_comptime_for = true - c.comptime.comptime_for_method_param_var = node.val_var - c.stmts(mut node.stmts) - c.pop_comptime_info() + if sym.kind == .function { + c.push_new_comptime_info() + c.comptime.inside_comptime_for = true + c.comptime.comptime_for_method_param_var = node.val_var + c.stmts(mut node.stmts) + c.pop_comptime_info() + } else { + c.error('iterating over `.params` is supported only for functions, and `${sym.name}` is not a function', + node.typ_pos) + return + } } else if node.kind == .variants { if c.variant_data_type == 0 { c.variant_data_type = ast.idx_to_type(c.table.find_type_idx('VariantData')) 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) + } +}