authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-04-29 10:38:47+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-04-30 01:05:48+03:00
loga6f254ec3e106e6496a84c89375427e6adc9065f
treef892444e1f9a7af783b7fb0f4ebdbdef6be86d6c
parent3b8187072fc76966da9d9ab1a6a0ecd7aff4e090

stage2: fix comptime unreachable


7 files changed, 16 insertions(+), 14 deletions(-)

src/AstGen.zig+1-1
......@@ -740,7 +740,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
740740 _ = try gz.addAsIndex(.{
741741 .tag = .@"unreachable",
742742 .data = .{ .@"unreachable" = .{
743 .safety = true,
743 .force_comptime = gz.force_comptime,
744744 .src_node = gz.nodeIndexToRelative(node),
745745 } },
746746 });
src/Sema.zig+5-4
......@@ -12337,14 +12337,15 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi
1233712337}
1233812338
1233912339fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
12340 const tracy = trace(@src());
12341 defer tracy.end();
12342
1234312340 const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable";
1234412341 const src = inst_data.src();
12342
12343 if (block.is_comptime or inst_data.force_comptime) {
12344 return sema.fail(block, src, "reached unreachable code", .{});
12345 }
1234512346 try sema.requireRuntimeBlock(block, src);
1234612347 // TODO Add compile error for @optimizeFor occurring too late in a scope.
12347 try block.addUnreachable(src, inst_data.safety);
12348 try block.addUnreachable(src, true);
1234812349 return always_noreturn;
1234912350}
1235012351
src/Zir.zig+1-5
......@@ -2502,11 +2502,7 @@ pub const Inst = struct {
25022502 /// Offset from Decl AST node index.
25032503 /// `Tag` determines which kind of AST node this points to.
25042504 src_node: i32,
2505 /// `false`: Not safety checked - the compiler will assume the
2506 /// correctness of this instruction.
2507 /// `true`: In safety-checked modes, this will generate a call
2508 /// to the panic function unless it can be proven unreachable by the compiler.
2509 safety: bool,
2505 force_comptime: bool,
25102506
25112507 pub fn src(self: @This()) LazySrcLoc {
25122508 return .{ .node_offset = self.src_node };
src/print_zir.zig-2
......@@ -2091,8 +2091,6 @@ const Writer = struct {
20912091
20922092 fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
20932093 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";
2094 const safety_str = if (inst_data.safety) "safe" else "unsafe";
2095 try stream.print("{s}) ", .{safety_str});
20962094 try self.writeSrc(stream, inst_data.src());
20972095 }
20982096
test/compile_errors/stage2/comptime_unreachable.zig created+7
......@@ -0,0 +1,7 @@
1pub export fn entry() void {
2 comptime unreachable;
3}
4
5// error
6//
7// :2:14: error: reached unreachable code
test/incremental/x86_64-linux/comptime_var.3.zig+1-1
......@@ -7,4 +7,4 @@ pub fn main() void {}
77
88// error
99//
10// :4:17: error: unable to resolve comptime value
10// :4:17: error: reached unreachable code
test/incremental/x86_64-macos/comptime_var.3.zig+1-1
......@@ -7,4 +7,4 @@ pub fn main() void {}
77
88// error
99//
10// :4:17: error: unable to resolve comptime value
10// :4:17: error: reached unreachable code