authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-11 22:41:44-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-11 22:41:44-04:00
log277b9cf8788f340f387e63029ad9fc12664cafff
tree4eed4bea02c91dd6e98786be22613b06aa1ddf6f
parent6e821078f625a03eb8b7794c983da0f7793366ab

fix comptime code modification of global const

closes #1008

2 files changed, 23 insertions(+), 1 deletions(-)

src/ir.cpp+6-1
...@@ -8686,6 +8686,10 @@ static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_...@@ -8686,6 +8686,10 @@ static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_
8686 *dest = *src;8686 *dest = *src;
8687 if (!same_global_refs) {8687 if (!same_global_refs) {
8688 dest->global_refs = global_refs;8688 dest->global_refs = global_refs;
8689 if (dest->type->id == TypeTableEntryIdStruct) {
8690 dest->data.x_struct.fields = allocate_nonzero<ConstExprValue>(dest->type->data.structure.src_field_count);
8691 memcpy(dest->data.x_struct.fields, src->data.x_struct.fields, sizeof(ConstExprValue) * dest->type->data.structure.src_field_count);
8692 }
8689 }8693 }
8690}8694}
86918695
...@@ -11670,7 +11674,8 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -11670,7 +11674,8 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
11670 if (var->mem_slot_index != SIZE_MAX) {11674 if (var->mem_slot_index != SIZE_MAX) {
11671 assert(var->mem_slot_index < ira->exec_context.mem_slot_count);11675 assert(var->mem_slot_index < ira->exec_context.mem_slot_count);
11672 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];11676 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
11673 *mem_slot = casted_init_value->value;11677 copy_const_val(mem_slot, &casted_init_value->value,
11678 !is_comptime_var || var->gen_is_const);
1167411679
11675 if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {11680 if (is_comptime_var || (var_class_requires_const && var->gen_is_const)) {
11676 ir_build_const_from(ira, &decl_var_instruction->base);11681 ir_build_const_from(ira, &decl_var_instruction->base);
test/cases/eval.zig+17
...@@ -536,3 +536,20 @@ test "runtime 128 bit integer division" {...@@ -536,3 +536,20 @@ test "runtime 128 bit integer division" {
536 var c = a / b;536 var c = a / b;
537 assert(c == 15231399999);537 assert(c == 15231399999);
538}538}
539
540pub const Info = struct {
541 version: u8,
542};
543
544pub const diamond_info = Info {
545 .version = 0,
546};
547
548test "comptime modification of const struct field" {
549 comptime {
550 var res = diamond_info;
551 res.version = 1;
552 assert(diamond_info.version == 0);
553 assert(res.version == 1);
554 }
555}