Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The provided example ([rust playground](https://play.rust-lang.org/?version=stable&mode=release&edit...) can generate assembly ('...' next to 'Run').

`main` just calls `panic` immediately. The functions returns undef. The reason: `x > 150` is undefined, `undef || ...` is undefined thus the first if-statement with side-effects may be interpret undefined as true.

I wonder why the optimiser didn't choose false and let the assert pass?



> I wonder why the optimiser didn't choose false and let the assert pass?

I don't know exactly, but it's kind of a moot point. I would have negated the statements until I found a way to make it return what I want it to.

The point is that the compiler picks some result, and it does so "locally", so when it picks results for multiple comparisons it makes no attempt to check that these results are all "consistent" and can even arise for a single value. The result that we can observe is that the value is "unstable".

To "fix" this (assuming we wanted to specify that unstable values are not allowed in C/C++/Rust), the compiler would have to keep track of which constant foldings it already did for some uninitialized value, and make sure it remains consistent with that. That's a hard problem and likely undecidable in general. Allowing unstable values frees the optimizer from this burden, letting it optimize more code better.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: