| author | |
| committer | |
| log | e41bc640c6c4277385236d3dd90b4db566550509 |
| tree | c3ae2d90492cac331e3546a9ffc5115faf078333 |
| parent | 98508a12cfae15a895d3e829539f907e9d88d0b6 |
If we use the discard result location any break with a value will be
ignored and not checked for usage.
Closes https://github.com/ziglang/zig/issues/14684.3 files changed, 43 insertions(+), 2 deletions(-)
src/AstGen.zig+2-2| ... | ... | @@ -2342,10 +2342,10 @@ fn blockExprStmts(gz: *GenZir, parent_scope: *Scope, statements: []const Ast.Nod |
| 2342 | 2342 | |
| 2343 | 2343 | .while_simple, |
| 2344 | 2344 | .while_cont, |
| 2345 | .@"while", => _ = try whileExpr(gz, scope, .{ .rl = .discard }, inner_node, tree.fullWhile(inner_node).?, true), | |
| 2345 | .@"while", => _ = try whileExpr(gz, scope, .{ .rl = .none }, inner_node, tree.fullWhile(inner_node).?, true), | |
| 2346 | 2346 | |
| 2347 | 2347 | .for_simple, |
| 2348 | .@"for", => _ = try forExpr(gz, scope, .{ .rl = .discard }, inner_node, tree.fullFor(inner_node).?, true), | |
| 2348 | .@"for", => _ = try forExpr(gz, scope, .{ .rl = .none }, inner_node, tree.fullFor(inner_node).?, true), | |
| 2349 | 2349 | |
| 2350 | 2350 | else => noreturn_src_node = try unusedResultExpr(gz, scope, inner_node), |
| 2351 | 2351 | // zig fmt: on |
test/cases/compile_errors/for_loop_break_value_ignored.zig created+15| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | fn returns() usize { | |
| 2 | return 2; | |
| 3 | } | |
| 4 | ||
| 5 | export fn f1() void { | |
| 6 | for ("hello") |_| { | |
| 7 | break returns(); | |
| 8 | } | |
| 9 | } | |
| 10 | ||
| 11 | // error | |
| 12 | // backend=stage2 | |
| 13 | // target=native | |
| 14 | // | |
| 15 | // :6:5: error: incompatible types: 'usize' and 'void' |
test/cases/compile_errors/while_loop_break_value_ignored.zig created+26| ... | ... | @@ -0,0 +1,26 @@ |
| 1 | fn returns() usize { | |
| 2 | return 2; | |
| 3 | } | |
| 4 | ||
| 5 | export fn f1() void { | |
| 6 | var a: bool = true; | |
| 7 | while (a) { | |
| 8 | break returns(); | |
| 9 | } | |
| 10 | } | |
| 11 | ||
| 12 | export fn f2() void { | |
| 13 | var x: bool = true; | |
| 14 | outer: while (x) { | |
| 15 | while (x) { | |
| 16 | break :outer returns(); | |
| 17 | } | |
| 18 | } | |
| 19 | } | |
| 20 | ||
| 21 | // error | |
| 22 | // backend=stage2 | |
| 23 | // target=native | |
| 24 | // | |
| 25 | // :7:5: error: incompatible types: 'usize' and 'void' | |
| 26 | // :14:12: error: incompatible types: 'usize' and 'void' |