checker: simplify noreturn.v

pull/11537/head
Delyan Angelov 2021-09-18 13:42:46 +03:00
parent 396eede4db
commit 0a7fb34613
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 24 additions and 23 deletions

View File

@ -9,38 +9,39 @@ fn (mut c Checker) check_noreturn_fn_decl(mut node ast.FnDecl) {
if node.no_body { if node.no_body {
return return
} }
if node.return_type != ast.void_type {
c.error('[noreturn] functions cannot have return types', node.pos)
}
if uses_return_stmt(node.stmts) { if uses_return_stmt(node.stmts) {
c.error('[noreturn] functions cannot use return statements', node.pos) c.error('[noreturn] functions cannot use return statements', node.pos)
} }
if node.return_type != ast.void_type { if node.stmts.len != 0 {
c.error('[noreturn] functions cannot have return types', node.pos) mut is_valid_end_of_noreturn_fn := false
} else { last_stmt := node.stmts.last()
if node.stmts.len != 0 { match last_stmt {
mut is_valid_end_of_noreturn_fn := false ast.ExprStmt {
last_stmt := node.stmts.last() if last_stmt.expr is ast.CallExpr {
match last_stmt { if last_stmt.expr.should_be_skipped {
ast.ExprStmt { c.error('[noreturn] functions cannot end with a skippable `[if ..]` call',
if last_stmt.expr is ast.CallExpr { last_stmt.pos)
if last_stmt.expr.should_be_skipped { return
c.error('[noreturn] functions cannot end with a skippable `[if ..]` call',
last_stmt.pos)
}
if last_stmt.expr.is_noreturn {
is_valid_end_of_noreturn_fn = true
}
} }
} if last_stmt.expr.is_noreturn {
ast.ForStmt {
if last_stmt.is_inf && last_stmt.stmts.len == 0 {
is_valid_end_of_noreturn_fn = true is_valid_end_of_noreturn_fn = true
} }
} }
else {}
} }
if !is_valid_end_of_noreturn_fn { ast.ForStmt {
c.error('[noreturn] functions should end with a call to another [noreturn] function, or with an infinite `for {}` loop', if last_stmt.is_inf && last_stmt.stmts.len == 0 {
last_stmt.pos) is_valid_end_of_noreturn_fn = true
}
} }
else {}
}
if !is_valid_end_of_noreturn_fn {
c.error('[noreturn] functions should end with a call to another [noreturn] function, or with an infinite `for {}` loop',
last_stmt.pos)
return
} }
} }
} }