5 ms·
This is just a symptom of a bad assert() implementation, which funny enough is the standard. If you properly (void) it out, side effects are maintained. https:
by nyc_pizzadev 6mo ago
This is just a symptom of a bad assert() implementation, which funny enough is the standard. If you properly (void) it out, side effects are maintained.
https://github.com/fiberfs/fiberfs/blob/7e79eaabbb180b0f1a7942b318e981293318a6da/src/utils/fbr_assert.h#L45 https://github.com/fiberfs/fiberfs/blob/7e79eaabbb180b0f1a79...
- omoikane 6mo agoassert() is meant to be compiled away if NDEBUG is defined, otherwise it shouldn't be called assert(). Given that assert() may be compiled away, it makes sense not to give it anything that has side effects. Abseil has the convention where instead of assert(), users call "CHECK" for checks that are guaranteed to happen at run time, or "DCHECK" for checks that will be compiled away when NDEBUG is defined. https://github.com/abseil/abseil-cpp/blob/0093ac6cac892086a6d7d09c55421a2a4c2cdb2e/absl/log/check.h#L21 https://github.com/abseil/abseil-cpp/blob/0093ac6cac892086a6... https://github.com/abseil/abseil-cpp/blob/0093ac6cac892086a6d7d09c55421a2a4c2cdb2e/absl/log/internal/check_impl.h#L39 https://github.com/abseil/abseil-cpp/blob/0093ac6cac892086a6...
- nmilo 6mo agoIf your assert compiles down to `if (condition) {}` in production then the compiler will optimize away the condition while keeping any side effects.
- IshKebab 6mo agoYeah which may not be what you want. E.g. `assert(expensive_to_compute() == 0)`. The correct way to solve this is with debug asserts (as in Rust, or how the parent described).
- nyc_pizzadev 6mo agoGenuine question, does Rust know if `expensive_to_compute()` has side effects? There are no params, so could it be compiled out if the return value is ignored? Ex: `expensive_to_compute()` What about: `(void) expensive_to_compute()`?
- IshKebab 6mo agoNo, Rust is the same as C++ in terms of tracking side effects. It doesn't matter that there are no parameters. It could manipulate globals or call other functions that have side effects (e.g. printing).
- functional_dev 6mo agoWhat about rust const fn()? I think it guarantees there are no side effects
- IshKebab 6mo agoI think you're right. Equivalent to C++'s constexpr.
- aw1621107 6mo agoNo, in general Rust doesn't (and can't) know whether an arbitrary function has side effects. The compiler does arguably have a leg up since Rust code is typically all built from source, but there's still things like FFI that act as visibility barriers for the compiler.
- nmilo 6mo agoCompilers are very good these days. If it has no side effects it will likely be compiled out.