stage2: fix comptime unreachable

This commit is contained in:
Veikka Tuominen
2022-04-29 10:38:47 +03:00
parent 3b8187072f
commit a6f254ec3e
7 changed files with 16 additions and 14 deletions

View File

@@ -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),
} },
});

View File

@@ -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;
}

View File

@@ -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 };

View File

@@ -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());
}

View File

@@ -0,0 +1,7 @@
pub export fn entry() void {
comptime unreachable;
}
// error
//
// :2:14: error: reached unreachable code

View File

@@ -7,4 +7,4 @@ pub fn main() void {}
// error
//
// :4:17: error: unable to resolve comptime value
// :4:17: error: reached unreachable code

View File

@@ -7,4 +7,4 @@ pub fn main() void {}
// error
//
// :4:17: error: unable to resolve comptime value
// :4:17: error: reached unreachable code