| ... | @@ -8431,8 +8431,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru | ... | @@ -8431,8 +8431,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru |
| 8431 | | 8431 | |
| 8432 | IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count); | 8432 | IrInstructionStructInitField *new_fields = allocate<IrInstructionStructInitField>(actual_field_count); |
| 8433 | | 8433 | |
| 8434 | FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec); | 8434 | bool is_comptime = ir_should_inline(&ira->new_irb); |
| 8435 | bool outside_fn = (fn_entry == nullptr); | | |
| 8436 | | 8435 | |
| 8437 | ConstExprValue const_val = {}; | 8436 | ConstExprValue const_val = {}; |
| 8438 | const_val.special = ConstValSpecialStatic; | 8437 | const_val.special = ConstValSpecialStatic; |
| ... | @@ -8456,6 +8455,10 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru | ... | @@ -8456,6 +8455,10 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru |
| 8456 | if (type_field->type_entry->id == TypeTableEntryIdInvalid) | 8455 | if (type_field->type_entry->id == TypeTableEntryIdInvalid) |
| 8457 | return ira->codegen->builtin_types.entry_invalid; | 8456 | return ira->codegen->builtin_types.entry_invalid; |
| 8458 | | 8457 | |
| | 8458 | IrInstruction *casted_field_value = ir_implicit_cast(ira, field_value, type_field->type_entry); |
| | 8459 | if (casted_field_value == ira->codegen->invalid_instruction) |
| | 8460 | return ira->codegen->builtin_types.entry_invalid; |
| | 8461 | |
| 8459 | size_t field_index = type_field->src_index; | 8462 | size_t field_index = type_field->src_index; |
| 8460 | AstNode *existing_assign_node = field_assign_nodes[field_index]; | 8463 | AstNode *existing_assign_node = field_assign_nodes[field_index]; |
| 8461 | if (existing_assign_node) { | 8464 | if (existing_assign_node) { |
| ... | @@ -8465,19 +8468,19 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru | ... | @@ -8465,19 +8468,19 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru |
| 8465 | } | 8468 | } |
| 8466 | field_assign_nodes[field_index] = field->source_node; | 8469 | field_assign_nodes[field_index] = field->source_node; |
| 8467 | | 8470 | |
| 8468 | new_fields[field_index].value = field_value; | 8471 | new_fields[field_index].value = casted_field_value; |
| 8469 | new_fields[field_index].type_struct_field = type_field; | 8472 | new_fields[field_index].type_struct_field = type_field; |
| 8470 | | 8473 | |
| 8471 | if (const_val.special == ConstValSpecialStatic) { | 8474 | if (const_val.special == ConstValSpecialStatic) { |
| 8472 | if (outside_fn || field_value->static_value.special != ConstValSpecialRuntime) { | 8475 | if (is_comptime || casted_field_value->static_value.special != ConstValSpecialRuntime) { |
| 8473 | ConstExprValue *field_val = ir_resolve_const(ira, field_value, UndefOk); | 8476 | ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk); |
| 8474 | if (!field_val) | 8477 | if (!field_val) |
| 8475 | return ira->codegen->builtin_types.entry_invalid; | 8478 | return ira->codegen->builtin_types.entry_invalid; |
| 8476 | | 8479 | |
| 8477 | const_val.data.x_struct.fields[field_index] = *field_val; | 8480 | const_val.data.x_struct.fields[field_index] = *field_val; |
| 8478 | const_val.depends_on_compile_var = const_val.depends_on_compile_var || field_val->depends_on_compile_var; | 8481 | const_val.depends_on_compile_var = const_val.depends_on_compile_var || field_val->depends_on_compile_var; |
| 8479 | } else { | 8482 | } else { |
| 8480 | first_non_const_instruction = field_value; | 8483 | first_non_const_instruction = casted_field_value; |
| 8481 | const_val.special = ConstValSpecialRuntime; | 8484 | const_val.special = ConstValSpecialRuntime; |
| 8482 | } | 8485 | } |
| 8483 | } | 8486 | } |
| ... | @@ -8500,7 +8503,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru | ... | @@ -8500,7 +8503,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru |
| 8500 | return container_type; | 8503 | return container_type; |
| 8501 | } | 8504 | } |
| 8502 | | 8505 | |
| 8503 | if (outside_fn) { | 8506 | if (is_comptime) { |
| 8504 | ir_add_error_node(ira, first_non_const_instruction->source_node, | 8507 | ir_add_error_node(ira, first_non_const_instruction->source_node, |
| 8505 | buf_sprintf("unable to evaluate constant expression")); | 8508 | buf_sprintf("unable to evaluate constant expression")); |
| 8506 | return ira->codegen->builtin_types.entry_invalid; | 8509 | return ira->codegen->builtin_types.entry_invalid; |
| ... | @@ -8513,7 +8516,9 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru | ... | @@ -8513,7 +8516,9 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru |
| 8513 | return container_type; | 8516 | return container_type; |
| 8514 | } | 8517 | } |
| 8515 | | 8518 | |
| 8516 | static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira, IrInstructionContainerInitList *instruction) { | 8519 | static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira, |
| | 8520 | IrInstructionContainerInitList *instruction) |
| | 8521 | { |
| 8517 | IrInstruction *container_type_value = instruction->container_type->other; | 8522 | IrInstruction *container_type_value = instruction->container_type->other; |
| 8518 | if (container_type_value->type_entry->id == TypeTableEntryIdInvalid) | 8523 | if (container_type_value->type_entry->id == TypeTableEntryIdInvalid) |
| 8519 | return ira->codegen->builtin_types.entry_invalid; | 8524 | return ira->codegen->builtin_types.entry_invalid; |
| ... | @@ -8527,7 +8532,8 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira | ... | @@ -8527,7 +8532,8 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira |
| 8527 | bool depends_on_compile_var = container_type_value->static_value.depends_on_compile_var; | 8532 | bool depends_on_compile_var = container_type_value->static_value.depends_on_compile_var; |
| 8528 | | 8533 | |
| 8529 | if (container_type->id == TypeTableEntryIdStruct && !is_slice(container_type) && elem_count == 0) { | 8534 | if (container_type->id == TypeTableEntryIdStruct && !is_slice(container_type) && elem_count == 0) { |
| 8530 | return ir_analyze_container_init_fields(ira, &instruction->base, container_type, 0, nullptr, depends_on_compile_var); | 8535 | return ir_analyze_container_init_fields(ira, &instruction->base, container_type, |
| | 8536 | 0, nullptr, depends_on_compile_var); |
| 8531 | } else if (is_slice(container_type)) { | 8537 | } else if (is_slice(container_type)) { |
| 8532 | TypeTableEntry *pointer_type = container_type->data.structure.fields[slice_ptr_index].type_entry; | 8538 | TypeTableEntry *pointer_type = container_type->data.structure.fields[slice_ptr_index].type_entry; |
| 8533 | assert(pointer_type->id == TypeTableEntryIdPointer); | 8539 | assert(pointer_type->id == TypeTableEntryIdPointer); |
| ... | @@ -8539,8 +8545,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira | ... | @@ -8539,8 +8545,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira |
| 8539 | const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count); | 8545 | const_val.data.x_array.elements = allocate<ConstExprValue>(elem_count); |
| 8540 | const_val.data.x_array.size = elem_count; | 8546 | const_val.data.x_array.size = elem_count; |
| 8541 | | 8547 | |
| 8542 | FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec); | 8548 | bool is_comptime = ir_should_inline(&ira->new_irb); |
| 8543 | bool outside_fn = (fn_entry == nullptr); | | |
| 8544 | | 8549 | |
| 8545 | IrInstruction **new_items = allocate<IrInstruction *>(elem_count); | 8550 | IrInstruction **new_items = allocate<IrInstruction *>(elem_count); |
| 8546 | | 8551 | |
| ... | @@ -8551,18 +8556,22 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira | ... | @@ -8551,18 +8556,22 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira |
| 8551 | if (arg_value->type_entry->id == TypeTableEntryIdInvalid) | 8556 | if (arg_value->type_entry->id == TypeTableEntryIdInvalid) |
| 8552 | return ira->codegen->builtin_types.entry_invalid; | 8557 | return ira->codegen->builtin_types.entry_invalid; |
| 8553 | | 8558 | |
| 8554 | new_items[i] = arg_value; | 8559 | IrInstruction *casted_arg = ir_implicit_cast(ira, arg_value, child_type); |
| | 8560 | if (casted_arg == ira->codegen->invalid_instruction) |
| | 8561 | return ira->codegen->builtin_types.entry_invalid; |
| | 8562 | |
| | 8563 | new_items[i] = casted_arg; |
| 8555 | | 8564 | |
| 8556 | if (const_val.special == ConstValSpecialStatic) { | 8565 | if (const_val.special == ConstValSpecialStatic) { |
| 8557 | if (outside_fn || arg_value->static_value.special != ConstValSpecialRuntime) { | 8566 | if (is_comptime || casted_arg->static_value.special != ConstValSpecialRuntime) { |
| 8558 | ConstExprValue *elem_val = ir_resolve_const(ira, arg_value, UndefBad); | 8567 | ConstExprValue *elem_val = ir_resolve_const(ira, casted_arg, UndefBad); |
| 8559 | if (!elem_val) | 8568 | if (!elem_val) |
| 8560 | return ira->codegen->builtin_types.entry_invalid; | 8569 | return ira->codegen->builtin_types.entry_invalid; |
| 8561 | | 8570 | |
| 8562 | const_val.data.x_array.elements[i] = *elem_val; | 8571 | const_val.data.x_array.elements[i] = *elem_val; |
| 8563 | const_val.depends_on_compile_var = const_val.depends_on_compile_var || elem_val->depends_on_compile_var; | 8572 | const_val.depends_on_compile_var = const_val.depends_on_compile_var || elem_val->depends_on_compile_var; |
| 8564 | } else { | 8573 | } else { |
| 8565 | first_non_const_instruction = arg_value; | 8574 | first_non_const_instruction = casted_arg; |
| 8566 | const_val.special = ConstValSpecialRuntime; | 8575 | const_val.special = ConstValSpecialRuntime; |
| 8567 | } | 8576 | } |
| 8568 | } | 8577 | } |
| ... | @@ -8575,7 +8584,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira | ... | @@ -8575,7 +8584,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira |
| 8575 | return fixed_size_array_type; | 8584 | return fixed_size_array_type; |
| 8576 | } | 8585 | } |
| 8577 | | 8586 | |
| 8578 | if (outside_fn) { | 8587 | if (is_comptime) { |
| 8579 | ir_add_error_node(ira, first_non_const_instruction->source_node, | 8588 | ir_add_error_node(ira, first_non_const_instruction->source_node, |
| 8580 | buf_sprintf("unable to evaluate constant expression")); | 8589 | buf_sprintf("unable to evaluate constant expression")); |
| 8581 | return ira->codegen->builtin_types.entry_invalid; | 8590 | return ira->codegen->builtin_types.entry_invalid; |
| ... | @@ -8602,10 +8611,8 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira | ... | @@ -8602,10 +8611,8 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira |
| 8602 | return ira->codegen->builtin_types.entry_invalid; | 8611 | return ira->codegen->builtin_types.entry_invalid; |
| 8603 | } | 8612 | } |
| 8604 | } else if (container_type_value->type_entry->id == TypeTableEntryIdEnumTag) { | 8613 | } else if (container_type_value->type_entry->id == TypeTableEntryIdEnumTag) { |
| 8605 | // TODO I wrote this commit message when I had some sake | | |
| 8606 | // might be worth re-examining sober | | |
| 8607 | if (elem_count != 1) { | 8614 | if (elem_count != 1) { |
| 8608 | ir_add_error(ira, &instruction->base, buf_sprintf("expected 1 elment")); | 8615 | ir_add_error(ira, &instruction->base, buf_sprintf("enum initialization requires exactly one element")); |
| 8609 | return ira->codegen->builtin_types.entry_invalid; | 8616 | return ira->codegen->builtin_types.entry_invalid; |
| 8610 | } | 8617 | } |
| 8611 | ConstExprValue *tag_value = ir_resolve_const(ira, container_type_value, UndefBad); | 8618 | ConstExprValue *tag_value = ir_resolve_const(ira, container_type_value, UndefBad); |