Skip to content

Commit

Permalink
checker: disallow iterating through .params if not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Oct 6, 2024
1 parent 2a1c7c2 commit bb59b01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
16 changes: 11 additions & 5 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
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)
}
}

0 comments on commit bb59b01

Please sign in to comment.