Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: disallow iterating through .params if not a function #22428

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/comptime_param_not_fn_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
8 changes: 8 additions & 0 deletions vlib/v/checker/tests/comptime_param_not_fn_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Test {
}

fn main() {
$for f in Test.params {
println(f)
}
}
Loading