Skip to content

Commit

Permalink
Merge pull request #21618 from mlugg/validate-runtime-value
Browse files Browse the repository at this point in the history
Sema: add a few missing runtime value validations
  • Loading branch information
mlugg authored Oct 7, 2024
2 parents 7a2fde9 + 95857d6 commit ea527f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2274,15 +2274,20 @@ pub fn resolveFinalDeclValue(
src: LazySrcLoc,
air_ref: Air.Inst.Ref,
) CompileError!Value {
const zcu = sema.pt.zcu;

const val = try sema.resolveValueAllowVariables(air_ref) orelse {
return sema.failWithNeededComptime(block, src, .{
.needed_comptime_reason = "global variable initializer must be comptime-known",
});
};
if (val.isGenericPoison()) return error.GenericPoison;
if (val.canMutateComptimeVarState(sema.pt.zcu)) {

const init_val: Value = if (val.getVariable(zcu)) |v| .fromInterned(v.init) else val;
if (init_val.canMutateComptimeVarState(zcu)) {
return sema.fail(block, src, "global variable contains reference to comptime var", .{});
}

return val;
}

Expand Down Expand Up @@ -26193,6 +26198,8 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
}

try sema.requireRuntimeBlock(block, src, runtime_src);
try sema.validateRuntimeValue(block, dest_src, dest_ptr);
try sema.validateRuntimeValue(block, src_src, src_ptr);

// Aliasing safety check.
if (block.wantSafety()) {
Expand Down Expand Up @@ -26321,6 +26328,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
};

try sema.requireRuntimeBlock(block, src, runtime_src);
try sema.validateRuntimeValue(block, dest_src, dest_ptr);
try sema.validateRuntimeValue(block, value_src, elem);

_ = try block.addInst(.{
.tag = if (block.wantSafety()) .memset_safe else .memset,
.data = .{ .bin_op = .{
Expand Down
20 changes: 20 additions & 0 deletions test/cases/compile_errors/comptime_var_referenced_at_runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export fn qar() void {
_ = y;
}

export fn bux() void {
comptime var x: [2]u32 = undefined;
x = .{ 1, 2 };

var rt: [2]u32 = undefined;
@memcpy(&rt, &x);
}

export fn far() void {
comptime var x: u32 = 123;

var rt: [2]*u32 = undefined;
const elem: *u32 = &x;
@memset(&rt, elem);
}

// error
//
// :5:19: error: runtime value contains reference to comptime var
Expand All @@ -63,3 +79,7 @@ export fn qar() void {
// :41:12: note: comptime var pointers are not available at runtime
// :46:39: error: runtime value contains reference to comptime var
// :46:39: note: comptime var pointers are not available at runtime
// :55:18: error: runtime value contains reference to comptime var
// :55:18: note: comptime var pointers are not available at runtime
// :63:18: error: runtime value contains reference to comptime var
// :63:18: note: comptime var pointers are not available at runtime
7 changes: 7 additions & 0 deletions test/cases/compile_errors/comptime_var_referenced_by_decl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export const g: *const *const u32 = g: {
break :g &aggregate[0];
};

// Mutable globals should have the same restrictions as const globals.
export var h: *[1]u32 = h: {
var x: [1]u32 = .{123};
break :h &x;
};

// error
//
// :1:27: error: global variable contains reference to comptime var
Expand All @@ -47,3 +53,4 @@ export const g: *const *const u32 = g: {
// :22:24: error: global variable contains reference to comptime var
// :28:33: error: global variable contains reference to comptime var
// :34:40: error: global variable contains reference to comptime var
// :42:28: error: global variable contains reference to comptime var

0 comments on commit ea527f7

Please sign in to comment.