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...@@ -740,7 +740,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
740 _ = try gz.addAsIndex(.{740 _ = try gz.addAsIndex(.{
741 .tag = .@"unreachable",741 .tag = .@"unreachable",
742 .data = .{ .@"unreachable" = .{742 .data = .{ .@"unreachable" = .{
743 .safety = true,743 .force_comptime = gz.force_comptime,
744 .src_node = gz.nodeIndexToRelative(node),744 .src_node = gz.nodeIndexToRelative(node),
745 } },745 } },
746 });746 });
src/Sema.zig+5-4
...@@ -12337,14 +12337,15 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi...@@ -12337,14 +12337,15 @@ fn addRuntimeBreak(sema: *Sema, child_block: *Block, break_data: BreakData) !voi
12337}12337}
1233812338
12339fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {12339fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
12340 const tracy = trace(@src());
12341 defer tracy.end();
12342
12343 const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable";12340 const inst_data = sema.code.instructions.items(.data)[inst].@"unreachable";
12344 const src = inst_data.src();12341 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 }
12345 try sema.requireRuntimeBlock(block, src);12346 try sema.requireRuntimeBlock(block, src);
12346 // TODO Add compile error for @optimizeFor occurring too late in a scope.12347 // 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);
12348 return always_noreturn;12349 return always_noreturn;
12349}12350}
1235012351
src/Zir.zig+1-5
...@@ -2502,11 +2502,7 @@ pub const Inst = struct {...@@ -2502,11 +2502,7 @@ pub const Inst = struct {
2502 /// Offset from Decl AST node index.2502 /// Offset from Decl AST node index.
2503 /// `Tag` determines which kind of AST node this points to.2503 /// `Tag` determines which kind of AST node this points to.
2504 src_node: i32,2504 src_node: i32,
2505 /// `false`: Not safety checked - the compiler will assume the2505 force_comptime: bool,
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,
25102506
2511 pub fn src(self: @This()) LazySrcLoc {2507 pub fn src(self: @This()) LazySrcLoc {
2512 return .{ .node_offset = self.src_node };2508 return .{ .node_offset = self.src_node };
src/print_zir.zig-2
...@@ -2091,8 +2091,6 @@ const Writer = struct {...@@ -2091,8 +2091,6 @@ const Writer = struct {
20912091
2092 fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {2092 fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2093 const inst_data = self.code.instructions.items(.data)[inst].@"unreachable";2093 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});
2096 try self.writeSrc(stream, inst_data.src());2094 try self.writeSrc(stream, inst_data.src());
2097 }2095 }
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 {}...@@ -7,4 +7,4 @@ pub fn main() void {}
77
8// error8// error
9//9//
10// :4:17: error: unable to resolve comptime value10// :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 {}...@@ -7,4 +7,4 @@ pub fn main() void {}
77
8// error8// error
9//9//
10// :4:17: error: unable to resolve comptime value10// :4:17: error: reached unreachable code