Merge pull request #21618 from mlugg/validate-runtime-value

Sema: add a few missing runtime value validations
This commit is contained in:
Matthew Lugg 2024-10-07 13:46:21 +01:00 committed by GitHub
commit ea527f7a85
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 1 deletions

View File

@ -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;
}
@ -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()) {
@ -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 = .{

View File

@ -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
@ -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

View File

@ -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
@ -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