authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-11 15:39:21+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-11 17:55:19+03:00
log20d4f7213dde1ffabe0880bbee46a1de44d586fc
treee60caf13833e99bbd092b67743baceeac4bf4e6e
parentc9e1360cdba2bc0c20dc04a3d22fbc0002bcd70b

Sema: add notes about function return type


16 files changed, 100 insertions(+), 32 deletions(-)

src/Sema.zig+69-18
......@@ -4135,23 +4135,24 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v
41354135 const ptr = try sema.resolveInst(extra.lhs);
41364136 const operand = try sema.resolveInst(extra.rhs);
41374137
4138 const is_ret = if (Zir.refToIndex(extra.lhs)) |ptr_index|
4139 zir_tags[ptr_index] == .ret_ptr
4140 else
4141 false;
4142
41384143 // Check for the possibility of this pattern:
41394144 // %a = ret_ptr
41404145 // %b = store(%a, %c)
41414146 // Where %c is an error union or error set. In such case we need to add
41424147 // to the current function's inferred error set, if any.
4143 if ((sema.typeOf(operand).zigTypeTag() == .ErrorUnion or
4148 if (is_ret and (sema.typeOf(operand).zigTypeTag() == .ErrorUnion or
41444149 sema.typeOf(operand).zigTypeTag() == .ErrorSet) and
41454150 sema.fn_ret_ty.zigTypeTag() == .ErrorUnion)
41464151 {
4147 if (Zir.refToIndex(extra.lhs)) |ptr_index| {
4148 if (zir_tags[ptr_index] == .ret_ptr) {
4149 try sema.addToInferredErrorSet(operand);
4150 }
4151 }
4152 try sema.addToInferredErrorSet(operand);
41524153 }
41534154
4154 return sema.storePtr(block, src, ptr, operand);
4155 return sema.storePtr2(block, src, ptr, src, operand, src, if (is_ret) .ret_ptr else .store);
41554156}
41564157
41574158fn zirParamType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -5543,7 +5544,7 @@ fn analyzeCall(
55435544 try sema.resolveBody(&child_block, fn_info.ret_ty_body, module_fn.zir_body_inst)
55445545 else
55455546 try sema.resolveInst(fn_info.ret_ty_ref);
5546 const ret_ty_src = func_src; // TODO better source location
5547 const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = 0 };
55475548 const bare_return_type = try sema.analyzeAsType(&child_block, ret_ty_src, ret_ty_inst);
55485549 // Create a fresh inferred error set type for inline/comptime calls.
55495550 const fn_ret_ty = blk: {
......@@ -6885,7 +6886,7 @@ fn zirFunc(
68856886 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
68866887 const extra = sema.code.extraData(Zir.Inst.Func, inst_data.payload_index);
68876888 const target = sema.mod.getTarget();
6888 const ret_ty_src = inst_data.src(); // TODO better source location
6889 const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = inst_data.src_node };
68896890
68906891 var extra_index = extra.end;
68916892
......@@ -7467,13 +7468,20 @@ fn analyzeAs(
74677468 zir_dest_type: Zir.Inst.Ref,
74687469 zir_operand: Zir.Inst.Ref,
74697470) CompileError!Air.Inst.Ref {
7471 const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|
7472 sema.code.instructions.items(.tag)[ptr_index] == .ret_type
7473 else
7474 false;
74707475 const dest_ty = try sema.resolveType(block, src, zir_dest_type);
74717476 const operand = try sema.resolveInst(zir_operand);
74727477 if (dest_ty.tag() == .var_args_param) return operand;
74737478 if (dest_ty.zigTypeTag() == .NoReturn) {
74747479 return sema.fail(block, src, "cannot cast to noreturn", .{});
74757480 }
7476 return sema.coerce(block, dest_ty, operand, src);
7481 return sema.coerceExtra(block, dest_ty, operand, src, true, is_ret) catch |err| switch (err) {
7482 error.NotCoercible => unreachable,
7483 else => |e| return e,
7484 };
74777485}
74787486
74797487fn zirPtrToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -13656,7 +13664,10 @@ fn analyzeRet(
1365613664 if (sema.fn_ret_ty.zigTypeTag() == .ErrorUnion) {
1365713665 try sema.addToInferredErrorSet(uncasted_operand);
1365813666 }
13659 const operand = try sema.coerce(block, sema.fn_ret_ty, uncasted_operand, src);
13667 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, true, true) catch |err| switch (err) {
13668 error.NotCoercible => unreachable,
13669 else => |e| return e,
13670 };
1366013671
1366113672 if (block.inlining) |inlining| {
1366213673 if (block.is_comptime) {
......@@ -19869,7 +19880,7 @@ fn coerce(
1986919880 inst: Air.Inst.Ref,
1987019881 inst_src: LazySrcLoc,
1987119882) CompileError!Air.Inst.Ref {
19872 return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, true) catch |err| switch (err) {
19883 return sema.coerceExtra(block, dest_ty_unresolved, inst, inst_src, true, false) catch |err| switch (err) {
1987319884 error.NotCoercible => unreachable,
1987419885 else => |e| return e,
1987519886 };
......@@ -19888,6 +19899,7 @@ fn coerceExtra(
1988819899 inst: Air.Inst.Ref,
1988919900 inst_src: LazySrcLoc,
1989019901 report_err: bool,
19902 is_ret: bool,
1989119903) CoersionError!Air.Inst.Ref {
1989219904 switch (dest_ty_unresolved.tag()) {
1989319905 .var_args_param => return sema.coerceVarArgParam(block, inst, inst_src),
......@@ -19939,7 +19951,7 @@ fn coerceExtra(
1993919951
1994019952 // T to ?T
1994119953 const child_type = try dest_ty.optionalChildAlloc(sema.arena);
19942 const intermediate = sema.coerceExtra(block, child_type, inst, inst_src, false) catch |err| switch (err) {
19954 const intermediate = sema.coerceExtra(block, child_type, inst, inst_src, false, is_ret) catch |err| switch (err) {
1994319955 error.NotCoercible => {
1994419956 if (in_memory_result == .no_match) {
1994519957 // Try to give more useful notes
......@@ -20056,7 +20068,7 @@ fn coerceExtra(
2005620068 return sema.addConstant(dest_ty, Value.@"null");
2005720069 },
2005820070 .ComptimeInt => {
20059 const addr = sema.coerceExtra(block, Type.usize, inst, inst_src, false) catch |err| switch (err) {
20071 const addr = sema.coerceExtra(block, Type.usize, inst, inst_src, false, is_ret) catch |err| switch (err) {
2006020072 error.NotCoercible => break :pointer,
2006120073 else => |e| return e,
2006220074 };
......@@ -20067,7 +20079,7 @@ fn coerceExtra(
2006720079 .signed => Type.isize,
2006820080 .unsigned => Type.usize,
2006920081 };
20070 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, false) catch |err| switch (err) {
20082 const addr = sema.coerceExtra(block, ptr_size_ty, inst, inst_src, false, is_ret) catch |err| switch (err) {
2007120083 error.NotCoercible => {
2007220084 // Try to give more useful notes
2007320085 in_memory_result = try sema.coerceInMemoryAllowed(block, ptr_size_ty, inst_ty, false, target, dest_ty_src, inst_src);
......@@ -20414,6 +20426,19 @@ fn coerceExtra(
2041420426
2041520427 if (!report_err) return error.NotCoercible;
2041620428
20429 if (is_ret and dest_ty.zigTypeTag() == .NoReturn) {
20430 const msg = msg: {
20431 const msg = try sema.errMsg(block, inst_src, "function declared 'noreturn' returns", .{});
20432 errdefer msg.destroy(sema.gpa);
20433
20434 const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = 0 };
20435 const src_decl = sema.mod.declPtr(sema.func.?.owner_decl);
20436 try sema.mod.errNoteNonLazy(ret_ty_src.toSrcLoc(src_decl), msg, "'noreturn' declared here", .{});
20437 break :msg msg;
20438 };
20439 return sema.failWithOwnedErrorMsg(block, msg);
20440 }
20441
2041720442 const msg = msg: {
2041820443 const msg = try sema.errMsg(block, inst_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(sema.mod), inst_ty.fmt(sema.mod) });
2041920444 errdefer msg.destroy(sema.gpa);
......@@ -20436,6 +20461,20 @@ fn coerceExtra(
2043620461 }
2043720462
2043820463 try in_memory_result.report(sema, block, inst_src, msg);
20464
20465 // Add notes about function return type
20466 if (is_ret and sema.mod.test_functions.get(sema.func.?.owner_decl) == null) {
20467 const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = 0 };
20468 const src_decl = sema.mod.declPtr(sema.func.?.owner_decl);
20469 if (inst_ty.isError() and !dest_ty.isError()) {
20470 try sema.mod.errNoteNonLazy(ret_ty_src.toSrcLoc(src_decl), msg, "function cannot return an error", .{});
20471 } else {
20472 try sema.mod.errNoteNonLazy(ret_ty_src.toSrcLoc(src_decl), msg, "function return type declared here", .{});
20473 }
20474 }
20475
20476 // TODO maybe add "cannot store an error in type '{}'" note
20477
2043920478 break :msg msg;
2044020479 };
2044120480 return sema.failWithOwnedErrorMsg(block, msg);
......@@ -21372,6 +21411,8 @@ fn storePtr2(
2137221411 // TODO do the same thing for anon structs as for tuples above.
2137321412 // However, beware of the need to handle missing/extra fields.
2137421413
21414 const is_ret = air_tag == .ret_ptr;
21415
2137521416 // Detect if we are storing an array operand to a bitcasted vector pointer.
2137621417 // If so, we instead reach through the bitcasted pointer to the vector pointer,
2137721418 // bitcast the array operand to a vector, and then lower this as a store of
......@@ -21380,12 +21421,18 @@ fn storePtr2(
2138021421 // https://github.com/ziglang/zig/issues/11154
2138121422 if (sema.obtainBitCastedVectorPtr(ptr)) |vector_ptr| {
2138221423 const vector_ty = sema.typeOf(vector_ptr).childType();
21383 const vector = try sema.coerce(block, vector_ty, uncasted_operand, operand_src);
21424 const vector = sema.coerceExtra(block, vector_ty, uncasted_operand, operand_src, true, is_ret) catch |err| switch (err) {
21425 error.NotCoercible => unreachable,
21426 else => |e| return e,
21427 };
2138421428 try sema.storePtr2(block, src, vector_ptr, ptr_src, vector, operand_src, .store);
2138521429 return;
2138621430 }
2138721431
21388 const operand = try sema.coerce(block, elem_ty, uncasted_operand, operand_src);
21432 const operand = sema.coerceExtra(block, elem_ty, uncasted_operand, operand_src, true, is_ret) catch |err| switch (err) {
21433 error.NotCoercible => unreachable,
21434 else => |e| return e,
21435 };
2138921436 const maybe_operand_val = try sema.resolveMaybeUndefVal(block, operand_src, operand);
2139021437
2139121438 const runtime_src = if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| rs: {
......@@ -21415,7 +21462,11 @@ fn storePtr2(
2141521462
2141621463 try sema.requireRuntimeBlock(block, runtime_src);
2141721464 try sema.queueFullTypeResolution(elem_ty);
21418 _ = try block.addBinOp(air_tag, ptr, operand);
21465 if (is_ret) {
21466 _ = try block.addBinOp(.store, ptr, operand);
21467 } else {
21468 _ = try block.addBinOp(air_tag, ptr, operand);
21469 }
2141921470}
2142021471
2142121472/// Traverse an arbitrary number of bitcasted pointers and return the underyling vector
test/cases/aarch64-macos/hello_world_with_updates.1.zig+2-1
......@@ -2,4 +2,5 @@ pub export fn main() noreturn {}
22
33// error
44//
5// :1:32: error: expected type 'noreturn', found 'void'
5// :1:32: error: function declared 'noreturn' returns
6// :1:22: note: 'noreturn' declared here
test/cases/compile_errors/address_of_number_literal.zig+1
......@@ -9,3 +9,4 @@ export fn entry() usize { return @sizeOf(@TypeOf(&foo)); }
99//
1010// :3:30: error: expected type '*const i32', found '*const comptime_int'
1111// :3:30: note: pointer type child 'comptime_int' cannot cast into pointer type child 'i32'
12// :3:10: note: function return type declared here
test/cases/compile_errors/incompatible_sentinels.zig+2
......@@ -21,8 +21,10 @@ export fn entry4() void {
2121//
2222// :4:12: error: expected type '[*:0]u8', found '[*:255]u8'
2323// :4:12: note: pointer sentinel '255' cannot cast into pointer sentinel '0'
24// :3:35: note: function return type declared here
2425// :7:12: error: expected type '[*:0]u8', found '[*]u8'
2526// :7:12: note: destination pointer requires '0' sentinel
27// :6:31: note: function return type declared here
2628// :10:35: error: expected type '[2:0]u8', found '[2:255]u8'
2729// :10:35: note: array sentinel '255' cannot cast into array sentinel '0'
2830// :14:31: error: expected type '[2:0]u8', found '[2]u8'
test/cases/compile_errors/incorrect_return_type.zig+1
......@@ -21,3 +21,4 @@
2121// :8:16: error: expected type 'tmp.A', found 'tmp.B'
2222// :10:12: note: struct declared here
2323// :4:12: note: struct declared here
24// :7:11: note: function return type declared here
test/cases/compile_errors/invalid_address_space_coercion.zig+1
......@@ -12,3 +12,4 @@ pub fn main() void {
1212//
1313// :2:12: error: expected type '*i32', found '*addrspace(.gs) i32'
1414// :2:12: note: address space 'gs' cannot cast into address space 'generic'
15// :1:34: note: function return type declared here
test/cases/compile_errors/invalid_pointer_keeps_address_space_when_taking_address_of_dereference.zig+1
......@@ -12,3 +12,4 @@ pub fn main() void {
1212//
1313// :2:12: error: expected type '*i32', found '*addrspace(.gs) i32'
1414// :2:12: note: address space 'gs' cannot cast into address space 'generic'
15// :1:34: note: function return type declared here
test/cases/compile_errors/pointer_with_different_address_spaces.zig+1
......@@ -12,3 +12,4 @@ export fn entry2() void {
1212//
1313// :2:12: error: expected type '*addrspace(.fs) i32', found '*addrspace(.gs) i32'
1414// :2:12: note: address space 'gs' cannot cast into address space 'fs'
15// :1:34: note: function return type declared here
test/cases/compile_errors/pointers_with_different_address_spaces.zig+1
......@@ -12,3 +12,4 @@ pub fn main() void {
1212//
1313// :2:13: error: expected type '*i32', found '*addrspace(.gs) i32'
1414// :2:13: note: address space 'gs' cannot cast into address space 'generic'
15// :1:35: note: function return type declared here
test/cases/compile_errors/slice_sentinel_mismatch-2.zig+1
......@@ -10,3 +10,4 @@ comptime { _ = foo; }
1010//
1111// :3:12: error: expected type '[:0]u8', found '[]u8'
1212// :3:12: note: destination pointer requires '0' sentinel
13// :1:10: note: function return type declared here
test/cases/compile_errors/stage1/test/helpful_return_type_error_message.zig+12-10
......@@ -16,15 +16,17 @@ export fn quux() u32 {
1616}
1717
1818// error
19// backend=stage1
19// backend=stage2
2020// target=native
21// is_test=1
2221//
23// tmp.zig:2:17: error: expected type 'u32', found 'error{Ohno}'
24// tmp.zig:1:17: note: function cannot return an error
25// tmp.zig:8:5: error: expected type 'void', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set'
26// tmp.zig:7:17: note: function cannot return an error
27// tmp.zig:11:15: error: cannot convert error union to payload type. consider using `try`, `catch`, or `if`. expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
28// tmp.zig:10:17: note: function cannot return an error
29// tmp.zig:15:14: error: cannot convert error union to payload type. consider using `try`, `catch`, or `if`. expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
30// tmp.zig:14:5: note: cannot store an error in type 'u32'
22// :2:18: error: expected type 'u32', found 'error{Ohno}'
23// :1:17: note: function cannot return an error
24// :8:5: error: expected type 'void', found '@typeInfo(@typeInfo(@TypeOf(tmp.bar)).Fn.return_type.?).ErrorUnion.error_set'
25// :7:17: note: function cannot return an error
26// :11:15: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(tmp.bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
27// :10:17: note: function cannot return an error
28// :11:15: note: cannot convert error union to payload type
29// :11:15: note: consider using `try`, `catch`, or `if`
30// :15:14: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(tmp.bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
31// :15:14: note: cannot convert error union to payload type
32// :15:14: note: consider using `try`, `catch`, or `if`
test/cases/compile_errors/try_in_function_with_non_error_return_type.zig+1
......@@ -8,3 +8,4 @@ fn something() anyerror!void { }
88// target=native
99//
1010// :2:5: error: expected type 'void', found 'anyerror'
11// :1:15: note: function cannot return an error
test/cases/compile_errors/unreachable_with_return.zig+2-1
......@@ -5,4 +5,5 @@ export fn entry() void { a(); }
55// backend=stage2
66// target=native
77//
8// :1:18: error: expected type 'noreturn', found 'void'
8// :1:18: error: function declared 'noreturn' returns
9// :1:8: note: 'noreturn' declared here
test/cases/compile_errors/variable_has_wrong_type.zig+1
......@@ -8,3 +8,4 @@ export fn f() i32 {
88// target=native
99//
1010// :3:12: error: expected type 'i32', found '*const [1:0]u8'
11// :1:15: note: function return type declared here
test/cases/x86_64-linux/hello_world_with_updates.1.zig+2-1
......@@ -2,4 +2,5 @@ pub export fn _start() noreturn {}
22
33// error
44//
5// :1:34: error: expected type 'noreturn', found 'void'
5// :1:34: error: function declared 'noreturn' returns
6// :1:24: note: 'noreturn' declared here
test/cases/x86_64-macos/hello_world_with_updates.1.zig+2-1
......@@ -2,4 +2,5 @@ pub export fn main() noreturn {}
22
33// error
44//
5// :1:32: error: expected type 'noreturn', found 'void'
5// :1:32: error: function declared 'noreturn' returns
6// :1:22: note: 'noreturn' declared here