authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-05-18 23:00:35+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-18 17:00:35-04:00
log3d64ed0353ba7ec1ca46f4779fe5d32af8d17358
tree71f63ffcc5da6ea49c80d3aefa095b9178dac57b
parent7cf2cbb33ef34c1d211135f56d30fe23b6cacd42
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

make `@trap` return unreachable/noreturn (#15749)

`@trap` is a special function that we know never returns so it should behave just like `@panic` and `@compileError` do currently and cause the "unreachable code" + "control flow is diverted here" compile error. Currently, `@trap(); @trap();` does not cause this error. Now it does.

3 files changed, 24 insertions(+), 2 deletions(-)

src/AstGen.zig+1-1
...@@ -8324,7 +8324,7 @@ fn builtinCall(...@@ -8324,7 +8324,7 @@ fn builtinCall(
8324 .trap => {8324 .trap => {
8325 try emitDbgNode(gz, node);8325 try emitDbgNode(gz, node);
8326 _ = try gz.addNode(.trap, node);8326 _ = try gz.addNode(.trap, node);
8327 return rvalue(gz, ri, .void_value, node);8327 return rvalue(gz, ri, .unreachable_value, node);
8328 },8328 },
8329 .error_to_int => {8329 .error_to_int => {
8330 const operand = try expr(gz, scope, .{ .rl = .none }, params[0]);8330 const operand = try expr(gz, scope, .{ .rl = .none }, params[0]);
test/behavior/builtin_functions_returning_void_or_noreturn.zig-1
...@@ -26,5 +26,4 @@ test {...@@ -26,5 +26,4 @@ test {
26 try testing.expectEqual({}, @setEvalBranchQuota(0));26 try testing.expectEqual({}, @setEvalBranchQuota(0));
27 try testing.expectEqual({}, @setFloatMode(.Optimized));27 try testing.expectEqual({}, @setFloatMode(.Optimized));
28 try testing.expectEqual({}, @setRuntimeSafety(true));28 try testing.expectEqual({}, @setRuntimeSafety(true));
29 try testing.expectEqual(noreturn, @TypeOf(if (true) @trap() else {}));
30}29}
test/cases/compile_errors/noreturn_builtins_divert_control_flow.zig created+23
...@@ -0,0 +1,23 @@
1export fn entry1() void {
2 @trap();
3 @trap();
4}
5export fn entry2() void {
6 @panic("");
7 @panic("");
8}
9export fn entry3() void {
10 @compileError("");
11 @compileError("");
12}
13
14// error
15// backend=stage2
16// target=native
17//
18// :3:5: error: unreachable code
19// :2:5: note: control flow is diverted here
20// :7:5: error: unreachable code
21// :6:5: note: control flow is diverted here
22// :11:5: error: unreachable code
23// :10:5: note: control flow is diverted here