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 {
54045404 <p>
54055405 For example, if we were to introduce another function to the above snippet:
54065406 </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#}
54085408fn max(comptime T: type, a: T, b: T) T {
54095409 return if (a > b) a else b;
54105410}
lib/std/event/loop.zig-23
......@@ -645,12 +645,6 @@ pub const Loop = struct {
645645 }
646646 }
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
654648 /// Yielding lets the event loop run, starting any unstarted async operations.
655649 /// Note that async operations automatically start when a function yields for any other reason,
656650 /// 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" {
942936 loop.run();
943937}
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
962939async fn testEventLoop() i32 {
963940 return 1234;
964941}
src/all_types.hpp+1
......@@ -3665,6 +3665,7 @@ struct IrInstructionArgType {
36653665
36663666 IrInstruction *fn_type;
36673667 IrInstruction *arg_index;
3668 bool allow_var;
36683669};
36693670
36703671struct IrInstructionExport {
src/codegen.cpp+5
......@@ -7820,6 +7820,11 @@ static void define_builtin_types(CodeGen *g) {
78207820 buf_init_from_str(&entry->name, "(null)");
78217821 g->builtin_types.entry_null = entry;
78227822 }
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 }
78237828 {
78247829 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);
78257830 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
206206 TypeStructField *field, IrInstruction *struct_ptr, ZigType *struct_type, bool initializing);
207207static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_name,
208208 IrInstruction *source_instr, IrInstruction *container_ptr, ZigType *container_type);
209static ResultLoc *no_result_loc(void);
209210
210211static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {
211212 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
31153116}
31163117
31173118static 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)
31193120{
31203121 IrInstructionArgType *instruction = ir_build_instruction<IrInstructionArgType>(irb, scope, source_node);
31213122 instruction->fn_type = fn_type;
31223123 instruction->arg_index = arg_index;
3124 instruction->allow_var = allow_var;
31233125
31243126 ir_ref_instruction(fn_type, irb->current_basic_block);
31253127 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
56475649 if (arg1_value == irb->codegen->invalid_instruction)
56485650 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);
56515653 return ir_lval_wrap(irb, scope, arg_type, lval, result_loc);
56525654 }
56535655 case BuiltinFnIdExport:
......@@ -5842,13 +5844,22 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node
58425844 if (fn_ref == irb->codegen->invalid_instruction)
58435845 return fn_ref;
58445846
5847 IrInstruction *fn_type = ir_build_typeof(irb, scope, node, fn_ref);
5848
58455849 size_t arg_count = node->data.fn_call_expr.params.length;
58465850 IrInstruction **args = allocate<IrInstruction*>(arg_count);
58475851 for (size_t i = 0; i < arg_count; i += 1) {
58485852 AstNode *arg_node = node->data.fn_call_expr.params.at(i);
5849 args[i] = ir_gen_node(irb, arg_node, scope);
5850 if (args[i] == irb->codegen->invalid_instruction)
5851 return args[i];
5853
5854 IrInstruction *arg_index = ir_build_const_usize(irb, scope, arg_node, 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);
58525863 }
58535864
58545865 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
1250412515 return result;
1250512516}
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
1250712539static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_instr,
1250812540 ZigType *wanted_type, IrInstruction *value, ResultLoc *result_loc)
1250912541{
......@@ -12515,6 +12547,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1251512547 return ira->codegen->invalid_instruction;
1251612548 }
1251712549
12550 // This means the wanted type is anything.
12551 if (wanted_type == ira->codegen->builtin_types.entry_var) {
12552 return value;
12553 }
12554
1251812555 // perfect match or non-const to const
1251912556 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type, actual_type,
1252012557 source_node, false);
......@@ -13071,6 +13108,25 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1307113108 return ir_analyze_int_to_c_ptr(ira, source_instr, value, wanted_type);
1307213109 }
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
1307413130 // cast from undefined to anything
1307513131 if (actual_type->id == ZigTypeIdUndefined) {
1307613132 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) {
1553715593 ptr->value.data.x_ptr.data.ref.pointee = undef_child;
1553815594}
1553915595
15540static bool ir_result_has_type(ResultLoc *result_loc) {
15596static Error ir_result_has_type(IrAnalyze *ira, ResultLoc *result_loc, bool *out) {
1554115597 switch (result_loc->id) {
1554215598 case ResultLocIdInvalid:
1554315599 case ResultLocIdPeerParent:
1554415600 zig_unreachable();
1554515601 case ResultLocIdNone:
1554615602 case ResultLocIdPeer:
15547 return false;
15603 *out = false;
15604 return ErrorNone;
1554815605 case ResultLocIdReturn:
1554915606 case ResultLocIdInstruction:
1555015607 case ResultLocIdBitCast:
15551 case ResultLocIdCast:
15552 return true;
15608 *out = 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 }
1555315618 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;
1555515621 }
1555615622 zig_unreachable();
1555715623}
......@@ -15698,7 +15764,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1569815764 }
1569915765 return nullptr;
1570015766 }
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) {
1570215771 if (peer_parent->parent->id == ResultLocIdReturn && value != nullptr) {
1570315772 reinterpret_cast<ResultLocReturn *>(peer_parent->parent)->implicit_return_type_done = true;
1570415773 ira->src_implicit_return_type_list.append(value);
......@@ -15741,6 +15810,11 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1574115810 if (type_is_invalid(dest_type))
1574215811 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
1574415818 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, dest_type, value_type,
1574515819 result_cast->base.source_instruction->source_node, false);
1574615820 if (const_cast_result.id == ConstCastResultIdInvalid)
......@@ -15948,6 +16022,9 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira,
1594816022 if (type_is_invalid(implicit_elem_type))
1594916023 return ira->codegen->invalid_instruction;
1595016024 } else {
16025 implicit_elem_type = ira->codegen->builtin_types.entry_var;
16026 }
16027 if (implicit_elem_type == ira->codegen->builtin_types.entry_var) {
1595116028 Buf *bare_name = buf_alloc();
1595216029 Buf *name = get_anon_type_name(ira->codegen, nullptr, container_string(ContainerKindStruct),
1595316030 instruction->base.scope, instruction->base.source_node, bare_name);
......@@ -17532,6 +17609,8 @@ static IrInstruction *ir_analyze_instruction_unreachable(IrAnalyze *ira,
1753217609}
1753317610
1753417611static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPhi *phi_instruction) {
17612 Error err;
17613
1753517614 if (ira->const_predecessor_bb) {
1753617615 for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {
1753717616 IrBasicBlock *predecessor = phi_instruction->incoming_blocks[i];
......@@ -17663,24 +17742,32 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1766317742 }
1766417743
1766517744 ZigType *resolved_type;
17666 if (peer_parent != nullptr && ir_result_has_type(peer_parent->parent)) {
17667 if (peer_parent->parent->id == ResultLocIdReturn) {
17668 resolved_type = ira->explicit_return_type;
17669 } else if (peer_parent->parent->id == ResultLocIdCast) {
17670 resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child);
17671 if (type_is_invalid(resolved_type))
17672 return ira->codegen->invalid_instruction;
17673 } else {
17674 ZigType *resolved_loc_ptr_type = peer_parent->parent->resolved_loc->value.type;
17675 ir_assert(resolved_loc_ptr_type->id == ZigTypeIdPointer, &phi_instruction->base);
17676 resolved_type = resolved_loc_ptr_type->data.pointer.child_type;
17745 if (peer_parent != nullptr) {
17746 bool peer_parent_has_type;
17747 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))
17748 return ira->codegen->invalid_instruction;
17749 if (peer_parent_has_type) {
17750 if (peer_parent->parent->id == ResultLocIdReturn) {
17751 resolved_type = ira->explicit_return_type;
17752 } else if (peer_parent->parent->id == ResultLocIdCast) {
17753 resolved_type = ir_resolve_type(ira, peer_parent->parent->source_instruction->child);
17754 if (type_is_invalid(resolved_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;
1767717762 }
17678 } else {
17763 }
17764 {
1767917765 resolved_type = ir_resolve_peer_types(ira, phi_instruction->base.source_node, nullptr,
1768017766 new_incoming_values.items, new_incoming_values.length);
1768117767 if (type_is_invalid(resolved_type))
1768217768 return ira->codegen->invalid_instruction;
1768317769 }
17770skip_resolve_peer_types:
1768417771
1768517772 switch (type_has_one_possible_value(ira->codegen, resolved_type)) {
1768617773 case OnePossibleValueInvalid:
......@@ -18379,7 +18466,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
1837918466 inferred_struct_field->inferred_struct_type = container_type;
1838018467 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;
1838318470 ZigType *field_ptr_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
1838418471 container_ptr_type->data.pointer.is_const, container_ptr_type->data.pointer.is_volatile,
1838518472 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
2554725634 if (!ir_resolve_usize(ira, arg_index_inst, &arg_index))
2554825635 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 }
2555025641 if (fn_type->id != ZigTypeIdFn) {
2555125642 ir_add_error(ira, fn_type_inst, buf_sprintf("expected function, found '%s'", buf_ptr(&fn_type->name)));
2555225643 return ira->codegen->invalid_instruction;
......@@ -25554,6 +25645,10 @@ static IrInstruction *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruct
2555425645
2555525646 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
2555625647 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 }
2555725652 ir_add_error(ira, arg_index_inst,
2555825653 buf_sprintf("arg index %" ZIG_PRI_u64 " out of bounds; '%s' has %" ZIG_PRI_usize " arguments",
2555925654 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
2556525660 // Args are only unresolved if our function is generic.
2556625661 ir_assert(fn_type->data.fn.is_generic, &instruction->base);
2556725662
25568 ir_add_error(ira, arg_index_inst,
25569 buf_sprintf("@ArgType could not resolve the type of arg %" ZIG_PRI_u64 " because '%s' is generic",
25570 arg_index, buf_ptr(&fn_type->name)));
25571 return ira->codegen->invalid_instruction;
25663 if (instruction->allow_var) {
25664 return ir_const_type(ira, &instruction->base, ira->codegen->builtin_types.entry_var);
25665 } else {
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 }
2557225671 }
2557325672 return ir_const_type(ira, &instruction->base, result_type);
2557425673}
test/compile_errors.zig+4-4
......@@ -2113,8 +2113,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
21132113 \\
21142114 \\fn bar(x: *b.Foo) void {}
21152115 ,
2116 "tmp.zig:6:10: 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'",
2116 "tmp.zig:6:9: error: expected type '*b.Foo', found '*a.Foo'",
2117 "tmp.zig:6:9: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",
21182118 "a.zig:1:17: note: a.Foo declared here",
21192119 "b.zig:1:17: note: b.Foo declared here",
21202120 );
......@@ -4978,7 +4978,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
49784978 \\
49794979 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
49804980 ,
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'",
49824982 );
49834983
49844984 cases.add(
......@@ -5675,7 +5675,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
56755675 \\ x.* += 1;
56765676 \\}
56775677 ,
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'",
56795679 );
56805680
56815681 cases.add(
test/stage1/behavior/fn.zig+16
......@@ -247,3 +247,19 @@ test "discard the result of a function that returns a struct" {
247247 S.entry();
248248 comptime S.entry();
249249}
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}