From f4c04844b24277116cdeec42b8508a5c4f6983e3 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 7 Oct 2024 15:08:24 +0530 Subject: [PATCH] checker: disallow iterating through .params, for types that are not a function/method (#22428) --- vlib/v/checker/comptime.v | 5 +++++ vlib/v/checker/tests/comptime_param_not_fn_err.out | 7 +++++++ vlib/v/checker/tests/comptime_param_not_fn_err.vv | 8 ++++++++ 3 files changed, 20 insertions(+) create mode 100644 vlib/v/checker/tests/comptime_param_not_fn_err.out create mode 100644 vlib/v/checker/tests/comptime_param_not_fn_err.vv diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 286c35d34..39f59572d 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 000000000..ab993165d --- /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 000000000..4991ae57d --- /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) + } +} -- 2.39.5