Skip to content

Commit

Permalink
parser,checker: allow a goto label right after return
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Oct 28, 2021
1 parent 8cd01e0 commit 1b6ccca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -5162,6 +5162,12 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
}
}
c.stmt(stmt)
if stmt is ast.GotoLabel {
unreachable = token.Position{
line_nr: -1
}
c.scope_returns = false
}
}
if unreachable.line_nr >= 0 {
c.error('unreachable code', unreachable)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ fn (mut p Parser) return_stmt() ast.Return {
p.next()
// no return
mut comments := p.eat_comments()
if p.tok.kind == .rcbr {
if p.tok.kind == .rcbr || ( p.tok.kind == .name && p.peek_tok.kind == .colon ) {
return ast.Return{
comments: comments
pos: first_pos
Expand Down
25 changes: 25 additions & 0 deletions vlib/v/tests/goto_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,28 @@ fn test_goto() {
}
assert i == 3
}

pub fn test_goto_after_return() {
a, b, c, d := 4, 5, 6, 7
for {
for {
for {
if a == 4 {
if b == 5 {
if c == 6 {
if d == 7 {
unsafe {
goto finally_ok
}
}
}
}
}
}
}
}
assert false
return
finally_ok:
assert true
}

0 comments on commit 1b6ccca

Please sign in to comment.