From f40090e8ff88d4e9ee9a88431655ece547f70c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Fri, 23 Jul 2021 00:13:36 +0200 Subject: [PATCH] checker: fix break inside anon fn (#10914) --- vlib/v/checker/checker.v | 2 +- vlib/v/checker/tests/break_anon_fn_err.out | 7 +++++++ vlib/v/checker/tests/break_anon_fn_err.vv | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/break_anon_fn_err.out create mode 100644 vlib/v/checker/tests/break_anon_fn_err.vv 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 + }() + } +}