Skip to content

Commit

Permalink
Sema: add missing compile error for runtime-known const with comptime…
Browse files Browse the repository at this point in the history
…-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.
  • Loading branch information
mlugg committed Sep 14, 2023
1 parent 024ed86 commit b684341
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3808,6 +3808,21 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
return sema.analyzeDeclRef(try anon_decl.finish(elem_ty, store_val, ptr_info.flags.alignment));
}

// If this is already a comptime-mutable allocation, we don't want to emit an error - the stores
// were already performed at comptime! Just make the pointer constant as normal.
implicit_ct: {
const ptr_val = try sema.resolveMaybeUndefVal(alloc) orelse break :implicit_ct;
if (ptr_val.isComptimeMutablePtr(mod)) break :implicit_ct;
return sema.makePtrConst(block, alloc);
}

if (try sema.typeRequiresComptime(elem_ty)) {
// The value was initialized through RLS, so we didn't detect the runtime condition earlier.
// TODO: source location of runtime control flow
const init_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
return sema.fail(block, init_src, "value with comptime-only type '{}' depends on runtime control flow", .{elem_ty.fmt(mod)});
}

return sema.makePtrConst(block, alloc);
}

Expand Down

0 comments on commit b684341

Please sign in to comment.