From 7ee7932f5e1be2c969110fc20210a04ba195014f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 8 Jul 2022 21:09:02 +0300 Subject: [PATCH] anon expr --- vlib/v/checker/struct.v | 9 +++++++-- vlib/v/parser/expr.v | 5 +++++ vlib/v/tests/struct_test.v | 5 +++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 260b1fe733..d8b3877c02 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -267,8 +267,13 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type { && c.table.cur_concrete_types.len == 0 { pos := type_sym.name.last_index('.') or { -1 } first_letter := type_sym.name[pos + 1] - if !first_letter.is_capital() && type_sym.kind != .placeholder { - c.error('cannot initialize builtin type `$type_sym.name`', node.pos) + if !first_letter.is_capital() && type_sym.kind != .placeholder + && !type_sym.name.starts_with('_VAnonStruct') { + s := type_sym.name + x := s.starts_with('_') + + c.error('cannot initialize builtin type1 $x "$s" "${s[..4]}" `$type_sym.name`', + node.pos) } } if type_sym.kind == .sum_type && node.fields.len == 1 { diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index c4aaa0b2ff..d174c247e4 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -325,6 +325,11 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { node = p.map_init() p.check(.rcbr) } + .key_struct { + // Anonymous struct + p.next() + return p.struct_init('', false) + } .key_fn { if p.expecting_type { // Anonymous function type diff --git a/vlib/v/tests/struct_test.v b/vlib/v/tests/struct_test.v index d73abf40fa..f8eee4fe28 100644 --- a/vlib/v/tests/struct_test.v +++ b/vlib/v/tests/struct_test.v @@ -425,9 +425,10 @@ struct Book { } fn test_anon() { - empty_book := Book{} // author:struct{'sdf', 23}} + empty_book := Book{} assert empty_book.author.age == 0 assert empty_book.author.name == '' - println(empty_book.author.age) + + book := Book{ author:struct{'sdf', 23}} }