From 0d53705776fa473fa8711d1c2bdacbd52b7183d1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 7 Oct 2021 02:04:33 +0800 Subject: [PATCH] v.checker: check fn returning void type (fix #12076) (#12078) --- vlib/v/checker/checker.v | 3 +++ vlib/v/checker/tests/return_void_type_err.out | 6 ++++++ vlib/v/checker/tests/return_void_type_err.vv | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/return_void_type_err.out create mode 100644 vlib/v/checker/tests/return_void_type_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 44bfbeb692..67326f7a73 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3706,6 +3706,9 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) { mut got_types := []ast.Type{} for expr in node.exprs { typ := c.expr(expr) + if typ == ast.void_type { + c.error('`$expr` used as value', node.pos) + } // Unpack multi return types sym := c.table.get_type_symbol(typ) if sym.kind == .multi_return { diff --git a/vlib/v/checker/tests/return_void_type_err.out b/vlib/v/checker/tests/return_void_type_err.out new file mode 100644 index 0000000000..19393f164e --- /dev/null +++ b/vlib/v/checker/tests/return_void_type_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/return_void_type_err.vv:2:2: error: `println(msg)` used as value + 1 | fn hello(msg string) ? { + 2 | return println(msg) + | ~~~~~~~~~~~~~~~~~~~ + 3 | } + 4 | diff --git a/vlib/v/checker/tests/return_void_type_err.vv b/vlib/v/checker/tests/return_void_type_err.vv new file mode 100644 index 0000000000..177da908ca --- /dev/null +++ b/vlib/v/checker/tests/return_void_type_err.vv @@ -0,0 +1,7 @@ +fn hello(msg string) ? { + return println(msg) +} + +fn main() { + hello('test') ? +}