move array and struct const checks to more appropriate places

This commit is contained in:
Vexu 2020-04-08 14:20:05 +03:00
parent ff0f97a1bc
commit b1e44adcba
No known key found for this signature in database
GPG key ID: 59AEB8936E16A6AC
2 changed files with 15 additions and 16 deletions

View file

@ -23552,10 +23552,14 @@ static IrInstGen *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
IrInstGen *result_loc = instruction->result_loc->child;
if (type_is_invalid(result_loc->value->type))
return result_loc;
ir_assert(result_loc->value->type->id == ZigTypeIdPointer, &instruction->base.base);
if (result_loc->value->type->data.pointer.is_const) {
ir_add_error(ira, &instruction->base.base, buf_sprintf("cannot assign to constant"));
return ira->codegen->invalid_inst_gen;
}
ZigType *container_type = result_loc->value->type->data.pointer.child_type;
size_t elem_count = instruction->item_count;
if (is_slice(container_type)) {
@ -23706,6 +23710,11 @@ static IrInstGen *ir_analyze_instruction_container_init_fields(IrAnalyze *ira,
return result_loc;
ir_assert(result_loc->value->type->id == ZigTypeIdPointer, &instruction->base.base);
if (result_loc->value->type->data.pointer.is_const) {
ir_add_error(ira, &instruction->base.base, buf_sprintf("cannot assign to constant"));
return ira->codegen->invalid_inst_gen;
}
ZigType *container_type = result_loc->value->type->data.pointer.child_type;
return ir_analyze_container_init_fields(ira, &instruction->base.base, container_type,
@ -27176,11 +27185,8 @@ done_with_return_type:
return result_loc;
}
if (result_loc->value->type->id == ZigTypeIdPointer &&
result_loc->value->type->data.pointer.is_const &&
instruction->result_loc->id == ResultLocIdInstruction &&
!instruction->result_loc->allow_write_through_const)
{
ir_assert(result_loc->value->type->id == ZigTypeIdPointer, &instruction->base.base);
if (result_loc->value->type->data.pointer.is_const) {
ir_add_error(ira, &instruction->base.base, buf_sprintf("cannot assign to constant"));
return ira->codegen->invalid_inst_gen;
}
@ -29918,15 +29924,8 @@ static IrInstGen *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstSrcEndEx
return result_loc;
if (!was_written || instruction->result_loc->id == ResultLocIdPeer) {
bool can_write_to_const_ptr = true;
if (result_loc->value->type->id == ZigTypeIdPointer &&
result_loc->value->type->data.pointer.is_const &&
instruction->result_loc->id == ResultLocIdInstruction)
{
can_write_to_const_ptr = false;
}
IrInstGen *store_ptr = ir_analyze_store_ptr(ira, &instruction->base.base, result_loc, value,
instruction->result_loc->allow_write_through_const && can_write_to_const_ptr);
instruction->result_loc->allow_write_through_const);
if (type_is_invalid(store_ptr->value->type)) {
return ira->codegen->invalid_inst_gen;
}

View file

@ -10,7 +10,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ reassign(.{1, 2, 3});
\\}
, &[_][]const u8{
"tmp.zig:2:16: error: cannot assign to constant"
"tmp.zig:2:15: error: cannot assign to constant"
});
cases.addTest("reassign to slice parameter",
@ -35,7 +35,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ reassign(S{.x = 3});
\\}
, &[_][]const u8{
"tmp.zig:5:16: error: cannot assign to constant"
"tmp.zig:5:10: error: cannot assign to constant"
});
cases.addTest("reference to const data",