Skip to content

Commit

Permalink
parser: fix parsing map value inside or expr
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 8, 2024
1 parent b8c649b commit 41b782c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
19 changes: 14 additions & 5 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -950,11 +950,20 @@ fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
match p.tok.kind {
.lcbr {
mut pos := p.tok.pos()
stmts := p.parse_block()
pos.last_line = p.prev_tok.line_nr
return ast.Block{
stmts: stmts
pos: pos
if p.peek_token(2).kind == .colon {
expr := p.expr(0)
// `{ 'abc' : 22 }`
return ast.ExprStmt{
expr: expr
pos: pos
}
} else {
stmts := p.parse_block()
pos.last_line = p.prev_tok.line_nr
return ast.Block{
stmts: stmts
pos: pos
}
}
}
.key_assert {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MapString({'abc': 'String Error'})
22 changes: 22 additions & 0 deletions vlib/v/slow_tests/inout/printing_option_or_expr_with_map_val.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
struct MyError {
msg string
code int
}

fn (my MyError) msg() string {
return my.msg
}

fn (my MyError) code() int {
return my.code
}

type MapString = map[string]string | int

fn f() ?MapString {
return MyError{msg: 'String Error'}
}

fn main() {
println(f() or { { 'abc': err.msg() } })
}

0 comments on commit 41b782c

Please sign in to comment.