From 9190f586a558d89f869542f838c0d2ebe43f8e35 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 12 Jun 2026 04:58:17 +0300 Subject: [PATCH] checker: reject optional array args for plain arrays (#27425) --- vlib/v/checker/fn.v | 8 ++++++-- vlib/v/checker/tests/option_array_arg_err.out | 13 +++++++++++++ vlib/v/checker/tests/option_array_arg_err.vv | 9 +++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/option_array_arg_err.out create mode 100644 vlib/v/checker/tests/option_array_arg_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 2cdea6924..815f77b92 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2574,7 +2574,9 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. } else { arg_typ.nr_muls() } - if param_nr_muls == arg_nr_muls + if param.typ.has_flag(.option) == arg_typ.has_flag(.option) + && param.typ.has_flag(.result) == arg_typ.has_flag(.result) + && param_nr_muls == arg_nr_muls && param_typ_sym.info.nr_dims == arg_typ_sym.info.nr_dims && param_elem_type == arg_elem_type { continue @@ -3865,7 +3867,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool) if param_typ_sym.info is ast.Array && arg_typ_sym.info is ast.Array { param_elem_type := c.table.unaliased_type(param_typ_sym.info.elem_type) arg_elem_type := c.table.unaliased_type(arg_typ_sym.info.elem_type) - if exp_arg_typ.nr_muls() == got_arg_typ.nr_muls() + if exp_arg_typ.has_flag(.option) == got_arg_typ.has_flag(.option) + && exp_arg_typ.has_flag(.result) == got_arg_typ.has_flag(.result) + && exp_arg_typ.nr_muls() == got_arg_typ.nr_muls() && param_typ_sym.info.nr_dims == arg_typ_sym.info.nr_dims && param_elem_type == arg_elem_type { continue diff --git a/vlib/v/checker/tests/option_array_arg_err.out b/vlib/v/checker/tests/option_array_arg_err.out new file mode 100644 index 000000000..cb9ec6df4 --- /dev/null +++ b/vlib/v/checker/tests/option_array_arg_err.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/option_array_arg_err.vv:6:12: error: cannot use `?[]u8` as `[]u8`, it must be unwrapped first in argument 1 to `use_bytes` + 4 | + 5 | fn main() { + 6 | use_bytes(?[]u8(none)) + | ~~~~~~~~~~~ + 7 | maybe := ?[]u8(none) + 8 | use_bytes(maybe) +vlib/v/checker/tests/option_array_arg_err.vv:8:12: error: cannot use `?[]u8` as `[]u8`, it must be unwrapped first in argument 1 to `use_bytes` + 6 | use_bytes(?[]u8(none)) + 7 | maybe := ?[]u8(none) + 8 | use_bytes(maybe) + | ~~~~~ + 9 | } diff --git a/vlib/v/checker/tests/option_array_arg_err.vv b/vlib/v/checker/tests/option_array_arg_err.vv new file mode 100644 index 000000000..f52a2c0f9 --- /dev/null +++ b/vlib/v/checker/tests/option_array_arg_err.vv @@ -0,0 +1,9 @@ +fn use_bytes(data []u8) int { + return data.len +} + +fn main() { + use_bytes(?[]u8(none)) + maybe := ?[]u8(none) + use_bytes(maybe) +} -- 2.39.5