compiler awareness of reachable functions, function reachability analysis #4094
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
Reaching
unreachable
is Illegal Behavior (#2402). If the compiler can prove that this happens at compile time, then it can be a compile error, which is strongly preferred to a runtime check, or worse, Undefined Behavior.Unfortunately, the compiler is unable to detect some obvious cases when it should be a compile error. For example:
This should be a compile error. It is understood that
main
is reachable, and therefore control flow is guaranteed to unconditionally reachunreachable
. Another example:In this case it should also be a compile error.
foo()
basically is equivalent tounreachable
in this case.Now it should not be a compile error.
unreachable
is no longer guaranteed to be hit. You can get the idea here that the call graph will quickly lose the ability to prove that functions are unconditionally reachable. So to take advantage of this it may be necessary to annotate that functions are reachable. Or maybe have some kind of rule that all functions must be reachable, unless otherwise specified.This proposal is vague because it's not clear how to go about solving this problem yet.
Where this starts to be important is with #425. Example:
The use case I have here is when using
assert
with a comptime known parameter:Assuming
x > y
happens to be a comptime-known value, #425 would change this into:unreachable;
It would be really, really beneficial if this could turn into a compile-error. Implicit compile-time asserts. But this
unreachable
can only be reported as a compile error if zig can prove that theunreachable
is unconditionally reachable.So unless this proposal can be implemented, we are left with explicit compile-time asserts:
The text was updated successfully, but these errors were encountered: