diff --git a/src/AstGen.zig b/src/AstGen.zig index c9fd82bd5f..006dbc5778 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -740,7 +740,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr _ = try gz.addAsIndex(.{ .tag = .@"unreachable", .data = .{ .@"unreachable" = .{ - .safety = true, + .force_comptime = gz.force_comptime, .src_node = gz.nodeIndexToRelative(node), } }, }); diff --git a/src/Sema.zig b/src/Sema.zig index c9d8f090ae..884f3ab847 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -12337,14 +12337,15 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi } fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index { - const tracy = trace(@src()); - defer tracy.end(); - const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable"; const src = inst_data.src(); + + if (block.is_comptime or inst_data.force_comptime) { + return sema.fail(block, src, "reached unreachable code", .{}); + } try sema.requireRuntimeBlock(block, src); // TODO Add compile error for @optimizeFor occurring too late in a scope. - try block.addUnreachable(src, inst_data.safety); + try block.addUnreachable(src, true); return always_noreturn; } diff --git a/src/Zir.zig b/src/Zir.zig index f4c62a6f24..fc638ef869 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -2502,11 +2502,7 @@ pub const Inst = struct { /// Offset from Decl AST node index. /// `Tag` determines which kind of AST node this points to. src_node: i32, - /// `false`: Not safety checked - the compiler will assume the - /// correctness of this instruction. - /// `true`: In safety-checked modes, this will generate a call - /// to the panic function unless it can be proven unreachable by the compiler. - safety: bool, + force_comptime: bool, pub fn src(self: @This()) LazySrcLoc { return .{ .node_offset = self.src_node }; diff --git a/src/print_zir.zig b/src/print_zir.zig index 776aeffbdc..ed1eeeeba3 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -2091,8 +2091,6 @@ const Writer = struct { fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void { const inst_data = self.code.instructions.items(.data)[inst].@"unreachable"; - const safety_str = if (inst_data.safety) "safe" else "unsafe"; - try stream.print("{s}) ", .{safety_str}); try self.writeSrc(stream, inst_data.src()); } diff --git a/test/compile_errors/stage2/comptime_unreachable.zig b/test/compile_errors/stage2/comptime_unreachable.zig new file mode 100644 index 0000000000..6086dd85c6 --- /dev/null +++ b/test/compile_errors/stage2/comptime_unreachable.zig @@ -0,0 +1,7 @@ +pub export fn entry() void { + comptime unreachable; +} + +// error +// +// :2:14: error: reached unreachable code diff --git a/test/incremental/x86_64-linux/comptime_var.3.zig b/test/incremental/x86_64-linux/comptime_var.3.zig index d4e6d85d9d..5d25d6556e 100644 --- a/test/incremental/x86_64-linux/comptime_var.3.zig +++ b/test/incremental/x86_64-linux/comptime_var.3.zig @@ -7,4 +7,4 @@ pub fn main() void {} // error // -// :4:17: error: unable to resolve comptime value +// :4:17: error: reached unreachable code diff --git a/test/incremental/x86_64-macos/comptime_var.3.zig b/test/incremental/x86_64-macos/comptime_var.3.zig index d4e6d85d9d..5d25d6556e 100644 --- a/test/incremental/x86_64-macos/comptime_var.3.zig +++ b/test/incremental/x86_64-macos/comptime_var.3.zig @@ -7,4 +7,4 @@ pub fn main() void {} // error // -// :4:17: error: unable to resolve comptime value +// :4:17: error: reached unreachable code