1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: handle void in struct field init (fix #13944) (#14876)

This commit is contained in:
yuyi 2022-06-28 16:06:25 +08:00 committed by GitHub
parent a4eb5b6356
commit ce6bc2c26d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -377,6 +377,9 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
expected_type = field_info.typ expected_type = field_info.typ
c.expected_type = expected_type c.expected_type = expected_type
expr_type = c.expr(field.expr) 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) { if !field_info.typ.has_flag(.optional) {
expr_type = c.check_expr_opt_call(field.expr, expr_type) expr_type = c.check_expr_opt_call(field.expr, expr_type)
} }

View File

@ -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 |

View File

@ -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)
}