-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
infinite loop optimized away unless function is exported #1658
Comments
Isn't this a long time LLVM bug present in other languages like C and C++ where an empty infinite loop results in no consequences and thus creates invalid programs? (for example there are many articles like https://stackoverflow.com/questions/59925618/how-do-i-make-an-infinite-empty-loop-that-wont-be-optimized-away). I guess the solution Zig would need for this specific case is adding in a |
export fn square(num: u8) u8 {
//while(true){}
return eternity(123);
}
pub fn eternity(bla:u8) u8 {
while(true){
asm volatile("nop");
}
} This code generates as intended as we can see on GodBolt. |
You probably have forgot to apply the flag -Drelease-safe, this happens to me as well on godbolt using Zig trunk |
Related: rust-lang/rust#82884 - apparently this was fixed in LLVM 12. export fn square(num: u8) u8 {
_ = num;
return eternity(123);
}
pub fn eternity(bla: u8) u8 {
_ = bla;
while (true) {}
}
export fn square2(num: u8) u8 {
_ = num;
while (true) {}
}
export fn square3() u8 {
while (true) {}
} This code, paired with
pub fn main() void {
while (true) {}
} I've also tested this executable with all four -O flags, and this works on all of them. |
See asm output on goldbold.
As you said during live coding, it seems like the eternal loop is considered to have no side effects and is thus removed.
Expected behaviour would be to refuse compilation
eternity
being declared as returningvoid
, an implicitreturn;
at the end of the function cannot be reachedThe text was updated successfully, but these errors were encountered: