diff --git a/vlib/v/checker/tests/struct_short_init_warning.out b/vlib/v/checker/tests/struct_short_init_warning.out new file mode 100644 index 0000000000..41daa52c03 --- /dev/null +++ b/vlib/v/checker/tests/struct_short_init_warning.out @@ -0,0 +1,13 @@ +vlib/v/checker/tests/struct_short_init_warning.vv:6:9: warning: short struct initalization is deprecated, use explicit struct name + 4 | + 5 | fn f(foo Foo) Foo { + 6 | return {v: foo.v} + | ~~~~~~~~~~ + 7 | } + 8 | +vlib/v/checker/tests/struct_short_init_warning.vv:10:4: warning: short struct initalization is deprecated, use explicit struct name + 8 | + 9 | fn main() { + 10 | f({v: 10}) + | ~~~~~~~ + 11 | } diff --git a/vlib/v/checker/tests/struct_short_init_warning.vv b/vlib/v/checker/tests/struct_short_init_warning.vv new file mode 100644 index 0000000000..74bd02ff10 --- /dev/null +++ b/vlib/v/checker/tests/struct_short_init_warning.vv @@ -0,0 +1,11 @@ +struct Foo { + v int +} + +fn f(foo Foo) Foo { + return {v: foo.v} +} + +fn main() { + f({v: 10}) +} diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index fc10e37e66..746e928764 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -312,9 +312,9 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { node = p.assoc() } else if (p.tok.kind == .name && p.peek_tok.kind == .colon) || p.tok.kind in [.rcbr, .comment, .ellipsis] { - p.warn_with_pos('short struct initalization is deprecated, use explicit struct name', - p.prev_tok.position()) node = p.struct_init(true) // short_syntax: true + p.warn_with_pos('short struct initalization is deprecated, use explicit struct name', + node.position()) } else if p.tok.kind == .name { p.next() return p.error_with_pos('unexpected $p.tok, expecting `:` after struct field name', diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 0471ca25e6..f282b5168b 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -329,7 +329,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { - first_pos := p.tok.position() + first_pos := (if short_syntax && p.prev_tok.kind == .lcbr { p.prev_tok } else { p.tok }).position() typ := if short_syntax { ast.void_type } else { p.parse_type() } p.expr_mod = '' // sym := p.table.get_type_symbol(typ)