authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-08 14:20:05+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-08 14:32:02+03:00
logb1e44adcba55e3839a3676db83ff61c344f3bbad
treecc2541bfa61def94dba357e08e29310da3cdb39f
parentff0f97a1bcab4d27212c67b0ebc6233adf2f0de9
signaturelock-open Commit is signed but in an unrecognized format.

move array and struct const checks to more appropriate places


2 files changed, 15 insertions(+), 16 deletions(-)

src/ir.cpp+13-14
......@@ -23552,10 +23552,14 @@ static IrInstGen *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
2355223552 IrInstGen *result_loc = instruction->result_loc->child;
2355323553 if (type_is_invalid(result_loc->value->type))
2355423554 return result_loc;
23555
2355523556 ir_assert(result_loc->value->type->id == ZigTypeIdPointer, &instruction->base.base);
23557 if (result_loc->value->type->data.pointer.is_const) {
23558 ir_add_error(ira, &instruction->base.base, buf_sprintf("cannot assign to constant"));
23559 return ira->codegen->invalid_inst_gen;
23560 }
2355623561
2355723562 ZigType *container_type = result_loc->value->type->data.pointer.child_type;
23558
2355923563 size_t elem_count = instruction->item_count;
2356023564
2356123565 if (is_slice(container_type)) {
......@@ -23706,6 +23710,11 @@ static IrInstGen *ir_analyze_instruction_container_init_fields(IrAnalyze *ira,
2370623710 return result_loc;
2370723711
2370823712 ir_assert(result_loc->value->type->id == ZigTypeIdPointer, &instruction->base.base);
23713 if (result_loc->value->type->data.pointer.is_const) {
23714 ir_add_error(ira, &instruction->base.base, buf_sprintf("cannot assign to constant"));
23715 return ira->codegen->invalid_inst_gen;
23716 }
23717
2370923718 ZigType *container_type = result_loc->value->type->data.pointer.child_type;
2371023719
2371123720 return ir_analyze_container_init_fields(ira, &instruction->base.base, container_type,
......@@ -27176,11 +27185,8 @@ done_with_return_type:
2717627185 return result_loc;
2717727186 }
2717827187
27179 if (result_loc->value->type->id == ZigTypeIdPointer &&
27180 result_loc->value->type->data.pointer.is_const &&
27181 instruction->result_loc->id == ResultLocIdInstruction &&
27182 !instruction->result_loc->allow_write_through_const)
27183 {
27188 ir_assert(result_loc->value->type->id == ZigTypeIdPointer, &instruction->base.base);
27189 if (result_loc->value->type->data.pointer.is_const) {
2718427190 ir_add_error(ira, &instruction->base.base, buf_sprintf("cannot assign to constant"));
2718527191 return ira->codegen->invalid_inst_gen;
2718627192 }
......@@ -29918,15 +29924,8 @@ static IrInstGen *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstSrcEndEx
2991829924 return result_loc;
2991929925
2992029926 if (!was_written || instruction->result_loc->id == ResultLocIdPeer) {
29921 bool can_write_to_const_ptr = true;
29922 if (result_loc->value->type->id == ZigTypeIdPointer &&
29923 result_loc->value->type->data.pointer.is_const &&
29924 instruction->result_loc->id == ResultLocIdInstruction)
29925 {
29926 can_write_to_const_ptr = false;
29927 }
2992829927 IrInstGen *store_ptr = ir_analyze_store_ptr(ira, &instruction->base.base, result_loc, value,
29929 instruction->result_loc->allow_write_through_const && can_write_to_const_ptr);
29928 instruction->result_loc->allow_write_through_const);
2993029929 if (type_is_invalid(store_ptr->value->type)) {
2993129930 return ira->codegen->invalid_inst_gen;
2993229931 }
test/compile_errors.zig+2-2
......@@ -10,7 +10,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1010 \\ reassign(.{1, 2, 3});
1111 \\}
1212 , &[_][]const u8{
13 "tmp.zig:2:16: error: cannot assign to constant"
13 "tmp.zig:2:15: error: cannot assign to constant"
1414 });
1515
1616 cases.addTest("reassign to slice parameter",
......@@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3535 \\ reassign(S{.x = 3});
3636 \\}
3737 , &[_][]const u8{
38 "tmp.zig:5:16: error: cannot assign to constant"
38 "tmp.zig:5:10: error: cannot assign to constant"
3939 });
4040
4141 cases.addTest("reference to const data",