Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sema: ensure result of block_comptime is comptime-known #22305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1612,11 +1612,30 @@ fn analyzeBodyInner(

.block => if (block.is_comptime) {
continue :inst .block_inline;
} else try sema.zirBlock(block, inst, false),
} else try sema.zirBlock(block, inst),

.block_comptime => if (block.is_comptime) {
continue :inst .block_inline;
} else try sema.zirBlock(block, inst, true),
} else {
const pl_node = datas[@intFromEnum(inst)].pl_node;
const src = block.nodeOffset(pl_node.src_node);
const extra = sema.code.extraData(Zir.Inst.Block, pl_node.payload_index);
const block_body = sema.code.bodySlice(extra.end, extra.data.body_len);

var child_block = block.makeSubBlock();
defer child_block.instructions.deinit(sema.gpa);
child_block.is_comptime = true;

const result = try sema.resolveInlineBody(&child_block, block_body, inst);

if (!try sema.isComptimeKnown(result)) {
// TODO: encode the reason for this comptime block in ZIR, then we
// can use `failWithNeededComptime` for a better error message.
return sema.fail(block, src, "unable to resolve comptime value", .{});
}

break :inst result;
},

.block_inline => blk: {
// Directly analyze the block body without introducing a new block.
Expand Down Expand Up @@ -6087,7 +6106,7 @@ fn zirSuspendBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) Comp
return sema.failWithUseOfAsync(parent_block, src);
}

fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_comptime: bool) CompileError!Air.Inst.Ref {
fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
const tracy = trace(@src());
defer tracy.end();

Expand Down Expand Up @@ -6123,7 +6142,7 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_compt
.instructions = .{},
.label = &label,
.inlining = parent_block.inlining,
.is_comptime = parent_block.is_comptime or force_comptime,
.is_comptime = parent_block.is_comptime,
.comptime_reason = parent_block.comptime_reason,
.is_typeof = parent_block.is_typeof,
.want_safety = parent_block.want_safety,
Expand Down
14 changes: 14 additions & 0 deletions test/cases/compile_errors/comptime_keyword_runtime_result.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var global_rt: u8 = 123;

export fn foo() void {
_ = comptime global_rt;
}

export fn bar(rt: ?*anyopaque) void {
if (rt != null) bar(comptime rt);
}

// error
//
// :4:18: error: unable to resolve comptime value
// :8:34: error: unable to resolve comptime value
Loading