# Assert\_safe alias for: if (std.debug.runtime\_safety)

**URL:** <https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155>\
**Category:** Brainstorming\
**Tags:** build-system\
**Created:** [October 30, 2026, 9:18pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155 "2026-10-30T21:18:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jangorecki](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@jangorecki](https://ziggit.dev/u/jangorecki)\
**Post date:** [October 30, 2026, 9:18pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/1 "2026-10-30T21:18:04Z")

</div>

I would find it very useful.

Rather than writing

```zig
if (std.debug.runtime_safety) assert(g < self.first_rows.len());

```

I would like to write

```zig
assert_safe(g < self.first_rows.len());

```

code is cleaner, easier to read.

I am on 0.17.0

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [October 30, 2026, 11:13pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/2 "2026-10-30T23:13:45Z")

</div>

It would first be helpful to understand _why_ you’re doing:

```zig
if (std.debug.runtime_safety) assert(g < self.first_rows.len());

```

* * *

Note that there has been a lot written about assert that you might be interested in reading:

- [You Must Fix Your Asserts | Loris Cro's Blog](https://kristoff.it/blog/fix-your-asserts/)
- [Assert but only in paranoid mode](https://ziggit.dev/t/assert-but-only-in-paranoid-mode/9934)
- [Proposal : builtin @assert() One assert to rule them all](https://ziggit.dev/t/proposal-builtin-assert-one-assert-to-rule-them-all/9707)
- [Do we need an @assert?](https://ziggit.dev/t/do-we-need-an-assert/13447)

---

<div class="post-metadata">

**Author:** ![jangorecki](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@jangorecki](https://ziggit.dev/u/jangorecki)\
**Post date:** [October 30, 2026, 11:23pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/3 "2026-10-30T23:23:51Z")

</div>

> [@squeek502](#):
>
> hat there has been a lot written about assert that you might

to run assert checks only during zig optimize=.safe and not other optimize levels

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [October 30, 2026, 11:41pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/4 "2026-10-30T23:41:21Z")

</div>

I think you’d benefit from reading [You Must Fix Your Asserts | Loris Cro's Blog](https://kristoff.it/blog/fix-your-asserts/)

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [October 31, 2026, 12:03am UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/5 "2026-10-31T00:03:10Z")

</div>

Hi!

I assume you want to make sure that the argument to `assert_safe` is not evaluated, for example because it is slow or doesn’t get optimized out.

This is not directly possible in Zig because it does not have macros, the argument to `assert_safe`will still be evaluated.

If this is not your goal, see the linked articles.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [October 31, 2026, 10:14am UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/6 "2026-10-31T10:14:43Z")

</div>

> [@jangorecki](#):
>
> Rather than writing
> 
> ```zig
> if (std.debug.runtime_safety) assert(g < self.first_rows.len());
> 
> ```
> 
> I would like to write
> 
> ```zig
> assert_safe(g < self.first_rows.len());
> 
> ```

these are not equivalent, in the first snippet `self.first_rows.len()` is behind a comptime condition, so it will certainly not be called if that condition is false.

in the second it is not, meaning that if the optimiser can’t determine if it can remove that call, then it will remain in the second case; this is important if the call is expensive.

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [October 31, 2026, 11:21am UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/7 "2026-10-31T11:21:10Z")

</div>

> [@squeek502](#):
>
> Note that there has been a lot written about assert that you might be interested in reading:

At this point `assert` has become _the_ canonical Ziggit topic 😄

> [@jangorecki](#):
>
> to run assert checks only during zig optimize=.safe and not other optimize levels

I think the best way to achieve this is with the following:

```zig
const assert: ?fn (ok: bool) void =
    if (std.debug.runtime_safety)
        std.debug.assert
    else
        null;

if (assert) |x| x(g < self.first_rows.len());

```

This is a bit less prone to accidental mistakes than other equivalent forms like `assert((comptime disable_assert) or expensiveCheck())`.

But I’d also second the recommendation to read Loris’s blog post on the subject. It is normally a good idea to leave your assertions enabled even in your release builds (perhaps with the exception of checks that are so expensive that they show up in a profiler).

---

<div class="post-metadata">

**Author:** ![Calder-Ty](https://ziggit.dev/user_avatar/ziggit.dev/calder-ty/32/10237_2.png) [@Calder-Ty](https://ziggit.dev/u/Calder-Ty)\
**Post date:** [October 31, 2026, 12:59pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/8 "2026-10-31T12:59:12Z")

</div>

> At this point `assert` has become _the_ canonical Ziggit topic 😄

that and Traits/comptime interfaces. @squeek502’s list is a good compilation of the discussions about Assert.

---

<div class="post-metadata">

**Author:** ![jangorecki](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@jangorecki](https://ziggit.dev/u/jangorecki)\
**Post date:** [October 31, 2026, 4:37pm UTC](https://ziggit.dev/t/assert-safe-alias-for-if-std-debug-runtime-safety/18155/9 "2026-10-31T16:37:30Z")

</div>

> [@cancername](#):
>
> I assume you want to make sure that the argument to `assert_safe` is not evaluated, for example because it is slow or doesn’t get optimized out.

yes,  
I have a table (like a data frame), I use asserts that runs on per-column basis.  
I could add many per-group and some per-row asserts, but it will impact all 4 build optimizations level in `make test`. My goal is running those slow asserts (for each row) on just safe level, and not on 3 others.
