checker: fix missing check for empty noreturn fn (fix #22468) (#22474)

This commit is contained in:
Felipe Pena 2024-10-10 14:52:24 -03:00 committed by GitHub
parent 1a145bfff3
commit 3af35eef14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 4 deletions

View File

@ -367,8 +367,9 @@ fn (mut c Checker) check_noreturn_fn_decl(mut node ast.FnDecl) {
if uses_return_stmt(node.stmts) {
c.error('[noreturn] functions cannot use return statements', node.pos)
}
if node.stmts.len != 0 {
mut pos := node.pos
mut is_valid_end_of_noreturn_fn := false
if node.stmts.len != 0 {
last_stmt := node.stmts.last()
match last_stmt {
ast.ExprStmt {
@ -391,11 +392,13 @@ fn (mut c Checker) check_noreturn_fn_decl(mut node ast.FnDecl) {
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
pos = last_stmt.pos
}
}
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',
pos)
}
}
fn uses_return_stmt(stmts []ast.Stmt) bool {

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/empty_fn_noreturn_err.vv:2:1: error: @[noreturn] functions should end with a call to another @[noreturn] function, or with an infinite `for {}` loop
1 | @[noreturn]
2 | fn f() {
| ~~~~~~
3 | }

View File

@ -0,0 +1,3 @@
@[noreturn]
fn f() {
}

View File

@ -21,6 +21,7 @@ fn test2(arg []string) {}
@[noreturn]
fn test3(a reflection.Function) {
panic('foo')
}
fn test_module_existing() {