diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 16eb9fba997491..b0a208e7fa4d06 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -53,7 +53,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr { } else if p.tok.kind == .question && p.peek_tok.kind == .amp { node = p.prefix_expr() } else if p.inside_for_expr && p.tok.kind == .name && p.tok.lit[0].is_capital() - && p.peek_tok.kind == .lcbr && p.peek_token(2).kind == .name { + && p.peek_tok.kind == .lcbr && p.peek_token(2).kind in [.rcbr, .name] { node = p.struct_init(p.mod + '.' + p.tok.lit, .normal, false) } else { if p.inside_comptime_if && p.is_generic_name() && p.peek_tok.kind != .dot { diff --git a/vlib/v/tests/struct_init_on_for_expr_test.v b/vlib/v/tests/struct_init_on_for_expr_test.v index 4843ada1be783d..03077ebb05d05a 100644 --- a/vlib/v/tests/struct_init_on_for_expr_test.v +++ b/vlib/v/tests/struct_init_on_for_expr_test.v @@ -11,7 +11,7 @@ fn (mut iter Iterator) next() ?int { return none } -fn test_main() { +fn test_iterator_with_field_init() { for k, x in Iterator{ counter: 10 } { @@ -24,3 +24,24 @@ fn test_main() { } } } + +// + +struct OddNumberIterator { +mut: + current i64 +} + +fn (mut i OddNumberIterator) next() ?i64 { + i.current += 2 + return i.current + 1 +} + +fn test_iterator_without_field_init() { + for x in OddNumberIterator{} { + dump(x) + if x > 10 { + break + } + } +}