Skip to content

Commit b684341

Browse files
committed
Sema: add missing compile error for runtime-known const with comptime-only type
When RLS is used to initialize a value with a comptime-only type, the usual "value with comptime-only type depends on runtime control flow" error message isn't hit, because we don't use results from a block. When we reach `make_ptr_const`, we must validate that the value is comptime-known.
1 parent 024ed86 commit b684341

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Sema.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,6 +3808,21 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
38083808
return sema.analyzeDeclRef(try anon_decl.finish(elem_ty, store_val, ptr_info.flags.alignment));
38093809
}
38103810

3811+
// If this is already a comptime-mutable allocation, we don't want to emit an error - the stores
3812+
// were already performed at comptime! Just make the pointer constant as normal.
3813+
implicit_ct: {
3814+
const ptr_val = try sema.resolveMaybeUndefVal(alloc) orelse break :implicit_ct;
3815+
if (ptr_val.isComptimeMutablePtr(mod)) break :implicit_ct;
3816+
return sema.makePtrConst(block, alloc);
3817+
}
3818+
3819+
if (try sema.typeRequiresComptime(elem_ty)) {
3820+
// The value was initialized through RLS, so we didn't detect the runtime condition earlier.
3821+
// TODO: source location of runtime control flow
3822+
const init_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
3823+
return sema.fail(block, init_src, "value with comptime-only type '{}' depends on runtime control flow", .{elem_ty.fmt(mod)});
3824+
}
3825+
38113826
return sema.makePtrConst(block, alloc);
38123827
}
38133828

0 commit comments

Comments
 (0)