diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 56a090e75a..d66dabde9f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4614,7 +4614,7 @@ fn (mut c Checker) block(node ast.Block) { } fn (mut c Checker) branch_stmt(node ast.BranchStmt) { - if c.in_for_count == 0 { + if c.in_for_count == 0 || c.inside_anon_fn { c.error('$node.kind.str() statement not within a loop', node.pos) } if node.label.len > 0 { diff --git a/vlib/v/checker/tests/break_anon_fn_err.out b/vlib/v/checker/tests/break_anon_fn_err.out new file mode 100644 index 0000000000..82b7ab5907 --- /dev/null +++ b/vlib/v/checker/tests/break_anon_fn_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/break_anon_fn_err.vv:4:4: error: break statement not within a loop + 2 | for true { + 3 | _ := fn () int { + 4 | break + | ~~~~~ + 5 | return 3 + 6 | }() diff --git a/vlib/v/checker/tests/break_anon_fn_err.vv b/vlib/v/checker/tests/break_anon_fn_err.vv new file mode 100644 index 0000000000..26586e6dac --- /dev/null +++ b/vlib/v/checker/tests/break_anon_fn_err.vv @@ -0,0 +1,8 @@ +fn main() { + for true { + _ := fn () int { + break + return 3 + }() + } +}