Skip to content

Commit

Permalink
checker: disallow printing variadic expansions of arrays: `print(...a…
Browse files Browse the repository at this point in the history
…)`, `println(...a)`, where a is an array (fix #19490) (#19503)
  • Loading branch information
Delta456 committed Oct 4, 2023
1 parent c79f84d commit dd7f3a7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vlib/v/checker/fn.v
Expand Up @@ -580,6 +580,8 @@ fn (mut c Checker) builtin_args(mut node ast.CallExpr, fn_name string, func ast.
} else if arg.typ == ast.char_type && arg.typ.nr_muls() == 0 {
c.error('`${fn_name}` cannot print type `char` directly, print its address or cast it to an integer instead',
node.pos)
} else if arg.expr is ast.ArrayDecompose {
c.error('`${fn_name}` cannot print variadic values', node.pos)

This comment has been minimized.

Copy link
@danieldaeschle

danieldaeschle Oct 4, 2023

Member

Is this only for println()? What happens with other functions that expect an any parameter?

This comment has been minimized.

Copy link
@Delta456

Delta456 Oct 4, 2023

Author Member

This is what was discussed. I will be handling the rest in upcoming PRs.

}
c.fail_if_unreadable(arg.expr, arg.typ, 'argument to print')
c.inside_casting_to_str = false
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/tests/variadic_value_print_err.out
@@ -0,0 +1,4 @@
vlib/v/checker/tests/variadic_value_print_err.vv:2:1: error: `println` cannot print variadic values
1 | a := [1, 2, 3]
2 | println(...a)
| ~~~~~~~~~~~~~
2 changes: 2 additions & 0 deletions vlib/v/checker/tests/variadic_value_print_err.vv
@@ -0,0 +1,2 @@
a := [1, 2, 3]
println(...a)

0 comments on commit dd7f3a7

Please sign in to comment.