From 3e084871985474c5b85cebe05dbc3d8608ba5c6b Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+tobealive@users.noreply.github.com> Date: Sat, 27 May 2023 00:55:14 +0200 Subject: [PATCH] checker: disallow multiple return values in const declarations (#18273) --- vlib/v/checker/checker.v | 7 +++++++ vlib/v/checker/tests/const_decl_multi_return_err.out | 7 +++++++ vlib/v/checker/tests/const_decl_multi_return_err.vv | 12 ++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 vlib/v/checker/tests/const_decl_multi_return_err.out create mode 100644 vlib/v/checker/tests/const_decl_multi_return_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7334029df9..fbf1aa9347 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1531,6 +1531,13 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) { } c.error('duplicate const `${field.name}`', name_pos) } + if field.expr is ast.CallExpr { + sym := c.table.sym(c.check_expr_opt_call(field.expr, c.expr(field.expr))) + if sym.kind == .multi_return { + c.error('const declarations do not support multiple return values yet', + field.expr.pos) + } + } c.const_names << field.name } for i, mut field in node.fields { diff --git a/vlib/v/checker/tests/const_decl_multi_return_err.out b/vlib/v/checker/tests/const_decl_multi_return_err.out new file mode 100644 index 0000000000..8e56bee1c0 --- /dev/null +++ b/vlib/v/checker/tests/const_decl_multi_return_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/const_decl_multi_return_err.vv:6:6: error: const declarations do not support multiple return values yet + 4 | + 5 | const ( + 6 | a = foo() + | ~~~~~ + 7 | ) + 8 | \ No newline at end of file diff --git a/vlib/v/checker/tests/const_decl_multi_return_err.vv b/vlib/v/checker/tests/const_decl_multi_return_err.vv new file mode 100644 index 0000000000..663949e520 --- /dev/null +++ b/vlib/v/checker/tests/const_decl_multi_return_err.vv @@ -0,0 +1,12 @@ +fn foo() (int, int, int) { + return 1, 2, 3 +} + +const ( + a = foo() +) + + +fn main() { + println("$a") +}