zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 490cafe2c5a62585fd80245a0f6fa5cff4f52dba (tree)
parent f4bb8be9fc8766fec93618f89552c28d3f0a201b
Author: LemonBoy <thatlemon@gmail.com>
Date:   Sun, 25 Oct 2020 23:17:32 +0100

stage1: Error out when trying to execute `unreachable`

Closes #6802

Diffstat:
Mdoc/langref.html.in | 2+-
Msrc/stage1/ir.cpp | 5+++++
Mtest/compile_errors.zig | 17++++++++++++++++-
3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/doc/langref.html.in b/doc/langref.html.in @@ -8675,7 +8675,7 @@ test "safety check" { {#code_end#} {#header_open|Reaching Unreachable Code#} <p>At compile-time:</p> - {#code_begin|test_err|unable to evaluate constant expression#} + {#code_begin|test_err|reached unreachable code#} comptime { assert(false); } diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp @@ -21592,6 +21592,11 @@ static IrInstGen *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstSrcCondBr static IrInstGen *ir_analyze_instruction_unreachable(IrAnalyze *ira, IrInstSrcUnreachable *unreachable_instruction) { + if (ir_should_inline(ira->old_irb.exec, unreachable_instruction->base.base.scope)) { + ir_add_error(ira, &unreachable_instruction->base.base, buf_sprintf("reached unreachable code")); + return ir_unreach_error(ira); + } + IrInstGen *result = ir_build_unreachable_gen(ira, &unreachable_instruction->base.base); return ir_finish_anal(ira, result); } diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -2,6 +2,21 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("unreachable executed at comptime", + \\fn foo(comptime x: i32) i32 { + \\ comptime { + \\ if (x >= 0) return -x; + \\ unreachable; + \\ } + \\} + \\export fn entry() void { + \\ _ = foo(-42); + \\} + , &[_][]const u8{ + "tmp.zig:4:9: error: reached unreachable code", + "tmp.zig:8:12: note: called from here", + }); + cases.add("indexing a undefined slice at comptime", \\comptime { \\ var slice: []u8 = undefined; @@ -6208,7 +6223,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ if (!ok) unreachable; \\} , &[_][]const u8{ - "tmp.zig:10:14: error: unable to evaluate constant expression", + "tmp.zig:10:14: error: reached unreachable code", "tmp.zig:6:20: note: referenced here", });