| ... | ... | @@ -7843,7 +7843,7 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou |
| 7843 | 7843 | bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 |
| 7844 | 7844 | || !actual_type->data.pointer.is_const); |
| 7845 | 7845 | |
| 7846 | | if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type, |
| 7846 | if (types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type, |
| 7847 | 7847 | array_type->data.array.child_type, source_node, |
| 7848 | 7848 | !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk && |
| 7849 | 7849 | (slice_ptr_type->data.pointer.sentinel == nullptr || |
| ... | ... | @@ -7851,6 +7851,14 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou |
| 7851 | 7851 | const_values_equal(ira->codegen, array_type->data.array.sentinel, |
| 7852 | 7852 | slice_ptr_type->data.pointer.sentinel)))) |
| 7853 | 7853 | { |
| 7854 | if (!const_ok) { |
| 7855 | ErrorMsg *msg = ir_add_error_node(ira, source_node, |
| 7856 | buf_sprintf("cannot cast pointer to array literal to slice type '%s'", |
| 7857 | buf_ptr(&wanted_type->name))); |
| 7858 | add_error_note(ira->codegen, msg, source_node, |
| 7859 | buf_sprintf("cast discards const qualifier")); |
| 7860 | return ira->codegen->invalid_inst_gen; |
| 7861 | } |
| 7854 | 7862 | // If the pointers both have ABI align, it works. |
| 7855 | 7863 | // Or if the array length is 0, alignment doesn't matter. |
| 7856 | 7864 | bool ok_align = array_type->data.array.len == 0 || |
| ... | ... | @@ -8208,8 +8216,16 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou |
| 8208 | 8216 | ZigType *wanted_child = wanted_type->data.pointer.child_type; |
| 8209 | 8217 | bool const_ok = (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const); |
| 8210 | 8218 | if (wanted_child->id == ZigTypeIdArray && (is_array_init || field_count == 0) && |
| 8211 | | wanted_child->data.array.len == field_count && (const_ok || field_count == 0)) |
| 8219 | wanted_child->data.array.len == field_count) |
| 8212 | 8220 | { |
| 8221 | if (!const_ok && field_count != 0) { |
| 8222 | ErrorMsg *msg = ir_add_error_node(ira, source_node, |
| 8223 | buf_sprintf("cannot cast pointer to array literal to '%s'", |
| 8224 | buf_ptr(&wanted_type->name))); |
| 8225 | add_error_note(ira->codegen, msg, source_node, |
| 8226 | buf_sprintf("cast discards const qualifier")); |
| 8227 | return ira->codegen->invalid_inst_gen; |
| 8228 | } |
| 8213 | 8229 | Stage1AirInst *res = ir_analyze_struct_literal_to_array(ira, scope, source_node, value, anon_type, wanted_child); |
| 8214 | 8230 | if (res->value->type->id == ZigTypeIdPointer) |
| 8215 | 8231 | return res; |
| ... | ... | @@ -8241,6 +8257,13 @@ static Stage1AirInst *ir_analyze_cast(IrAnalyze *ira, Scope *scope, AstNode *sou |
| 8241 | 8257 | res = ir_get_ref(ira, scope, source_node, res, actual_type->data.pointer.is_const, actual_type->data.pointer.is_volatile); |
| 8242 | 8258 | |
| 8243 | 8259 | return ir_resolve_ptr_of_array_to_slice(ira, scope, source_node, res, wanted_type, nullptr); |
| 8260 | } else if (!slice_type->data.pointer.is_const && actual_type->data.pointer.is_const && field_count != 0) { |
| 8261 | ErrorMsg *msg = ir_add_error_node(ira, source_node, |
| 8262 | buf_sprintf("cannot cast pointer to array literal to slice type '%s'", |
| 8263 | buf_ptr(&wanted_type->name))); |
| 8264 | add_error_note(ira->codegen, msg, source_node, |
| 8265 | buf_sprintf("cast discards const qualifier")); |
| 8266 | return ira->codegen->invalid_inst_gen; |
| 8244 | 8267 | } |
| 8245 | 8268 | } |
| 8246 | 8269 | } |
| ... | ... | @@ -15068,7 +15091,7 @@ static Stage1AirInst *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, Stage1ZirI |
| 15068 | 15091 | return ira->codegen->invalid_inst_gen; |
| 15069 | 15092 | if (actual_array_type->id != ZigTypeIdArray) { |
| 15070 | 15093 | ir_add_error_node(ira, elem_ptr_instruction->init_array_type_source_node, |
| 15071 | | buf_sprintf("array literal requires address-of operator to coerce to slice type '%s'", |
| 15094 | buf_sprintf("array literal requires address-of operator (&) to coerce to slice type '%s'", |
| 15072 | 15095 | buf_ptr(&actual_array_type->name))); |
| 15073 | 15096 | return ira->codegen->invalid_inst_gen; |
| 15074 | 15097 | } |
| ... | ... | @@ -17473,7 +17496,7 @@ static Stage1AirInst *ir_analyze_instruction_container_init_list(IrAnalyze *ira, |
| 17473 | 17496 | |
| 17474 | 17497 | if (is_slice(container_type)) { |
| 17475 | 17498 | ir_add_error_node(ira, instruction->init_array_type_source_node, |
| 17476 | | buf_sprintf("array literal requires address-of operator to coerce to slice type '%s'", |
| 17499 | buf_sprintf("array literal requires address-of operator (&) to coerce to slice type '%s'", |
| 17477 | 17500 | buf_ptr(&container_type->name))); |
| 17478 | 17501 | return ira->codegen->invalid_inst_gen; |
| 17479 | 17502 | } |