add helpful error note for when function cannot return an error

This has caused frequent confusion since it looks like you are handling
errors correctly with a try but you forgot to change your return type.
This commit is contained in:
Vexu
2020-07-28 00:07:15 +03:00
committed by Andrew Kelley
parent b8e22d2002
commit 5f0bde6358
2 changed files with 43 additions and 6 deletions

View File

@@ -18,6 +18,28 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:2:16: error: sentinels are only allowed on slices and unknown-length pointers",
});
cases.addTest("helpful return type error message",
\\export fn foo() u32 {
\\ return error.Ohno;
\\}
\\fn bar() !u32 {
\\ return error.Ohno;
\\}
\\export fn baz() void {
\\ try bar();
\\}
\\export fn quux() u32 {
\\ return bar();
\\}
, &[_][]const u8{
"tmp.zig:2:17: error: expected type 'u32', found 'error{Ohno}'",
"tmp.zig:1:17: note: function cannot return an error",
"tmp.zig:8:5: error: expected type 'void', found '@TypeOf(bar).ReturnType.ErrorSet'",
"tmp.zig:7:17: note: function cannot return an error",
"tmp.zig:11:15: error: expected type 'u32', found '@TypeOf(bar).ReturnType.ErrorSet!u32'",
"tmp.zig:10:18: note: function cannot return an error",
});
cases.addTest("int/float conversion to comptime_int/float",
\\export fn foo() void {
\\ var a: f32 = 2;