authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-19 14:35:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-19 14:35:59-04:00
logb588a803bf4493d2fd1892fe172b9ce8cfb0ca30
tree8d5c9306669cc5d185568ec45445d26e326a3fd9
parent79671efd3a5b67e359aa60c66e4007c03342b28e
signature Commit is signed but in an unrecognized format.

fix comptime modification of const struct field


6 files changed, 41 insertions(+), 27 deletions(-)

src/all_types.hpp+3
......@@ -334,6 +334,9 @@ struct ConstExprValue {
334334 RuntimeHintPtr rh_ptr;
335335 RuntimeHintSlice rh_slice;
336336 } data;
337
338 ConstExprValue(const ConstExprValue &other) = delete; // plz zero initialize with {}
339 ConstExprValue& operator= (const ConstExprValue &other) = delete; // use copy_const_val
337340};
338341
339342enum ReturnKnowledge {
src/analyze.cpp+1-1
......@@ -4711,7 +4711,7 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) {
47114711void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
47124712 auto entry = g->string_literals_table.maybe_get(str);
47134713 if (entry != nullptr) {
4714 *const_val = *entry->value;
4714 memcpy(const_val, entry->value, sizeof(ConstExprValue));
47154715 return;
47164716 }
47174717
src/codegen.cpp+1-1
......@@ -6676,7 +6676,7 @@ static void do_code_gen(CodeGen *g) {
66766676 zig_panic("TODO debug info for var with ptr casted value");
66776677 }
66786678 ZigType *var_type = g->builtin_types.entry_f128;
6679 ConstExprValue coerced_value;
6679 ConstExprValue coerced_value = {};
66806680 coerced_value.special = ConstValSpecialStatic;
66816681 coerced_value.type = var_type;
66826682 coerced_value.data.x_f128 = bigfloat_to_f128(&const_val->data.x_bigfloat);
src/ir.cpp+27-16
......@@ -10624,13 +10624,16 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1062410624
1062510625static void copy_const_val(ConstExprValue *dest, ConstExprValue *src, bool same_global_refs) {
1062610626 ConstGlobalRefs *global_refs = dest->global_refs;
10627 assert(!same_global_refs || src->global_refs != nullptr);
10628 *dest = *src;
10627 memcpy(dest, src, sizeof(ConstExprValue));
1062910628 if (!same_global_refs) {
1063010629 dest->global_refs = global_refs;
10630 if (src->special == ConstValSpecialUndef)
10631 return;
1063110632 if (dest->type->id == ZigTypeIdStruct) {
10632 dest->data.x_struct.fields = allocate_nonzero<ConstExprValue>(dest->type->data.structure.src_field_count);
10633 memcpy(dest->data.x_struct.fields, src->data.x_struct.fields, sizeof(ConstExprValue) * dest->type->data.structure.src_field_count);
10633 dest->data.x_struct.fields = create_const_vals(dest->type->data.structure.src_field_count);
10634 for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
10635 copy_const_val(&dest->data.x_struct.fields[i], &src->data.x_struct.fields[i], false);
10636 }
1063410637 }
1063510638 }
1063610639}
......@@ -13579,7 +13582,7 @@ static ErrorMsg *ir_eval_math_op_scalar(IrAnalyze *ira, IrInstruction *source_in
1357913582 }
1358013583 } else {
1358113584 float_div_trunc(out_val, op1_val, op2_val);
13582 ConstExprValue remainder;
13585 ConstExprValue remainder = {};
1358313586 float_rem(&remainder, op1_val, op2_val);
1358413587 if (float_cmp_zero(&remainder) != CmpEQ) {
1358513588 return ir_add_error(ira, source_instr, buf_sprintf("exact division had a remainder"));
......@@ -13954,8 +13957,8 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1395413957 // have a remainder function ambiguity problem
1395513958 ok = true;
1395613959 } else {
13957 ConstExprValue rem_result;
13958 ConstExprValue mod_result;
13960 ConstExprValue rem_result = {};
13961 ConstExprValue mod_result = {};
1395913962 float_rem(&rem_result, op1_val, op2_val);
1396013963 float_mod(&mod_result, op1_val, op2_val);
1396113964 ok = float_cmp(&rem_result, &mod_result) == CmpEQ;
......@@ -14178,10 +14181,12 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1417814181
1417914182 size_t next_index = 0;
1418014183 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
14181 out_array_val->data.x_array.data.s_none.elements[next_index] = op1_array_val->data.x_array.data.s_none.elements[i];
14184 copy_const_val(&out_array_val->data.x_array.data.s_none.elements[next_index],
14185 &op1_array_val->data.x_array.data.s_none.elements[i], true);
1418214186 }
1418314187 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
14184 out_array_val->data.x_array.data.s_none.elements[next_index] = op2_array_val->data.x_array.data.s_none.elements[i];
14188 copy_const_val(&out_array_val->data.x_array.data.s_none.elements[next_index],
14189 &op2_array_val->data.x_array.data.s_none.elements[i], true);
1418514190 }
1418614191 if (next_index < new_len) {
1418714192 ConstExprValue *null_byte = &out_array_val->data.x_array.data.s_none.elements[next_index];
......@@ -14242,7 +14247,8 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1424214247 uint64_t i = 0;
1424314248 for (uint64_t x = 0; x < mult_amt; x += 1) {
1424414249 for (uint64_t y = 0; y < old_array_len; y += 1) {
14245 out_val->data.x_array.data.s_none.elements[i] = array_val->data.x_array.data.s_none.elements[y];
14250 copy_const_val(&out_val->data.x_array.data.s_none.elements[i],
14251 &array_val->data.x_array.data.s_none.elements[y], true);
1424614252 i += 1;
1424714253 }
1424814254 }
......@@ -14382,7 +14388,12 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1438214388 if (instr_is_comptime(var_ptr) && var_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) {
1438314389 init_val = const_ptr_pointee(ira, ira->codegen, &var_ptr->value, decl_var_instruction->base.source_node);
1438414390 if (is_comptime_var) {
14385 var->const_value = init_val;
14391 if (var->gen_is_const) {
14392 var->const_value = init_val;
14393 } else {
14394 var->const_value = create_const_vals(1);
14395 copy_const_val(var->const_value, init_val, false);
14396 }
1438614397 }
1438714398 }
1438814399
......@@ -15291,7 +15302,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1529115302 arg_val = create_const_runtime(casted_arg->value.type);
1529215303 }
1529315304 if (arg_part_of_generic_id) {
15294 generic_id->params[generic_id->param_count] = *arg_val;
15305 copy_const_val(&generic_id->params[generic_id->param_count], arg_val, true);
1529515306 generic_id->param_count += 1;
1529615307 }
1529715308
......@@ -15476,7 +15487,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1547615487 // * "string literal used as comptime slice is memoized"
1547715488 // * "comptime modification of const struct field" - except modified to avoid
1547815489 // ConstPtrMutComptimeVar, thus defeating the logic below.
15479 bool same_global_refs = ptr->value.data.x_ptr.mut != ConstPtrMutComptimeVar;
15490 bool same_global_refs = ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst;
1548015491 copy_const_val(dest_val, &value->value, same_global_refs);
1548115492 if (!ira->new_irb.current_basic_block->must_be_comptime_source_instr) {
1548215493 ira->new_irb.current_basic_block->must_be_comptime_source_instr = source_instr;
......@@ -15877,7 +15888,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1587715888 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr);
1587815889 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1587915890 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
15880 const_instruction->base.value = *align_result;
15891 copy_const_val(&const_instruction->base.value, align_result, true);
1588115892
1588215893 uint32_t align_bytes = 0;
1588315894 ir_resolve_align(ira, &const_instruction->base, &align_bytes);
......@@ -21667,7 +21678,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
2166721678
2166821679 ConstExprValue *byte_val = &casted_byte->value;
2166921680 for (size_t i = start; i < end; i += 1) {
21670 dest_elements[i] = *byte_val;
21681 copy_const_val(&dest_elements[i], byte_val, true);
2167121682 }
2167221683
2167321684 return ir_const_void(ira, &instruction->base);
......@@ -21835,7 +21846,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2183521846 // TODO check for noalias violations - this should be generalized to work for any function
2183621847
2183721848 for (size_t i = 0; i < count; i += 1) {
21838 dest_elements[dest_start + i] = src_elements[src_start + i];
21849 copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true);
2183921850 }
2184021851
2184121852 return ir_const_void(ira, &instruction->base);
test/stage1/behavior.zig+1-1
......@@ -48,7 +48,7 @@ comptime {
4848 _ = @import("behavior/enum.zig"); // TODO
4949 _ = @import("behavior/enum_with_members.zig");
5050 _ = @import("behavior/error.zig"); // TODO
51 _ = @import("behavior/eval.zig"); // TODO
51 _ = @import("behavior/eval.zig");
5252 _ = @import("behavior/field_parent_ptr.zig");
5353 _ = @import("behavior/fn.zig");
5454 _ = @import("behavior/fn_in_struct_in_comptime.zig");
test/stage1/behavior/eval.zig+8-8
......@@ -583,14 +583,14 @@ pub const Info = struct {
583583
584584pub const diamond_info = Info{ .version = 0 };
585585
586//test "comptime modification of const struct field" {
587// comptime {
588// var res = diamond_info;
589// res.version = 1;
590// expect(diamond_info.version == 0);
591// expect(res.version == 1);
592// }
593//}
586test "comptime modification of const struct field" {
587 comptime {
588 var res = diamond_info;
589 res.version = 1;
590 expect(diamond_info.version == 0);
591 expect(res.version == 1);
592 }
593}
594594
595595test "pointer to type" {
596596 comptime {