From 529f46d8087d4ffde8f2ba594f06aa64f5c9ca87 Mon Sep 17 00:00:00 2001 From: Enzo Date: Sun, 3 Jan 2021 15:45:39 +0100 Subject: [PATCH] checker: don't panic on non-array decomposition (#7836) --- vlib/v/checker/checker.v | 5 +++-- vlib/v/checker/tests/decompose_type_err.out | 6 ++++++ vlib/v/checker/tests/decompose_type_err.vv | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/decompose_type_err.out create mode 100644 vlib/v/checker/tests/decompose_type_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6b7f5b8755..5f3056c431 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -398,7 +398,7 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { field.type_pos) } // Separate error condition for `any_int` and `any_float` because `util.suggestion` may give different - // suggestions due to f32 comparision issue. + // suggestions due to f32 comparision issue. if sym.kind in [.any_int, .any_float] { msg := if sym.kind == .any_int { 'unknown type `$sym.name`.\nDid you mean `int`?' @@ -3049,7 +3049,8 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { typ := c.expr(node.expr) type_sym := c.table.get_type_symbol(typ) if type_sym.kind != .array { - c.error('expected array', node.pos) + c.error('decomposition can only be used on arrays', node.expr.position()) + return table.void_type } array_info := type_sym.info as table.Array elem_type := array_info.elem_type.set_flag(.variadic) diff --git a/vlib/v/checker/tests/decompose_type_err.out b/vlib/v/checker/tests/decompose_type_err.out new file mode 100644 index 0000000000..03e6154fc1 --- /dev/null +++ b/vlib/v/checker/tests/decompose_type_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/decompose_type_err.vv:4:10: error: decomposition can only be used on arrays + 2 | + 3 | fn main() { + 4 | varargs(123...) + | ~~~ + 5 | } diff --git a/vlib/v/checker/tests/decompose_type_err.vv b/vlib/v/checker/tests/decompose_type_err.vv new file mode 100644 index 0000000000..9e4de49855 --- /dev/null +++ b/vlib/v/checker/tests/decompose_type_err.vv @@ -0,0 +1,5 @@ +fn varargs(a ...int) { println(a) } + +fn main() { + varargs(123...) +}