Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix missing check for empty noreturn fn #22474

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,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 @@ -392,11 +393,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')
spytheman marked this conversation as resolved.
Show resolved Hide resolved
}

fn test_module_existing() {
Expand Down
Loading