On Wednesday, 30 July 2014 at 15:49:33 UTC, Daniel Murphy wrote:
"Tofu Ninja" wrote in message
news:dtjqnyucskwnqjvks...@forum.dlang.org...
Question?
If an if condition throws or returns in its body is it ok for
the optimizer to 'assume' that the condition is false after
and make optimizations for it? If so then every one
complaining that assert gets removed in -release should really
just be using enforce.
Yes. In fact, it's required that an optimizer do this in order
not to be considered garbage.
The possibility for the optimizer to make the same
optimizations for assert can be done for enforce, the only
difference is that it does not get removed in -release....
Not quite. With enforce, it is impossible (ignoring hardware
or optimizer errors) for the condition to be false if the
enforce was not triggered. Because the assert is not checked,
the condition could be false and the code could do something
awful.
Also, the optimizer can only use the enforce's assumption
_after_ it has checked the condition. Since with assert it
doesn't need to check, it can go backwards and assume the
previous code will never produce a value that can violate the
assertion.
if (y > 7) // x can't possibly be >= 5, so this can't be true
x = y + 1;
assert(x < 5);
if (y > 7) // the program will throw if >= 5, but that doesn't
mean it can't be true
x = y + 1;
enforce(x < 5);
Assert is much much more powerful.
Ok so what is sounds like, is that assert is really what every
one claims assume is and enforce is what every one claims assert
is...
Maybe a documentation change on assert is all that is needed, it
seems like the docs describe assert as something similar to
enforce. Maybe it should be described in a way similar to assume
with the added benefit that in non-release modes it will actually
check that it is true at runtime. Also if that was changed then
the documentation on version(assert) would need changing too.