1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser,checker: allow a goto label right after return

This commit is contained in:
Delyan Angelov 2021-10-28 10:40:18 +03:00
parent 8cd01e0eac
commit 1b6cccaf6d
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 32 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
}