From dd7f3a73f36f064b991c7abe39797b82eb8d81da Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Wed, 4 Oct 2023 11:58:20 +0530 Subject: [PATCH] checker: disallow printing variadic expansions of arrays: `print(...a)`, `println(...a)`, where a is an array (fix #19490) (#19503) --- vlib/v/checker/fn.v | 2 ++ vlib/v/checker/tests/variadic_value_print_err.out | 4 ++++ vlib/v/checker/tests/variadic_value_print_err.vv | 2 ++ 3 files changed, 8 insertions(+) create mode 100644 vlib/v/checker/tests/variadic_value_print_err.out create mode 100644 vlib/v/checker/tests/variadic_value_print_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 0ce523e5e..b5815a544 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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) } c.fail_if_unreadable(arg.expr, arg.typ, 'argument to print') c.inside_casting_to_str = false diff --git a/vlib/v/checker/tests/variadic_value_print_err.out b/vlib/v/checker/tests/variadic_value_print_err.out new file mode 100644 index 000000000..9948dcdb6 --- /dev/null +++ b/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) + | ~~~~~~~~~~~~~ diff --git a/vlib/v/checker/tests/variadic_value_print_err.vv b/vlib/v/checker/tests/variadic_value_print_err.vv new file mode 100644 index 000000000..0b125f4db --- /dev/null +++ b/vlib/v/checker/tests/variadic_value_print_err.vv @@ -0,0 +1,2 @@ +a := [1, 2, 3] +println(...a) -- 2.39.5