authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 16:10:45-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 16:10:45-04:00
logb2d94f9af2968e01bd3d8db38c9ae1992bbd3678
tree3b1b518ebad8684cfa4f1a9f6f4f5a65830ecbcf
parent6cb4cac5cd1c68f41c62a3c23b09513988337c8d
parentd105769926fd5360a5309be3e202cc65d32ce604
signature Commit is signed but in an unrecognized format.

Merge branch 'assignment-result-loc'

With this merge, assignments participate in result location semantics.

7 files changed, 128 insertions(+), 57 deletions(-)

src/all_types.hpp+2
......@@ -2543,6 +2543,7 @@ struct IrInstructionLoadPtrGen {
25432543struct IrInstructionStorePtr {
25442544 IrInstruction base;
25452545
2546 bool allow_write_through_const;
25462547 IrInstruction *ptr;
25472548 IrInstruction *value;
25482549};
......@@ -3707,6 +3708,7 @@ enum ResultLocId {
37073708struct ResultLoc {
37083709 ResultLocId id;
37093710 bool written;
3711 bool allow_write_through_const;
37103712 IrInstruction *resolved_loc; // result ptr
37113713 IrInstruction *source_instruction;
37123714 IrInstruction *gen_instruction; // value to store to the result loc
src/codegen.cpp+7-1
......@@ -4458,8 +4458,14 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
44584458 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(get_llvm_type(g, child_type)), payload_val, "");
44594459 }
44604460
4461 // When the cmpxchg is discarded, the result location will have no bits.
4462 if (!type_has_bits(instruction->result_loc->value.type)) {
4463 return nullptr;
4464 }
4465
44614466 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
4462 assert(type_has_bits(child_type));
4467 src_assert(result_loc != nullptr, instruction->base.source_node);
4468 src_assert(type_has_bits(child_type), instruction->base.source_node);
44634469
44644470 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
44654471 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_child_index, "");
src/ir.cpp+72-48
......@@ -189,7 +189,8 @@ static IrInstruction *ir_analyze_bit_cast(IrAnalyze *ira, IrInstruction *source_
189189static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspend_source_instr,
190190 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime, bool non_null_comptime);
191191static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,
192 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime, bool non_null_comptime);
192 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value, bool force_runtime,
193 bool non_null_comptime, bool allow_discard);
193194static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstruction *source_instr,
194195 IrInstruction *base_ptr, bool safety_check_on, bool initializing);
195196static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,
......@@ -197,7 +198,7 @@ static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruct
197198static IrInstruction *ir_analyze_unwrap_err_code(IrAnalyze *ira, IrInstruction *source_instr,
198199 IrInstruction *base_ptr, bool initializing);
199200static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
200 IrInstruction *ptr, IrInstruction *uncasted_value);
201 IrInstruction *ptr, IrInstruction *uncasted_value, bool allow_write_through_const);
201202static IrInstruction *ir_gen_union_init_expr(IrBuilder *irb, Scope *scope, AstNode *source_node,
202203 IrInstruction *union_type, IrInstruction *field_name, AstNode *expr_node,
203204 LVal lval, ResultLoc *parent_result_loc);
......@@ -1612,7 +1613,7 @@ static IrInstruction *ir_build_unreachable(IrBuilder *irb, Scope *scope, AstNode
16121613 return &unreachable_instruction->base;
16131614}
16141615
1615static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
1616static IrInstructionStorePtr *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
16161617 IrInstruction *ptr, IrInstruction *value)
16171618{
16181619 IrInstructionStorePtr *instruction = ir_build_instruction<IrInstructionStorePtr>(irb, scope, source_node);
......@@ -1624,7 +1625,7 @@ static IrInstruction *ir_build_store_ptr(IrBuilder *irb, Scope *scope, AstNode *
16241625 ir_ref_instruction(ptr, irb->current_basic_block);
16251626 ir_ref_instruction(value, irb->current_basic_block);
16261627
1627 return &instruction->base;
1628 return instruction;
16281629}
16291630
16301631static IrInstruction *ir_build_var_decl_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
......@@ -4001,12 +4002,20 @@ static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *no
40014002
40024003static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {
40034004 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
4004 IrInstruction *rvalue = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
4005 if (lvalue == irb->codegen->invalid_instruction)
4006 return irb->codegen->invalid_instruction;
4007
4008 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
4009 result_loc_inst->base.id = ResultLocIdInstruction;
4010 result_loc_inst->base.source_instruction = lvalue;
4011 ir_ref_instruction(lvalue, irb->current_basic_block);
4012 ir_build_reset_result(irb, scope, node, &result_loc_inst->base);
40054013
4006 if (lvalue == irb->codegen->invalid_instruction || rvalue == irb->codegen->invalid_instruction)
4014 IrInstruction *rvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op2, scope, LValNone,
4015 &result_loc_inst->base);
4016 if (rvalue == irb->codegen->invalid_instruction)
40074017 return irb->codegen->invalid_instruction;
40084018
4009 ir_build_store_ptr(irb, scope, node, lvalue, rvalue);
40104019 return ir_build_const_void(irb, scope, node);
40114020}
40124021
......@@ -6042,6 +6051,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
60426051 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
60436052 result_loc_inst->base.id = ResultLocIdInstruction;
60446053 result_loc_inst->base.source_instruction = field_ptr;
6054 result_loc_inst->base.allow_write_through_const = true;
60456055 ir_ref_instruction(field_ptr, irb->current_basic_block);
60466056 ir_build_reset_result(irb, scope, expr_node, &result_loc_inst->base);
60476057
......@@ -6080,6 +6090,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
60806090 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
60816091 result_loc_inst->base.id = ResultLocIdInstruction;
60826092 result_loc_inst->base.source_instruction = elem_ptr;
6093 result_loc_inst->base.allow_write_through_const = true;
60836094 ir_ref_instruction(elem_ptr, irb->current_basic_block);
60846095 ir_build_reset_result(irb, scope, expr_node, &result_loc_inst->base);
60856096
......@@ -6637,7 +6648,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
66376648
66386649 ir_set_cursor_at_end_and_append_block(irb, continue_block);
66396650 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);
6640 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val));
6651 ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val)->allow_write_through_const = true;
66416652 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
66426653
66436654 IrInstruction *else_result = nullptr;
......@@ -11155,7 +11166,8 @@ static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruc
1115511166 }
1115611167
1115711168 if (result_loc == nullptr) result_loc = no_result_loc();
11158 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11169 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true,
11170 false, true);
1115911171 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1116011172 return result_loc_inst;
1116111173 }
......@@ -11615,7 +11627,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
1161511627 }
1161611628 IrInstruction *result_loc_inst = nullptr;
1161711629 if (result_loc != nullptr) {
11618 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11630 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false, true);
1161911631 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1162011632 return result_loc_inst;
1162111633 }
......@@ -11658,7 +11670,7 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction
1165811670 IrInstruction *result_loc_inst;
1165911671 if (handle_is_ptr(wanted_type)) {
1166011672 if (result_loc == nullptr) result_loc = no_result_loc();
11661 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11673 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false, true);
1166211674 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1166311675 return result_loc_inst;
1166411676 }
......@@ -11743,7 +11755,7 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
1174311755 IrInstruction *result_loc_inst;
1174411756 if (handle_is_ptr(wanted_type)) {
1174511757 if (result_loc == nullptr) result_loc = no_result_loc();
11746 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11758 result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false, true);
1174711759 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1174811760 return result_loc_inst;
1174911761 }
......@@ -11816,7 +11828,8 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi
1181611828
1181711829 IrInstruction *result_loc;
1181811830 if (type_has_bits(ptr_type) && !handle_is_ptr(value->value.type)) {
11819 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true, false);
11831 result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true,
11832 false, true);
1182011833 } else {
1182111834 result_loc = nullptr;
1182211835 }
......@@ -11860,7 +11873,8 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
1186011873 if (!array_ptr) array_ptr = ir_get_ref(ira, source_instr, array, true, false);
1186111874
1186211875 if (result_loc == nullptr) result_loc = no_result_loc();
11863 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr, true, false);
11876 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr,
11877 true, false, true);
1186411878 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1186511879 return result_loc_inst;
1186611880 }
......@@ -12516,7 +12530,8 @@ static IrInstruction *ir_analyze_vector_to_array(IrAnalyze *ira, IrInstruction *
1251612530 if (result_loc == nullptr) {
1251712531 result_loc = no_result_loc();
1251812532 }
12519 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr, true, false);
12533 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, array_type, nullptr,
12534 true, false, true);
1252012535 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1252112536 return result_loc_inst;
1252212537 }
......@@ -13097,7 +13112,8 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1309713112 IrInstruction *result_loc_inst;
1309813113 if (type_entry->data.pointer.host_int_bytes != 0 && handle_is_ptr(child_type)) {
1309913114 if (result_loc == nullptr) result_loc = no_result_loc();
13100 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr, true, false);
13115 result_loc_inst = ir_resolve_result(ira, source_instruction, result_loc, child_type, nullptr,
13116 true, false, true);
1310113117 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
1310213118 return result_loc_inst;
1310313119 }
......@@ -14834,7 +14850,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1483414850 // instruction.
1483514851 assert(deref->value.special != ConstValSpecialRuntime);
1483614852 var_ptr->value.special = ConstValSpecialRuntime;
14837 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref);
14853 ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref, false);
1483814854 }
1483914855
1484014856 if (var_ptr->value.special == ConstValSpecialStatic && var->mem_slot_index != SIZE_MAX) {
......@@ -15352,7 +15368,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1535215368
1535315369 if (peer_parent->peers.length == 1) {
1535415370 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15355 value_type, value, force_runtime, non_null_comptime);
15371 value_type, value, force_runtime, non_null_comptime, true);
1535615372 result_peer->suspend_pos.basic_block_index = SIZE_MAX;
1535715373 result_peer->suspend_pos.instruction_index = SIZE_MAX;
1535815374 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
......@@ -15372,7 +15388,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1537215388 if (peer_parent->skipped) {
1537315389 if (non_null_comptime) {
1537415390 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15375 value_type, value, force_runtime, non_null_comptime);
15391 value_type, value, force_runtime, non_null_comptime, true);
1537615392 }
1537715393 return nullptr;
1537815394 }
......@@ -15390,7 +15406,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1539015406 }
1539115407
1539215408 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
15393 peer_parent->resolved_type, nullptr, force_runtime, non_null_comptime);
15409 peer_parent->resolved_type, nullptr, force_runtime, non_null_comptime, true);
1539415410 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
1539515411 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
1539615412 {
......@@ -15440,7 +15456,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1544015456 }
1544115457
1544215458 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,
15443 dest_type, bitcasted_value, force_runtime, non_null_comptime);
15459 dest_type, bitcasted_value, force_runtime, non_null_comptime, true);
1544415460 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
1544515461 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
1544615462 {
......@@ -15469,8 +15485,15 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1546915485
1547015486static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_source_instr,
1547115487 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstruction *value, bool force_runtime,
15472 bool non_null_comptime)
15488 bool non_null_comptime, bool allow_discard)
1547315489{
15490 if (!allow_discard && result_loc_pass1->id == ResultLocIdInstruction &&
15491 instr_is_comptime(result_loc_pass1->source_instruction) &&
15492 result_loc_pass1->source_instruction->value.type->id == ZigTypeIdPointer &&
15493 result_loc_pass1->source_instruction->value.data.x_ptr.special == ConstPtrSpecialDiscard)
15494 {
15495 result_loc_pass1 = no_result_loc();
15496 }
1547415497 IrInstruction *result_loc = ir_resolve_result_raw(ira, suspend_source_instr, result_loc_pass1, value_type,
1547515498 value, force_runtime, non_null_comptime);
1547615499 if (result_loc == nullptr || (instr_is_unreachable(result_loc) || type_is_invalid(result_loc->value.type)))
......@@ -15525,7 +15548,7 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrIn
1552515548 if (type_is_invalid(implicit_elem_type))
1552615549 return ira->codegen->invalid_instruction;
1552715550 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
15528 implicit_elem_type, nullptr, false, true);
15551 implicit_elem_type, nullptr, false, true, true);
1552915552 if (result_loc != nullptr)
1553015553 return result_loc;
1553115554
......@@ -15534,7 +15557,7 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrIn
1553415557 instruction->result_loc->id == ResultLocIdReturn)
1553515558 {
1553615559 result_loc = ir_resolve_result(ira, &instruction->base, no_result_loc(),
15537 implicit_elem_type, nullptr, false, true);
15560 implicit_elem_type, nullptr, false, true, true);
1553815561 if (result_loc != nullptr &&
1553915562 (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)))
1554015563 {
......@@ -15623,7 +15646,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc
1562315646 ZigType *async_return_type = get_error_union_type(ira->codegen, alloc_fn_error_set_type, promise_type);
1562415647
1562515648 IrInstruction *result_loc = ir_resolve_result(ira, &call_instruction->base, no_result_loc(),
15626 async_return_type, nullptr, true, true);
15649 async_return_type, nullptr, true, true, false);
1562715650 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
1562815651 return result_loc;
1562915652 }
......@@ -15841,7 +15864,7 @@ no_mem_slot:
1584115864}
1584215865
1584315866static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
15844 IrInstruction *ptr, IrInstruction *uncasted_value)
15867 IrInstruction *ptr, IrInstruction *uncasted_value, bool allow_write_through_const)
1584515868{
1584615869 assert(ptr->value.type->id == ZigTypeIdPointer);
1584715870
......@@ -15857,7 +15880,7 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1585715880
1585815881 ZigType *child_type = ptr->value.type->data.pointer.child_type;
1585915882
15860 if (ptr->value.type->data.pointer.is_const && !source_instr->is_gen) {
15883 if (ptr->value.type->data.pointer.is_const && !allow_write_through_const) {
1586115884 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
1586215885 return ira->codegen->invalid_instruction;
1586315886 }
......@@ -15936,10 +15959,9 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
1593615959 break;
1593715960 }
1593815961
15939 IrInstruction *result = ir_build_store_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node,
15940 ptr, value);
15941 result->value.type = ira->codegen->builtin_types.entry_void;
15942 return result;
15962 IrInstructionStorePtr *store_ptr = ir_build_store_ptr(&ira->new_irb, source_instr->scope,
15963 source_instr->source_node, ptr, value);
15964 return &store_ptr->base;
1594315965}
1594415966
1594515967static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *call_instruction,
......@@ -16382,7 +16404,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1638216404 IrInstruction *result_loc;
1638316405 if (handle_is_ptr(impl_fn_type_id->return_type)) {
1638416406 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
16385 impl_fn_type_id->return_type, nullptr, true, true);
16407 impl_fn_type_id->return_type, nullptr, true, true, false);
1638616408 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) ||
1638716409 instr_is_unreachable(result_loc)))
1638816410 {
......@@ -16504,7 +16526,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1650416526 IrInstruction *result_loc;
1650516527 if (handle_is_ptr(return_type)) {
1650616528 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
16507 return_type, nullptr, true, true);
16529 return_type, nullptr, true, true, false);
1650816530 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {
1650916531 return result_loc;
1651016532 }
......@@ -17020,7 +17042,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1702017042
1702117043 // In case resolving the parent activates a suspend, do it now
1702217044 IrInstruction *parent_result_loc = ir_resolve_result(ira, &phi_instruction->base, peer_parent->parent,
17023 peer_parent->resolved_type, nullptr, false, false);
17045 peer_parent->resolved_type, nullptr, false, false, true);
1702417046 if (parent_result_loc != nullptr &&
1702517047 (type_is_invalid(parent_result_loc->value.type) || instr_is_unreachable(parent_result_loc)))
1702617048 {
......@@ -17477,6 +17499,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1747717499 return result;
1747817500 } else if (is_slice(array_type)) {
1747917501 ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index];
17502 ir_assert(ptr_field != nullptr, &elem_ptr_instruction->base);
1748017503 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
1748117504 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
1748217505 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,
......@@ -17663,7 +17686,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
1766317686 return ira->codegen->invalid_instruction;
1766417687 if (type_is_invalid(struct_val->type))
1766517688 return ira->codegen->invalid_instruction;
17666 if (struct_val->special == ConstValSpecialUndef && initializing) {
17689 if (initializing && struct_val->special == ConstValSpecialUndef) {
1766717690 struct_val->data.x_struct.fields = create_const_vals(struct_type->data.structure.src_field_count);
1766817691 struct_val->special = ConstValSpecialStatic;
1766917692 for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {
......@@ -18261,7 +18284,7 @@ static IrInstruction *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstruc
1826118284 if (type_is_invalid(value->value.type))
1826218285 return ira->codegen->invalid_instruction;
1826318286
18264 return ir_analyze_store_ptr(ira, &instruction->base, ptr, value);
18287 return ir_analyze_store_ptr(ira, &instruction->base, ptr, value, instruction->allow_write_through_const);
1826518288}
1826618289
1826718290static IrInstruction *ir_analyze_instruction_load_ptr(IrAnalyze *ira, IrInstructionLoadPtr *instruction) {
......@@ -18764,7 +18787,7 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr
1876418787 if (optional_val == nullptr)
1876518788 return ira->codegen->invalid_instruction;
1876618789
18767 if (initializing && optional_val->special == ConstValSpecialUndef) {
18790 if (initializing) {
1876818791 switch (type_has_one_possible_value(ira->codegen, child_type)) {
1876918792 case OnePossibleValueInvalid:
1877018793 return ira->codegen->invalid_instruction;
......@@ -19669,7 +19692,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1966919692
1967019693 IrInstruction *field_ptr = ir_analyze_struct_field_ptr(ira, instruction, field, result_loc,
1967119694 container_type, true);
19672 ir_analyze_store_ptr(ira, instruction, field_ptr, runtime_inst);
19695 ir_analyze_store_ptr(ira, instruction, field_ptr, runtime_inst, false);
1967319696 if (instr_is_comptime(field_ptr) && field_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) {
1967419697 const_ptrs.append(field_ptr);
1967519698 } else {
......@@ -19686,7 +19709,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1968619709 IrInstruction *field_result_loc = const_ptrs.at(i);
1968719710 IrInstruction *deref = ir_get_deref(ira, field_result_loc, field_result_loc, nullptr);
1968819711 field_result_loc->value.special = ConstValSpecialRuntime;
19689 ir_analyze_store_ptr(ira, field_result_loc, field_result_loc, deref);
19712 ir_analyze_store_ptr(ira, field_result_loc, field_result_loc, deref, false);
1969019713 }
1969119714 }
1969219715 }
......@@ -19813,7 +19836,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1981319836 assert(elem_result_loc->value.special == ConstValSpecialStatic);
1981419837 IrInstruction *deref = ir_get_deref(ira, elem_result_loc, elem_result_loc, nullptr);
1981519838 elem_result_loc->value.special = ConstValSpecialRuntime;
19816 ir_analyze_store_ptr(ira, elem_result_loc, elem_result_loc, deref);
19839 ir_analyze_store_ptr(ira, elem_result_loc, elem_result_loc, deref, false);
1981719840 }
1981819841 }
1981919842 }
......@@ -21532,7 +21555,7 @@ static IrInstruction *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructi
2153221555 IrInstruction *result_loc;
2153321556 if (handle_is_ptr(result_type)) {
2153421557 result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21535 result_type, nullptr, true, false);
21558 result_type, nullptr, true, false, true);
2153621559 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
2153721560 return result_loc;
2153821561 }
......@@ -21789,7 +21812,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
2178921812 }
2179021813
2179121814 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21792 dest_slice_type, nullptr, true, false);
21815 dest_slice_type, nullptr, true, false, true);
2179321816 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {
2179421817 return result_loc;
2179521818 }
......@@ -21866,7 +21889,7 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
2186621889 }
2186721890
2186821891 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
21869 dest_slice_type, nullptr, true, false);
21892 dest_slice_type, nullptr, true, false, true);
2187021893 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
2187121894 return result_loc;
2187221895 }
......@@ -22608,7 +22631,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2260822631 }
2260922632
2261022633 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
22611 return_type, nullptr, true, false);
22634 return_type, nullptr, true, false, true);
2261222635 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
2261322636 return result_loc;
2261422637 }
......@@ -23260,7 +23283,7 @@ static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruct
2326023283 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
2326123284 if (err_union_val == nullptr)
2326223285 return ira->codegen->invalid_instruction;
23263 if (err_union_val->special == ConstValSpecialUndef && initializing) {
23286 if (initializing && err_union_val->special == ConstValSpecialUndef) {
2326423287 ConstExprValue *vals = create_const_vals(2);
2326523288 ConstExprValue *err_set_val = &vals[0];
2326623289 ConstExprValue *payload_val = &vals[1];
......@@ -25388,7 +25411,7 @@ static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstruct
2538825411
2538925412 bool was_written = instruction->result_loc->written;
2539025413 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
25391 value->value.type, value, false, false);
25414 value->value.type, value, false, false, true);
2539225415 if (result_loc != nullptr) {
2539325416 if (type_is_invalid(result_loc->value.type))
2539425417 return ira->codegen->invalid_instruction;
......@@ -25396,7 +25419,8 @@ static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstruct
2539625419 return result_loc;
2539725420
2539825421 if (!was_written) {
25399 IrInstruction *store_ptr = ir_analyze_store_ptr(ira, &instruction->base, result_loc, value);
25422 IrInstruction *store_ptr = ir_analyze_store_ptr(ira, &instruction->base, result_loc, value,
25423 instruction->result_loc->allow_write_through_const);
2540025424 if (type_is_invalid(store_ptr->value.type)) {
2540125425 return ira->codegen->invalid_instruction;
2540225426 }
......@@ -25420,7 +25444,7 @@ static IrInstruction *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInst
2542025444 return operand;
2542125445
2542225446 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base,
25423 &instruction->result_loc_bit_cast->base, operand->value.type, operand, false, false);
25447 &instruction->result_loc_bit_cast->base, operand->value.type, operand, false, false, true);
2542425448 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)))
2542525449 return result_loc;
2542625450
test/compile_errors.zig+8-8
......@@ -201,7 +201,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
201201 \\ return error.OutOfMemory;
202202 \\}
203203 ,
204 "tmp.zig:2:7: error: error is discarded",
204 "tmp.zig:2:12: error: error is discarded",
205205 );
206206
207207 cases.add(
......@@ -2740,7 +2740,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27402740 \\ 3 = 3;
27412741 \\}
27422742 ,
2743 "tmp.zig:2:7: error: cannot assign to constant",
2743 "tmp.zig:2:9: error: cannot assign to constant",
27442744 );
27452745
27462746 cases.add(
......@@ -2750,7 +2750,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
27502750 \\ a = 4;
27512751 \\}
27522752 ,
2753 "tmp.zig:3:7: error: cannot assign to constant",
2753 "tmp.zig:3:9: error: cannot assign to constant",
27542754 );
27552755
27562756 cases.add(
......@@ -2820,7 +2820,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
28202820 \\}
28212821 \\export fn entry() void { f(); }
28222822 ,
2823 "tmp.zig:3:7: error: cannot assign to constant",
2823 "tmp.zig:3:9: error: cannot assign to constant",
28242824 );
28252825
28262826 cases.add(
......@@ -3883,7 +3883,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
38833883 \\
38843884 \\export fn entry() usize { return @sizeOf(@typeOf(a)); }
38853885 ,
3886 "tmp.zig:6:24: error: unable to evaluate constant expression",
3886 "tmp.zig:6:26: error: unable to evaluate constant expression",
38873887 "tmp.zig:4:17: note: called from here",
38883888 );
38893889
......@@ -4133,7 +4133,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41334133 \\ cstr[0] = 'W';
41344134 \\}
41354135 ,
4136 "tmp.zig:3:11: error: cannot assign to constant",
4136 "tmp.zig:3:13: error: cannot assign to constant",
41374137 );
41384138
41394139 cases.add(
......@@ -4143,7 +4143,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
41434143 \\ cstr[0] = 'W';
41444144 \\}
41454145 ,
4146 "tmp.zig:3:11: error: cannot assign to constant",
4146 "tmp.zig:3:13: error: cannot assign to constant",
41474147 );
41484148
41494149 cases.add(
......@@ -4291,7 +4291,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
42914291 \\ f.field = 0;
42924292 \\}
42934293 ,
4294 "tmp.zig:6:13: error: cannot assign to constant",
4294 "tmp.zig:6:15: error: cannot assign to constant",
42954295 );
42964296
42974297 cases.add(
test/stage1/behavior/atomics.zig+10
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
34const builtin = @import("builtin");
45const AtomicRmwOp = builtin.AtomicRmwOp;
56const AtomicOrder = builtin.AtomicOrder;
......@@ -90,3 +91,12 @@ test "cmpxchg with ptr" {
9091// expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
9192// expect(x == 42);
9293//}
94
95test "cmpxchg with ignored result" {
96 var x: i32 = 1234;
97 var ptr = &x;
98
99 _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);
100
101 expectEqual(i32(5678), x);
102}
test/stage1/behavior/eval.zig+10
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
34const builtin = @import("builtin");
45
56test "compile time recursion" {
......@@ -794,3 +795,12 @@ test "no undeclared identifier error in unanalyzed branches" {
794795 lol_this_doesnt_exist = nonsense;
795796 }
796797}
798
799test "comptime assign int to optional int" {
800 comptime {
801 var x: ?i32 = null;
802 x = 2;
803 x.? *= 10;
804 expectEqual(20, x.?);
805 }
806}
test/stage1/behavior/fn.zig+19
......@@ -228,3 +228,22 @@ test "implicit cast fn call result to optional in field result" {
228228 S.entry();
229229 comptime S.entry();
230230}
231
232test "discard the result of a function that returns a struct" {
233 const S = struct {
234 fn entry() void {
235 _ = func();
236 }
237
238 fn func() Foo {
239 return undefined;
240 }
241
242 const Foo = struct {
243 a: u64,
244 b: u64,
245 };
246 };
247 S.entry();
248 comptime S.entry();
249}