1
0
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:
Felipe Pena 2022-12-06 12:27:59 -03:00 committed by GitHub
parent ada8643ac5
commit cf015e5073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 1 deletions

View File

@ -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 {

View File

@ -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 | }

View File

@ -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
}
}

View 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
}
}
}