Skip to content

Commit

Permalink
fix comptime code modification of global const
Browse files Browse the repository at this point in the history
closes #1008
  • Loading branch information
andrewrk committed May 12, 2018
1 parent 6e82107 commit 277b9cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ir.cpp
Expand Up @@ -8686,6 +8686,10 @@ static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_
*dest = *src;
if (!same_global_refs) {
dest->global_refs = global_refs;
if (dest->type->id == TypeTableEntryIdStruct) {
dest->data.x_struct.fields = allocate_nonzero<ConstExprValue>(dest->type->data.structure.src_field_count);
memcpy(dest->data.x_struct.fields, src->data.x_struct.fields, sizeof(ConstExprValue) * dest->type->data.structure.src_field_count);
}
}
}

Expand Down Expand Up @@ -11670,7 +11674,8 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
if (var->mem_slot_index != SIZE_MAX) {
assert(var->mem_slot_index < ira->exec_context.mem_slot_count);
ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
*mem_slot = casted_init_value->value;
copy_const_val(mem_slot, &casted_init_value->value,
!is_comptime_var || var->gen_is_const);

if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {
ir_build_const_from(ira, &decl_var_instruction->base);
Expand Down
17 changes: 17 additions & 0 deletions test/cases/eval.zig
Expand Up @@ -536,3 +536,20 @@ test "runtime 128 bit integer division" {
var c = a / b;
assert(c == 15231399999);
}

pub const Info = struct {
version: u8,
};

pub const diamond_info = Info {
.version = 0,
};

test "comptime modification of const struct field" {
comptime {
var res = diamond_info;
res.version = 1;
assert(diamond_info.version == 0);
assert(res.version == 1);
}
}

0 comments on commit 277b9cf

Please sign in to comment.