authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 16:09:40-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 16:09:40-04:00
logd105769926fd5360a5309be3e202cc65d32ce604
tree3b1b518ebad8684cfa4f1a9f6f4f5a65830ecbcf
parent9069ee957cb8c9069028b325af5b862bbf8f66af
signaturelock-open Commit is signed but in an unrecognized format.

fix regressions regarding writing through const pointers


3 files changed, 28 insertions(+), 24 deletions(-)

src/all_types.hpp+2
......@@ -2543,6 +2543,7 @@ struct IrInstructionLoadPtrGen {
25432543struct IrInstructionStorePtr {
25442544 IrInstruction base;
25452545
2546 bool allow_write_through_const;
25462547 IrInstruction *ptr;
25472548 IrInstruction *value;
25482549};
......@@ -3707,6 +3708,7 @@ enum ResultLocId {
37073708struct ResultLoc {
37083709 ResultLocId id;
37093710 bool written;
3711 bool allow_write_through_const;
37103712 IrInstruction *resolved_loc; // result ptr
37113713 IrInstruction *source_instruction;
37123714 IrInstruction *gen_instruction; // value to store to the result loc
src/ir.cpp+18-16
......@@ -198,7 +198,7 @@ static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruct
198198static IrInstruction *ir_analyze_unwrap_err_code(IrAnalyze *ira, IrInstruction *source_instr,
199199 IrInstruction *base_ptr, bool initializing);
200200static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
201 IrInstruction *ptr, IrInstruction *uncasted_value);
201 IrInstruction *ptr, IrInstruction *uncasted_value, bool allow_write_through_const);
202202static IrInstruction *ir_gen_union_init_expr(IrBuilder *irb, Scope *scope, AstNode *source_node,
203203 IrInstruction *union_type, IrInstruction *field_name, AstNode *expr_node,
204204 LVal lval, ResultLoc *parent_result_loc);
......@@ -1613,7 +1613,7 @@ static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode
16131613 return &unreachable_instruction->base;
16141614}
16151615
1616static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1616static IrInstructionStorePtr *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
16171617 IrInstruction *ptr, IrInstruction *value)
16181618{
16191619 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, scope, source_node);
......@@ -1625,7 +1625,7 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
16251625 ir_ref_instruction(ptr, irb->current_basic_block);
16261626 ir_ref_instruction(value, irb->current_basic_block);
16271627
1628 return &instruction->base;
1628 return instruction;
16291629}
16301630
16311631static IrInstruction *ir_build_var_decl_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
......@@ -6051,6 +6051,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
60516051 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
60526052 result_loc_inst->base.id = ResultLocIdInstruction;
60536053 result_loc_inst->base.source_instruction = field_ptr;
6054 result_loc_inst->base.allow_write_through_const = true;
60546055 ir_ref_instruction(field_ptr, irb->current_basic_block);
60556056 ir_build_reset_result(irb, scope, expr_node, &result_loc_inst->base);
60566057
......@@ -6089,6 +6090,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
60896090 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
60906091 result_loc_inst->base.id = ResultLocIdInstruction;
60916092 result_loc_inst->base.source_instruction = elem_ptr;
6093 result_loc_inst->base.allow_write_through_const = true;
60926094 ir_ref_instruction(elem_ptr, irb->current_basic_block);
60936095 ir_build_reset_result(irb, scope, expr_node, &result_loc_inst->base);
60946096
......@@ -6646,7 +6648,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
66466648
66476649 ir_set_cursor_at_end_and_append_block(irb, continue_block);
66486650 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);
6649 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val));
6651 ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val)->allow_write_through_const = true;
66506652 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
66516653
66526654 IrInstruction *else_result = nullptr;
......@@ -14848,7 +14850,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1484814850 // instruction.
1484914851 assert(deref->value.special != ConstValSpecialRuntime);
1485014852 var_ptr->value.special = ConstValSpecialRuntime;
14851 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref);
14853 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref, false);
1485214854 }
1485314855
1485414856 if (var_ptr->value.special == ConstValSpecialStatic && var->mem_slot_index != SIZE_MAX) {
......@@ -15862,7 +15864,7 @@ no_mem_slot:
1586215864}
1586315865
1586415866static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
15865 IrInstruction *ptr, IrInstruction *uncasted_value)
15867 IrInstruction *ptr, IrInstruction *uncasted_value, bool allow_write_through_const)
1586615868{
1586715869 assert(ptr->value.type->id == ZigTypeIdPointer);
1586815870
......@@ -15878,7 +15880,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1587815880
1587915881 ZigType *child_type = ptr->value.type->data.pointer.child_type;
1588015882
15881 if (ptr->value.type->data.pointer.is_const && !source_instr->is_gen) {
15883 if (ptr->value.type->data.pointer.is_const && !allow_write_through_const) {
1588215884 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
1588315885 return ira->codegen->invalid_instruction;
1588415886 }
......@@ -15957,10 +15959,9 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1595715959 break;
1595815960 }
1595915961
15960 IrInstruction *result = ir_build_store_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node,
15961 ptr, value);
15962 result->value.type = ira->codegen->builtin_types.entry_void;
15963 return result;
15962 IrInstructionStorePtr *store_ptr = ir_build_store_ptr(&ira->new_irb, source_instr->scope,
15963 source_instr->source_node, ptr, value);
15964 return &store_ptr->base;
1596415965}
1596515966
1596615967static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *call_instruction,
......@@ -18283,7 +18284,7 @@ static IrInstruction *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstruc
1828318284 if (type_is_invalid(value->value.type))
1828418285 return ira->codegen->invalid_instruction;
1828518286
18286 return ir_analyze_store_ptr(ira, &instruction->base, ptr, value);
18287 return ir_analyze_store_ptr(ira, &instruction->base, ptr, value, instruction->allow_write_through_const);
1828718288}
1828818289
1828918290static IrInstruction *ir_analyze_instruction_load_ptr(IrAnalyze *ira, IrInstructionLoadPtr *instruction) {
......@@ -19691,7 +19692,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1969119692
1969219693 IrInstruction *field_ptr = ir_analyze_struct_field_ptr(ira, instruction, field, result_loc,
1969319694 container_type, true);
19694 ir_analyze_store_ptr(ira, instruction, field_ptr, runtime_inst);
19695 ir_analyze_store_ptr(ira, instruction, field_ptr, runtime_inst, false);
1969519696 if (instr_is_comptime(field_ptr) && field_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) {
1969619697 const_ptrs.append(field_ptr);
1969719698 } else {
......@@ -19708,7 +19709,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1970819709 IrInstruction *field_result_loc = const_ptrs.at(i);
1970919710 IrInstruction *deref = ir_get_deref(ira, field_result_loc, field_result_loc, nullptr);
1971019711 field_result_loc->value.special = ConstValSpecialRuntime;
19711 ir_analyze_store_ptr(ira, field_result_loc, field_result_loc, deref);
19712 ir_analyze_store_ptr(ira, field_result_loc, field_result_loc, deref, false);
1971219713 }
1971319714 }
1971419715 }
......@@ -19835,7 +19836,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1983519836 assert(elem_result_loc->value.special == ConstValSpecialStatic);
1983619837 IrInstruction *deref = ir_get_deref(ira, elem_result_loc, elem_result_loc, nullptr);
1983719838 elem_result_loc->value.special = ConstValSpecialRuntime;
19838 ir_analyze_store_ptr(ira, elem_result_loc, elem_result_loc, deref);
19839 ir_analyze_store_ptr(ira, elem_result_loc, elem_result_loc, deref, false);
1983919840 }
1984019841 }
1984119842 }
......@@ -25418,7 +25419,8 @@ static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstruct
2541825419 return result_loc;
2541925420
2542025421 if (!was_written) {
25421 IrInstruction *store_ptr = ir_analyze_store_ptr(ira, &instruction->base, result_loc, value);
25422 IrInstruction *store_ptr = ir_analyze_store_ptr(ira, &instruction->base, result_loc, value,
25423 instruction->result_loc->allow_write_through_const);
2542225424 if (type_is_invalid(store_ptr->value.type)) {
2542325425 return ira->codegen->invalid_instruction;
2542425426 }
test/compile_errors.zig+8-8
......@@ -201,7 +201,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
201201 \\ return error.OutOfMemory;
202202 \\}
203203 ,
204 "tmp.zig:2:7: error: error is discarded",
204 "tmp.zig:2:12: error: error is discarded",
205205 );
206206
207207 cases.add(
......@@ -2740,7 +2740,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27402740 \\ 3 = 3;
27412741 \\}
27422742 ,
2743 "tmp.zig:2:7: error: cannot assign to constant",
2743 "tmp.zig:2:9: error: cannot assign to constant",
27442744 );
27452745
27462746 cases.add(
......@@ -2750,7 +2750,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27502750 \\ a = 4;
27512751 \\}
27522752 ,
2753 "tmp.zig:3:7: error: cannot assign to constant",
2753 "tmp.zig:3:9: error: cannot assign to constant",
27542754 );
27552755
27562756 cases.add(
......@@ -2820,7 +2820,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
28202820 \\}
28212821 \\export fn entry() void { f(); }
28222822 ,
2823 "tmp.zig:3:7: error: cannot assign to constant",
2823 "tmp.zig:3:9: error: cannot assign to constant",
28242824 );
28252825
28262826 cases.add(
......@@ -3883,7 +3883,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38833883 \\
38843884 \\export fn entry() usize { return @sizeOf(@typeOf(a)); }
38853885 ,
3886 "tmp.zig:6:24: error: unable to evaluate constant expression",
3886 "tmp.zig:6:26: error: unable to evaluate constant expression",
38873887 "tmp.zig:4:17: note: called from here",
38883888 );
38893889
......@@ -4133,7 +4133,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41334133 \\ cstr[0] = 'W';
41344134 \\}
41354135 ,
4136 "tmp.zig:3:11: error: cannot assign to constant",
4136 "tmp.zig:3:13: error: cannot assign to constant",
41374137 );
41384138
41394139 cases.add(
......@@ -4143,7 +4143,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41434143 \\ cstr[0] = 'W';
41444144 \\}
41454145 ,
4146 "tmp.zig:3:11: error: cannot assign to constant",
4146 "tmp.zig:3:13: error: cannot assign to constant",
41474147 );
41484148
41494149 cases.add(
......@@ -4291,7 +4291,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42914291 \\ f.field = 0;
42924292 \\}
42934293 ,
4294 "tmp.zig:6:13: error: cannot assign to constant",
4294 "tmp.zig:6:15: error: cannot assign to constant",
42954295 );
42964296
42974297 cases.add(