authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-09-14 22:52:09+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-09-15 14:29:57+01:00
log50ef10eb4963167225f7153dc5165292dbac0046
treed6cb9b5028c9b38f867c648fef7c16299fab4990
parent6a349648cb29b3e624dfffcc412c4249c0e19d9f
signaturelock-open Commit is signed but in an unrecognized format.

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 files changed, 15 insertions(+), 0 deletions(-)

src/Sema.zig+15
......@@ -3780,6 +3780,21 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
37803780 return sema.analyzeDeclRef(try anon_decl.finish(elem_ty, store_val, ptr_info.flags.alignment));
37813781 }
37823782
3783 // If this is already a comptime-mutable allocation, we don't want to emit an error - the stores
3784 // were already performed at comptime! Just make the pointer constant as normal.
3785 implicit_ct: {
3786 const ptr_val = try sema.resolveMaybeUndefVal(alloc) orelse break :implicit_ct;
3787 if (ptr_val.isComptimeMutablePtr(mod)) break :implicit_ct;
3788 return sema.makePtrConst(block, alloc);
3789 }
3790
3791 if (try sema.typeRequiresComptime(elem_ty)) {
3792 // The value was initialized through RLS, so we didn't detect the runtime condition earlier.
3793 // TODO: source location of runtime control flow
3794 const init_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
3795 return sema.fail(block, init_src, "value with comptime-only type '{}' depends on runtime control flow", .{elem_ty.fmt(mod)});
3796 }
3797
37833798 return sema.makePtrConst(block, alloc);
37843799}
37853800