From be16c5b21ddb566e6053db306c49146868a4df45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Sat, 11 Apr 2020 13:56:55 +0200 Subject: [PATCH] parser: better error on short init struct --- vlib/v/parser/parser.v | 7 ++++++- vlib/v/tests/short_struct_param_syntax_test.v | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/short_struct_param_syntax_test.v diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f3105f2cb0..e4bbb7c0b5 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -813,12 +813,17 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr { if p.tok.kind == .string { node = p.map_init() } else { + // it should be a struct if p.peek_tok.kind == .pipe { node = p.assoc() } else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr { node = p.struct_init(true) // short_syntax: true + } else if p.tok.kind == .name { + p.next() + lit := if p.tok.lit != '' { p.tok.lit } else { p.tok.kind.str() } + p.error('unexpected ‘$lit‘, expecting ‘:‘') } else { - p.error('unexpected {') + p.error('unexpected ‘$p.tok.lit‘, expecting struct key') } } p.check(.rcbr) diff --git a/vlib/v/tests/short_struct_param_syntax_test.v b/vlib/v/tests/short_struct_param_syntax_test.v new file mode 100644 index 0000000000..c59a4363ec --- /dev/null +++ b/vlib/v/tests/short_struct_param_syntax_test.v @@ -0,0 +1,18 @@ +struct TOptions { + a int +} + +fn t(options TOptions) bool { + if options.a == 1 { + return true + } + return false +} + +fn test_short_struct_as_parameter(){ + if t({a: 1}) { + assert true + return + } + assert false +}