Skip to content

Commit b1e44ad

Browse files
committed
move array and struct const checks to more appropriate places
1 parent ff0f97a commit b1e44ad

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/ir.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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",

0 commit comments

Comments
 (0)