mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add clearer errors for break
/continue
used within a $for
loop (#16600)
This commit is contained in:
parent
ada8643ac5
commit
cf015e5073
@ -1828,7 +1828,11 @@ fn (mut c Checker) branch_stmt(node ast.BranchStmt) {
|
||||
c.error('`${node.kind.str()}` is not allowed in defer statements', node.pos)
|
||||
}
|
||||
if c.in_for_count == 0 {
|
||||
c.error('${node.kind.str()} statement not within a loop', node.pos)
|
||||
if c.inside_comptime_for_field {
|
||||
c.error('${node.kind.str()} is not allowed within a compile-time loop', node.pos)
|
||||
} else {
|
||||
c.error('${node.kind.str()} statement not within a loop', node.pos)
|
||||
}
|
||||
}
|
||||
if node.label.len > 0 {
|
||||
if node.label != c.loop_label {
|
||||
|
@ -0,0 +1,28 @@
|
||||
vlib/v/checker/tests/check_wrong_usage_of_break_and_continue.vv:9:3: error: continue is not allowed within a compile-time loop
|
||||
7 | $for field in Test.fields {
|
||||
8 | println(field)
|
||||
9 | continue
|
||||
| ~~~~~~~~
|
||||
10 | }
|
||||
11 | $for field in Test.fields {
|
||||
vlib/v/checker/tests/check_wrong_usage_of_break_and_continue.vv:13:3: error: break is not allowed within a compile-time loop
|
||||
11 | $for field in Test.fields {
|
||||
12 | println(field)
|
||||
13 | break
|
||||
| ~~~~~
|
||||
14 | }
|
||||
15 | {
|
||||
vlib/v/checker/tests/check_wrong_usage_of_break_and_continue.vv:16:3: error: break statement not within a loop
|
||||
14 | }
|
||||
15 | {
|
||||
16 | break
|
||||
| ~~~~~
|
||||
17 | continue
|
||||
18 | }
|
||||
vlib/v/checker/tests/check_wrong_usage_of_break_and_continue.vv:17:3: error: continue statement not within a loop
|
||||
15 | {
|
||||
16 | break
|
||||
17 | continue
|
||||
| ~~~~~~~~
|
||||
18 | }
|
||||
19 | }
|
@ -0,0 +1,19 @@
|
||||
struct Test {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
fn main() {
|
||||
$for field in Test.fields {
|
||||
println(field)
|
||||
continue
|
||||
}
|
||||
$for field in Test.fields {
|
||||
println(field)
|
||||
break
|
||||
}
|
||||
{
|
||||
break
|
||||
continue
|
||||
}
|
||||
}
|
17
vlib/v/tests/comptime_for_break_test.v
Normal file
17
vlib/v/tests/comptime_for_break_test.v
Normal file
@ -0,0 +1,17 @@
|
||||
struct Test {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
fn test_for() {
|
||||
$for field in Test.fields {
|
||||
for attr in field.attrs {
|
||||
break
|
||||
}
|
||||
}
|
||||
$for field in Test.fields {
|
||||
for attr in field.attrs {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user