-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new ast-check compile error: control flow implicitly returns from non-void function #10420
Comments
I think we should amend this proposal to allow implicit returns in functions with return type fn fatal() noreturn {
std.log.err("something bad happened, can't recover\n", .{});
std.os.exit(1);
} |
Ah, the existence of fn foo(c: bool) i32 {
if (c) {
return 1234;
}
exit(1);
} This code should be allowed, for example. |
Here's fn foo(c: bool) i32 {
if (c) {
return 1234;
}
exit(1);
unreachable; // this clearly communicates intent, to both early compiler stages, and readers!
} I think that adequately solves this problem? |
@rohlem then you don't get a compile error at the call site if, for example, the return type of |
@ifreund Maybe we could introduce such a compile error? |
We have this, it's called |
Yeah, that's not how things work in practice. Unreachable code errors MUST happen in AstGen (i.e. before semantic analysis) as branching at comptime allows arbitrary code to be only reachable when targeting a certain OS for example. |
@ifreund Why can't we emit a simple "unreachable poison" AstNode (-> ZIR -> AIR)? (Special-case it if normal I realize that this means the problem isn't isolated to be fully-verified during AstGen. EDIT: If what I'm suggesting here is basically already implemented or planned in later stages anyway, then you can ignore these details and I'll yield to my original suggestion that I'd prefer |
I believe this would be unblocked by #17234 . |
Functions end with an implicit
return;
which implicitly returns a void value. Therefore, functions which returnvoid
have no requirement to have an explicitreturn
expression:However functions which do not return
void
require areturn
statement or there will be an error such as this:Currently this error occurs during type checking, and so it won't trigger if the function is not referenced. It also will allow something like this:
Again, no error on this one.
This proposal is to add an additional check into
zig ast-check
(also known asAstGen
). It makes the rules a bit stricter; functions which do not have a trivialvoid
return type (literally the identifiervoid
as the return type; no detection of aliasing) are required to terminate the function body block with an expression of typenoreturn
. This could bereturn
or it could be, for example,unreachable
, or it could be awhile(true) {}
loop.The above example (with
MyVoid
) would become:This compile error is directly related to all the
unreachable code
compile errors done by ast-check.Additionally accepted amendment to the proposal
The text was updated successfully, but these errors were encountered: