Commit a6f254ec3e

Veikka Tuominen <git@vexu.eu>
2022-04-29 09:38:47
stage2: fix comptime unreachable
1 parent 3b81870
Changed files (7)
src
test
compile_errors
incremental
x86_64-linux
x86_64-macos
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),
                 } },
             });
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());
     }
 
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;
 }
 
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 };
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
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
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