Skip to content

Commit

Permalink
checker: fix missing check for empty noreturn fn (fix #22468) (#22474)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 10, 2024
1 parent 1a145bf commit 3af35ee
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
mut pos := node.pos
mut is_valid_end_of_noreturn_fn := false
if node.stmts.len != 0 {
mut is_valid_end_of_noreturn_fn := false
last_stmt := node.stmts.last()
match last_stmt {
ast.ExprStmt {
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/empty_fn_noreturn_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/empty_fn_noreturn_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@[noreturn]
fn f() {
}
1 change: 1 addition & 0 deletions vlib/v/tests/reflection_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn test2(arg []string) {}

@[noreturn]
fn test3(a reflection.Function) {
panic('foo')
}

fn test_module_existing() {
Expand Down

0 comments on commit 3af35ee

Please sign in to comment.