authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-12 17:59:24-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-12 18:55:17-05:00
log37318bf15138d0a7b158b32ca43cbcdcf5382942
tree6eaa8e27a874daaaa452107c069c2ac137eff758
parent956ba8b0e7ada08f2f85cd41ee73af6453db0c16
signaturelock-open Commit is signed but in an unrecognized format.

fn parameters participate in result location semantics

See #3665

7 files changed, 154 insertions(+), 56 deletions(-)

doc/langref.html.in+1-1
...@@ -5404,7 +5404,7 @@ fn gimmeTheBiggerInteger(a: u64, b: u64) u64 {...@@ -5404,7 +5404,7 @@ fn gimmeTheBiggerInteger(a: u64, b: u64) u64 {
5404 <p>5404 <p>
5405 For example, if we were to introduce another function to the above snippet:5405 For example, if we were to introduce another function to the above snippet:
5406 </p>5406 </p>
5407 {#code_begin|test_err|cannot store runtime value in type 'type'#}5407 {#code_begin|test_err|values of type 'type' must be comptime known#}
5408fn max(comptime T: type, a: T, b: T) T {5408fn max(comptime T: type, a: T, b: T) T {
5409 return if (a > b) a else b;5409 return if (a > b) a else b;
5410}5410}
lib/std/event/loop.zig-23
...@@ -645,12 +645,6 @@ pub const Loop = struct {...@@ -645,12 +645,6 @@ pub const Loop = struct {
645 }645 }
646 }646 }
647647
648 /// This is equivalent to function call, except it calls `startCpuBoundOperation` first.
649 pub fn call(comptime func: var, args: ...) @typeOf(func).ReturnType {
650 startCpuBoundOperation();
651 return func(args);
652 }
653
654 /// Yielding lets the event loop run, starting any unstarted async operations.648 /// Yielding lets the event loop run, starting any unstarted async operations.
655 /// Note that async operations automatically start when a function yields for any other reason,649 /// Note that async operations automatically start when a function yields for any other reason,
656 /// for example, when async I/O is performed. This function is intended to be used only when650 /// for example, when async I/O is performed. This function is intended to be used only when
...@@ -942,23 +936,6 @@ test "std.event.Loop - basic" {...@@ -942,23 +936,6 @@ test "std.event.Loop - basic" {
942 loop.run();936 loop.run();
943}937}
944938
945test "std.event.Loop - call" {
946 // https://github.com/ziglang/zig/issues/1908
947 if (builtin.single_threaded) return error.SkipZigTest;
948
949 var loop: Loop = undefined;
950 try loop.initMultiThreaded();
951 defer loop.deinit();
952
953 var did_it = false;
954 var handle = async Loop.call(testEventLoop);
955 var handle2 = async Loop.call(testEventLoop2, &handle, &did_it);
956
957 loop.run();
958
959 testing.expect(did_it);
960}
961
962async fn testEventLoop() i32 {939async fn testEventLoop() i32 {
963 return 1234;940 return 1234;
964}941}
src/all_types.hpp+1
...@@ -3665,6 +3665,7 @@ struct IrInstructionArgType {...@@ -3665,6 +3665,7 @@ struct IrInstructionArgType {
36653665
3666 IrInstruction *fn_type;3666 IrInstruction *fn_type;
3667 IrInstruction *arg_index;3667 IrInstruction *arg_index;
3668 bool allow_var;
3668};3669};
36693670
3670struct IrInstructionExport {3671struct IrInstructionExport {
src/codegen.cpp+5
...@@ -7820,6 +7820,11 @@ static void define_builtin_types(CodeGen *g) {...@@ -7820,6 +7820,11 @@ static void define_builtin_types(CodeGen *g) {
7820 buf_init_from_str(&entry->name, "(null)");7820 buf_init_from_str(&entry->name, "(null)");
7821 g->builtin_types.entry_null = entry;7821 g->builtin_types.entry_null = entry;
7822 }7822 }
7823 {
7824 ZigType *entry = new_type_table_entry(ZigTypeIdOpaque);
7825 buf_init_from_str(&entry->name, "(var)");
7826 g->builtin_types.entry_var = entry;
7827 }
7823 {7828 {
7824 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);7829 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);
7825 buf_init_from_str(&entry->name, "(args)");7830 buf_init_from_str(&entry->name, "(args)");
src/ir.cpp+127-28
...@@ -206,6 +206,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction...@@ -206,6 +206,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
206 TypeStructField *field, IrInstruction *struct_ptr, ZigType *struct_type, bool initializing);206 TypeStructField *field, IrInstruction *struct_ptr, ZigType *struct_type, bool initializing);
207static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,207static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
208 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);208 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
209static ResultLoc *no_result_loc(void);
209210
210static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {211static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {
211 assert(get_src_ptr_type(const_val->type) != nullptr);212 assert(get_src_ptr_type(const_val->type) != nullptr);
...@@ -3115,11 +3116,12 @@ static IrInstruction *ir_build_set_align_stack(IrBuilder *irb, Scope *scope, Ast...@@ -3115,11 +3116,12 @@ static IrInstruction *ir_build_set_align_stack(IrBuilder *irb, Scope *scope, Ast
3115}3116}
31163117
3117static IrInstruction *ir_build_arg_type(IrBuilder *irb, Scope *scope, AstNode *source_node,3118static IrInstruction *ir_build_arg_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
3118 IrInstruction *fn_type, IrInstruction *arg_index)3119 IrInstruction *fn_type, IrInstruction *arg_index, bool allow_var)
3119{3120{
3120 IrInstructionArgType *instruction = ir_build_instruction<IrInstructionArgType>(irb, scope, source_node);3121 IrInstructionArgType *instruction = ir_build_instruction<IrInstructionArgType>(irb, scope, source_node);
3121 instruction->fn_type = fn_type;3122 instruction->fn_type = fn_type;
3122 instruction->arg_index = arg_index;3123 instruction->arg_index = arg_index;
3124 instruction->allow_var = allow_var;
31233125
3124 ir_ref_instruction(fn_type, irb->current_basic_block);3126 ir_ref_instruction(fn_type, irb->current_basic_block);
3125 ir_ref_instruction(arg_index, irb->current_basic_block);3127 ir_ref_instruction(arg_index, irb->current_basic_block);
...@@ -5647,7 +5649,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -5647,7 +5649,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
5647 if (arg1_value == irb->codegen->invalid_instruction)5649 if (arg1_value == irb->codegen->invalid_instruction)
5648 return arg1_value;5650 return arg1_value;
56495651
5650 IrInstruction *arg_type = ir_build_arg_type(irb, scope, node, arg0_value, arg1_value);5652 IrInstruction *arg_type = ir_build_arg_type(irb, scope, node, arg0_value, arg1_value, false);
5651 return ir_lval_wrap(irb, scope, arg_type, lval, result_loc);5653 return ir_lval_wrap(irb, scope, arg_type, lval, result_loc);
5652 }5654 }
5653 case BuiltinFnIdExport:5655 case BuiltinFnIdExport:
...@@ -5842,13 +5844,22 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node...@@ -5842,13 +5844,22 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
5842 if (fn_ref == irb->codegen->invalid_instruction)5844 if (fn_ref == irb->codegen->invalid_instruction)
5843 return fn_ref;5845 return fn_ref;
58445846
5847 IrInstruction *fn_type = ir_build_typeof(irb, scope, node, fn_ref);
5848
5845 size_t arg_count = node->data.fn_call_expr.params.length;5849 size_t arg_count = node->data.fn_call_expr.params.length;
5846 IrInstruction **args = allocate<IrInstruction*>(arg_count);5850 IrInstruction **args = allocate<IrInstruction*>(arg_count);
5847 for (size_t i = 0; i < arg_count; i += 1) {5851 for (size_t i = 0; i < arg_count; i += 1) {
5848 AstNode *arg_node = node->data.fn_call_expr.params.at(i);5852 AstNode *arg_node = node->data.fn_call_expr.params.at(i);
5849 args[i] = ir_gen_node(irb, arg_node, scope);5853
5850 if (args[i] == irb->codegen->invalid_instruction)5854 IrInstruction *arg_index = ir_build_const_usize(irb, scope, arg_node, i);
5851 return args[i];5855 IrInstruction *arg_type = ir_build_arg_type(irb, scope, node, fn_type, arg_index, true);
5856 ResultLocCast *result_loc_cast = ir_build_cast_result_loc(irb, arg_type, no_result_loc());
5857
5858 IrInstruction *arg = ir_gen_node_extra(irb, arg_node, scope, LValNone, &result_loc_cast->base);
5859 if (arg == irb->codegen->invalid_instruction)
5860 return arg;
5861
5862 args[i] = ir_build_implicit_cast(irb, scope, arg_node, arg, result_loc_cast);
5852 }5863 }
58535864
5854 IrInstruction *fn_call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,5865 IrInstruction *fn_call = ir_build_call_src(irb, scope, node, nullptr, fn_ref, arg_count, args, false,
...@@ -12504,6 +12515,27 @@ static IrInstruction *ir_analyze_enum_literal(IrAnalyze *ira, IrInstruction *sou...@@ -12504,6 +12515,27 @@ static IrInstruction *ir_analyze_enum_literal(IrAnalyze *ira, IrInstruction *sou
12504 return result;12515 return result;
12505}12516}
1250612517
12518static IrInstruction *ir_analyze_struct_literal_to_array(IrAnalyze *ira, IrInstruction *source_instr,
12519 IrInstruction *value, ZigType *wanted_type)
12520{
12521 ir_add_error(ira, source_instr, buf_sprintf("TODO: type coercion of anon list literal to array"));
12522 return ira->codegen->invalid_instruction;
12523}
12524
12525static IrInstruction *ir_analyze_struct_literal_to_struct(IrAnalyze *ira, IrInstruction *source_instr,
12526 IrInstruction *value, ZigType *wanted_type)
12527{
12528 ir_add_error(ira, source_instr, buf_sprintf("TODO: type coercion of anon struct literal to struct"));
12529 return ira->codegen->invalid_instruction;
12530}
12531
12532static IrInstruction *ir_analyze_struct_literal_to_union(IrAnalyze *ira, IrInstruction *source_instr,
12533 IrInstruction *value, ZigType *wanted_type)
12534{
12535 ir_add_error(ira, source_instr, buf_sprintf("TODO: type coercion of anon struct literal to union"));
12536 return ira->codegen->invalid_instruction;
12537}
12538
12507static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,12539static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,
12508 ZigType *wanted_type, IrInstruction *value, ResultLoc *result_loc)12540 ZigType *wanted_type, IrInstruction *value, ResultLoc *result_loc)
12509{12541{
...@@ -12515,6 +12547,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -12515,6 +12547,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
12515 return ira->codegen->invalid_instruction;12547 return ira->codegen->invalid_instruction;
12516 }12548 }
1251712549
12550 // This means the wanted type is anything.
12551 if (wanted_type == ira->codegen->builtin_types.entry_var) {
12552 return value;
12553 }
12554
12518 // perfect match or non-const to const12555 // perfect match or non-const to const
12519 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type, actual_type,12556 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type, actual_type,
12520 source_node, false);12557 source_node, false);
...@@ -13071,6 +13108,25 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -13071,6 +13108,25 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
13071 return ir_analyze_int_to_c_ptr(ira, source_instr, value, wanted_type);13108 return ir_analyze_int_to_c_ptr(ira, source_instr, value, wanted_type);
13072 }13109 }
1307313110
13111 // cast from inferred struct type to array, union, or struct
13112 if (actual_type->id == ZigTypeIdStruct && actual_type->data.structure.is_inferred) {
13113 AstNode *decl_node = actual_type->data.structure.decl_node;
13114 ir_assert(decl_node->type == NodeTypeContainerInitExpr, source_instr);
13115 ContainerInitKind init_kind = decl_node->data.container_init_expr.kind;
13116 uint32_t field_count = actual_type->data.structure.src_field_count;
13117 if (wanted_type->id == ZigTypeIdArray && (init_kind == ContainerInitKindArray || field_count == 0) &&
13118 wanted_type->data.array.len == field_count)
13119 {
13120 return ir_analyze_struct_literal_to_array(ira, source_instr, value, wanted_type);
13121 } else if (wanted_type->id == ZigTypeIdStruct &&
13122 (init_kind == ContainerInitKindStruct || field_count == 0))
13123 {
13124 return ir_analyze_struct_literal_to_struct(ira, source_instr, value, wanted_type);
13125 } else if (wanted_type->id == ZigTypeIdUnion && init_kind == ContainerInitKindStruct && field_count == 1) {
13126 return ir_analyze_struct_literal_to_union(ira, source_instr, value, wanted_type);
13127 }
13128 }
13129
13074 // cast from undefined to anything13130 // cast from undefined to anything
13075 if (actual_type->id == ZigTypeIdUndefined) {13131 if (actual_type->id == ZigTypeIdUndefined) {
13076 return ir_analyze_undefined_to_anything(ira, source_instr, value, wanted_type);13132 return ir_analyze_undefined_to_anything(ira, source_instr, value, wanted_type);
...@@ -15537,21 +15593,31 @@ static void set_up_result_loc_for_inferred_comptime(IrInstruction *ptr) {...@@ -15537,21 +15593,31 @@ static void set_up_result_loc_for_inferred_comptime(IrInstruction *ptr) {
15537 ptr->value.data.x_ptr.data.ref.pointee = undef_child;15593 ptr->value.data.x_ptr.data.ref.pointee = undef_child;
15538}15594}
1553915595
15540static bool ir_result_has_type(ResultLoc *result_loc) {15596static Error ir_result_has_type(IrAnalyze *ira, ResultLoc *result_loc, bool *out) {
15541 switch (result_loc->id) {15597 switch (result_loc->id) {
15542 case ResultLocIdInvalid:15598 case ResultLocIdInvalid:
15543 case ResultLocIdPeerParent:15599 case ResultLocIdPeerParent:
15544 zig_unreachable();15600 zig_unreachable();
15545 case ResultLocIdNone:15601 case ResultLocIdNone:
15546 case ResultLocIdPeer:15602 case ResultLocIdPeer:
15547 return false;15603 *out = false;
15604 return ErrorNone;
15548 case ResultLocIdReturn:15605 case ResultLocIdReturn:
15549 case ResultLocIdInstruction:15606 case ResultLocIdInstruction:
15550 case ResultLocIdBitCast:15607 case ResultLocIdBitCast:
15551 case ResultLocIdCast:15608 *out = true;
15552 return true;15609 return ErrorNone;
15610 case ResultLocIdCast: {
15611 ResultLocCast *result_cast = reinterpret_cast<ResultLocCast *>(result_loc);
15612 ZigType *dest_type = ir_resolve_type(ira, result_cast->base.source_instruction->child);
15613 if (type_is_invalid(dest_type))
15614 return ErrorSemanticAnalyzeFail;
15615 *out = (dest_type != ira->codegen->builtin_types.entry_var);
15616 return ErrorNone;
15617 }
15553 case ResultLocIdVar:15618 case ResultLocIdVar:
15554 return reinterpret_cast<ResultLocVar *>(result_loc)->var->decl_node->data.variable_declaration.type != nullptr;15619 *out = reinterpret_cast<ResultLocVar *>(result_loc)->var->decl_node->data.variable_declaration.type != nullptr;
15620 return ErrorNone;
15555 }15621 }
15556 zig_unreachable();15622 zig_unreachable();
15557}15623}
...@@ -15698,7 +15764,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15698,7 +15764,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15698 }15764 }
15699 return nullptr;15765 return nullptr;
15700 }15766 }
15701 if (ir_result_has_type(peer_parent->parent)) {15767 bool peer_parent_has_type;
15768 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))
15769 return ira->codegen->invalid_instruction;
15770 if (peer_parent_has_type) {
15702 if (peer_parent->parent->id == ResultLocIdReturn && value != nullptr) {15771 if (peer_parent->parent->id == ResultLocIdReturn && value != nullptr) {
15703 reinterpret_cast<ResultLocReturn *>(peer_parent->parent)->implicit_return_type_done = true;15772 reinterpret_cast<ResultLocReturn *>(peer_parent->parent)->implicit_return_type_done = true;
15704 ira->src_implicit_return_type_list.append(value);15773 ira->src_implicit_return_type_list.append(value);
...@@ -15741,6 +15810,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15741,6 +15810,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15741 if (type_is_invalid(dest_type))15810 if (type_is_invalid(dest_type))
15742 return ira->codegen->invalid_instruction;15811 return ira->codegen->invalid_instruction;
1574315812
15813 if (dest_type == ira->codegen->builtin_types.entry_var) {
15814 return ir_resolve_no_result_loc(ira, suspend_source_instr, result_loc, value_type,
15815 force_runtime, non_null_comptime);
15816 }
15817
15744 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, dest_type, value_type,15818 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, dest_type, value_type,
15745 result_cast->base.source_instruction->source_node, false);15819 result_cast->base.source_instruction->source_node, false);
15746 if (const_cast_result.id == ConstCastResultIdInvalid)15820 if (const_cast_result.id == ConstCastResultIdInvalid)
...@@ -15948,6 +16022,9 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira,...@@ -15948,6 +16022,9 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira,
15948 if (type_is_invalid(implicit_elem_type))16022 if (type_is_invalid(implicit_elem_type))
15949 return ira->codegen->invalid_instruction;16023 return ira->codegen->invalid_instruction;
15950 } else {16024 } else {
16025 implicit_elem_type = ira->codegen->builtin_types.entry_var;
16026 }
16027 if (implicit_elem_type == ira->codegen->builtin_types.entry_var) {
15951 Buf *bare_name = buf_alloc();16028 Buf *bare_name = buf_alloc();
15952 Buf *name = get_anon_type_name(ira->codegen, nullptr, container_string(ContainerKindStruct),16029 Buf *name = get_anon_type_name(ira->codegen, nullptr, container_string(ContainerKindStruct),
15953 instruction->base.scope, instruction->base.source_node, bare_name);16030 instruction->base.scope, instruction->base.source_node, bare_name);
...@@ -17532,6 +17609,8 @@ static IrInstruction *ir_analyze_instruction_unreachable(IrAnalyze *ira,...@@ -17532,6 +17609,8 @@ static IrInstruction *ir_analyze_instruction_unreachable(IrAnalyze *ira,
17532}17609}
1753317610
17534static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPhi *phi_instruction) {17611static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPhi *phi_instruction) {
17612 Error err;
17613
17535 if (ira->const_predecessor_bb) {17614 if (ira->const_predecessor_bb) {
17536 for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {17615 for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {
17537 IrBasicBlock *predecessor = phi_instruction->incoming_blocks[i];17616 IrBasicBlock *predecessor = phi_instruction->incoming_blocks[i];
...@@ -17663,24 +17742,32 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh...@@ -17663,24 +17742,32 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
17663 }17742 }
1766417743
17665 ZigType *resolved_type;17744 ZigType *resolved_type;
17666 if (peer_parent != nullptr && ir_result_has_type(peer_parent->parent)) {17745 if (peer_parent != nullptr) {
17667 if (peer_parent->parent->id == ResultLocIdReturn) {17746 bool peer_parent_has_type;
17668 resolved_type = ira->explicit_return_type;17747 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))
17669 } else if (peer_parent->parent->id == ResultLocIdCast) {17748 return ira->codegen->invalid_instruction;
17670 resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child);17749 if (peer_parent_has_type) {
17671 if (type_is_invalid(resolved_type))17750 if (peer_parent->parent->id == ResultLocIdReturn) {
17672 return ira->codegen->invalid_instruction;17751 resolved_type = ira->explicit_return_type;
17673 } else {17752 } else if (peer_parent->parent->id == ResultLocIdCast) {
17674 ZigType *resolved_loc_ptr_type = peer_parent->parent->resolved_loc->value.type;17753 resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child);
17675 ir_assert(resolved_loc_ptr_type->id == ZigTypeIdPointer, &phi_instruction->base);17754 if (type_is_invalid(resolved_type))
17676 resolved_type = resolved_loc_ptr_type->data.pointer.child_type;17755 return ira->codegen->invalid_instruction;
17756 } else {
17757 ZigType *resolved_loc_ptr_type = peer_parent->parent->resolved_loc->value.type;
17758 ir_assert(resolved_loc_ptr_type->id == ZigTypeIdPointer, &phi_instruction->base);
17759 resolved_type = resolved_loc_ptr_type->data.pointer.child_type;
17760 }
17761 goto skip_resolve_peer_types;
17677 }17762 }
17678 } else {17763 }
17764 {
17679 resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node, nullptr,17765 resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node, nullptr,
17680 new_incoming_values.items, new_incoming_values.length);17766 new_incoming_values.items, new_incoming_values.length);
17681 if (type_is_invalid(resolved_type))17767 if (type_is_invalid(resolved_type))
17682 return ira->codegen->invalid_instruction;17768 return ira->codegen->invalid_instruction;
17683 }17769 }
17770skip_resolve_peer_types:
1768417771
17685 switch (type_has_one_possible_value(ira->codegen, resolved_type)) {17772 switch (type_has_one_possible_value(ira->codegen, resolved_type)) {
17686 case OnePossibleValueInvalid:17773 case OnePossibleValueInvalid:
...@@ -18379,7 +18466,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n...@@ -18379,7 +18466,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
18379 inferred_struct_field->inferred_struct_type = container_type;18466 inferred_struct_field->inferred_struct_type = container_type;
18380 inferred_struct_field->field_name = field_name;18467 inferred_struct_field->field_name = field_name;
1838118468
18382 ZigType *elem_type = ira->codegen->builtin_types.entry_c_void;18469 ZigType *elem_type = ira->codegen->builtin_types.entry_var;
18383 ZigType *field_ptr_type = get_pointer_to_type_extra2(ira->codegen, elem_type,18470 ZigType *field_ptr_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
18384 container_ptr_type->data.pointer.is_const, container_ptr_type->data.pointer.is_volatile,18471 container_ptr_type->data.pointer.is_const, container_ptr_type->data.pointer.is_volatile,
18385 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, inferred_struct_field);18472 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, inferred_struct_field);
...@@ -25547,6 +25634,10 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct...@@ -25547,6 +25634,10 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct
25547 if (!ir_resolve_usize(ira, arg_index_inst, &arg_index))25634 if (!ir_resolve_usize(ira, arg_index_inst, &arg_index))
25548 return ira->codegen->invalid_instruction;25635 return ira->codegen->invalid_instruction;
2554925636
25637 if (fn_type->id == ZigTypeIdBoundFn) {
25638 fn_type = fn_type->data.bound_fn.fn_type;
25639 arg_index += 1;
25640 }
25550 if (fn_type->id != ZigTypeIdFn) {25641 if (fn_type->id != ZigTypeIdFn) {
25551 ir_add_error(ira, fn_type_inst, buf_sprintf("expected function, found '%s'", buf_ptr(&fn_type->name)));25642 ir_add_error(ira, fn_type_inst, buf_sprintf("expected function, found '%s'", buf_ptr(&fn_type->name)));
25552 return ira->codegen->invalid_instruction;25643 return ira->codegen->invalid_instruction;
...@@ -25554,6 +25645,10 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct...@@ -25554,6 +25645,10 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct
2555425645
25555 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;25646 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
25556 if (arg_index >= fn_type_id->param_count) {25647 if (arg_index >= fn_type_id->param_count) {
25648 if (instruction->allow_var) {
25649 // TODO remove this with var args
25650 return ir_const_type(ira, &instruction->base, ira->codegen->builtin_types.entry_var);
25651 }
25557 ir_add_error(ira, arg_index_inst,25652 ir_add_error(ira, arg_index_inst,
25558 buf_sprintf("arg index %" ZIG_PRI_u64 " out of bounds; '%s' has %" ZIG_PRI_usize " arguments",25653 buf_sprintf("arg index %" ZIG_PRI_u64 " out of bounds; '%s' has %" ZIG_PRI_usize " arguments",
25559 arg_index, buf_ptr(&fn_type->name), fn_type_id->param_count));25654 arg_index, buf_ptr(&fn_type->name), fn_type_id->param_count));
...@@ -25565,10 +25660,14 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct...@@ -25565,10 +25660,14 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct
25565 // Args are only unresolved if our function is generic.25660 // Args are only unresolved if our function is generic.
25566 ir_assert(fn_type->data.fn.is_generic, &instruction->base);25661 ir_assert(fn_type->data.fn.is_generic, &instruction->base);
2556725662
25568 ir_add_error(ira, arg_index_inst,25663 if (instruction->allow_var) {
25569 buf_sprintf("@ArgType could not resolve the type of arg %" ZIG_PRI_u64 " because '%s' is generic",25664 return ir_const_type(ira, &instruction->base, ira->codegen->builtin_types.entry_var);
25570 arg_index, buf_ptr(&fn_type->name)));25665 } else {
25571 return ira->codegen->invalid_instruction;25666 ir_add_error(ira, arg_index_inst,
25667 buf_sprintf("@ArgType could not resolve the type of arg %" ZIG_PRI_u64 " because '%s' is generic",
25668 arg_index, buf_ptr(&fn_type->name)));
25669 return ira->codegen->invalid_instruction;
25670 }
25572 }25671 }
25573 return ir_const_type(ira, &instruction->base, result_type);25672 return ir_const_type(ira, &instruction->base, result_type);
25574}25673}
test/compile_errors.zig+4-4
...@@ -2113,8 +2113,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -2113,8 +2113,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2113 \\2113 \\
2114 \\fn bar(x: *b.Foo) void {}2114 \\fn bar(x: *b.Foo) void {}
2115 ,2115 ,
2116 "tmp.zig:6:10: error: expected type '*b.Foo', found '*a.Foo'",2116 "tmp.zig:6:9: error: expected type '*b.Foo', found '*a.Foo'",
2117 "tmp.zig:6:10: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",2117 "tmp.zig:6:9: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",
2118 "a.zig:1:17: note: a.Foo declared here",2118 "a.zig:1:17: note: a.Foo declared here",
2119 "b.zig:1:17: note: b.Foo declared here",2119 "b.zig:1:17: note: b.Foo declared here",
2120 );2120 );
...@@ -4978,7 +4978,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -4978,7 +4978,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4978 \\4978 \\
4979 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }4979 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
4980 ,4980 ,
4981 "tmp.zig:8:26: error: expected type '*const u3', found '*align(:3:1) const u3'",4981 "tmp.zig:8:16: error: expected type '*const u3', found '*align(:3:1) const u3'",
4982 );4982 );
49834983
4984 cases.add(4984 cases.add(
...@@ -5675,7 +5675,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5675,7 +5675,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5675 \\ x.* += 1;5675 \\ x.* += 1;
5676 \\}5676 \\}
5677 ,5677 ,
5678 "tmp.zig:8:13: error: expected type '*u32', found '*align(1) u32'",5678 "tmp.zig:8:9: error: expected type '*u32', found '*align(1) u32'",
5679 );5679 );
56805680
5681 cases.add(5681 cases.add(
test/stage1/behavior/fn.zig+16
...@@ -247,3 +247,19 @@ test "discard the result of a function that returns a struct" {...@@ -247,3 +247,19 @@ test "discard the result of a function that returns a struct" {
247 S.entry();247 S.entry();
248 comptime S.entry();248 comptime S.entry();
249}249}
250
251test "function call with anon list literal" {
252 const S = struct {
253 fn doTheTest() void {
254 consumeVec(.{9, 8, 7});
255 }
256
257 fn consumeVec(vec: [3]f32) void {
258 expect(vec[0] == 9);
259 expect(vec[1] == 8);
260 expect(vec[2] == 7);
261 }
262 };
263 S.doTheTest();
264 comptime S.doTheTest();
265}