From ce6bc2c26d40c5e16f759da49d471b2f8e04d79a Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 28 Jun 2022 16:06:25 +0800 Subject: [PATCH] checker: handle `void` in struct field init (fix #13944) (#14876) --- vlib/v/checker/struct.v | 3 +++ .../struct_field_init_with_void_expr_err.out | 7 ++++++ .../struct_field_init_with_void_expr_err.vv | 25 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 vlib/v/checker/tests/struct_field_init_with_void_expr_err.out create mode 100644 vlib/v/checker/tests/struct_field_init_with_void_expr_err.vv diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index b285bbe1b1..ea9622fcd2 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -377,6 +377,9 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type { expected_type = field_info.typ c.expected_type = expected_type expr_type = c.expr(field.expr) + if expr_type == ast.void_type { + c.error('`$field.expr` (no value) used as value', field.pos) + } if !field_info.typ.has_flag(.optional) { expr_type = c.check_expr_opt_call(field.expr, expr_type) } diff --git a/vlib/v/checker/tests/struct_field_init_with_void_expr_err.out b/vlib/v/checker/tests/struct_field_init_with_void_expr_err.out new file mode 100644 index 0000000000..d04ad4d646 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_init_with_void_expr_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/struct_field_init_with_void_expr_err.vv:19:3: error: `app.test_func()` (no value) used as value + 17 | name: 'add' + 18 | description: 'Add something.' + 19 | execute: app.test_func() + | ~~~~~~~~~~~~~~~~~~~~~~~~ + 20 | } + 21 | diff --git a/vlib/v/checker/tests/struct_field_init_with_void_expr_err.vv b/vlib/v/checker/tests/struct_field_init_with_void_expr_err.vv new file mode 100644 index 0000000000..82288e681a --- /dev/null +++ b/vlib/v/checker/tests/struct_field_init_with_void_expr_err.vv @@ -0,0 +1,25 @@ +import cli { Command } +import os + +struct App {} + +fn (a App) test_func() {} + +fn main() { + mut cmd := Command{ + name: 'cli' + version: '0.0.1' + } + + app := App{} + + mut add := Command{ + name: 'add' + description: 'Add something.' + execute: app.test_func() + } + + cmd.add_command(add) + cmd.setup() + cmd.parse(os.args) +}