Skip to content

Commit

Permalink
parser: fix for x in Iterator{} {, when there are no field initiali…
Browse files Browse the repository at this point in the history
…sations (#21333)
  • Loading branch information
spytheman authored Apr 22, 2024
1 parent 4f0a8b5 commit 227f31b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion vlib/v/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 22 additions & 1 deletion vlib/v/tests/struct_init_on_for_expr_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
} {
Expand All @@ -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
}
}
}

0 comments on commit 227f31b

Please sign in to comment.