# Why \`?\` and \`orelse unreachable\` behave inconsistently under ReleaseFast/ReleaseSmall

**URL:** <https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373>\
**Category:** Help\
**Tags:** language\
**Created:** [August 5, 2023, 9:33am UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373 "2023-08-05T09:33:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aiac](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aiac](https://ziggit.dev/u/aiac)\
**Post date:** [August 5, 2023, 9:33am UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/1 "2023-08-05T09:33:05Z")

</div>

![Screenshot 2023-08-05 173551](https://ziggit.dev/uploads/default/original/1X/269752847eaed9236f848d013353164cdf83da0d.png)

```zig
pub fn main() !void {
    var mm = m();
    std.debug.print("mm: {any}\n", .{mm});
    std.debug.print("type of mm: {any}\n", .{@TypeOf(mm)});

    std.debug.print("{s}\n", .{"-" ** 60});

    var nn = n();
    std.debug.print("nn: {any}\n", .{nn});
    std.debug.print("type of nn: {any}\n", .{@TypeOf(nn)});
}

const Point = struct { x: i32, y: u32 };

fn m() Point {
    var a: ?Point = null;
    return a.?;
}

fn n() Point {
    var a: ?Point = null;
    return a orelse unreachable;
}

```

- outpuit in ReleaseFast/ReleaseSmall mode ( **mm is printed and nn is not** )

```zig
mm: main.Point{ .x = 0, .y = 0 }
type of mm: main.Point
------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [August 5, 2023, 10:07am UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/2 "2023-08-05T10:07:35Z")

</div>

I think you’re running into undefined behavior here - from kristoff-it on the old zig reddit:

> Absolutely not. `unreachable` marks that a code path is impossible to trigger. In safe modes you will get a panic if you got it wrong, while in unsafe modes (ReleaseFast, ReleaseSmall) you will potentially get a misbehaving program.

Your point is well taken though - it’s strange that in the documentation it says that it’s equivalent to unreachable. @kristoff any thoughts?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [August 5, 2023, 10:21am UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/3 "2023-08-05T10:21:52Z")

</div>

I tried it in [godbolt](https://godbolt.org/z/W9dsGv5Tc)  
It indeed seems to produce different code, both in debug and release.  
`orelse unreachable` produces more code in debug, which I guess makes sense given that `.?` is a more specific concept.  
However llvm seems to be better at optimizing `orelse unreachable`

But I personally think it’s fine that they behave differently, given that it’s only noticable within the bounds of undefined behavior.

---

<div class="post-metadata">

**Author:** ![aiac](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@aiac](https://ziggit.dev/u/aiac)\
**Post date:** [August 5, 2023, 10:49am UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/4 "2023-08-05T10:49:03Z")

</div>

Also I’m confused, if .? is different from else reachable, why .? behaves differently in releaseSafe and releaseFast 🤔

---

<div class="post-metadata">

**Author:** ![kristoff](https://ziggit.dev/user_avatar/ziggit.dev/kristoff/32/9_2.png) [@kristoff](https://ziggit.dev/u/kristoff)\
**Post date:** [August 5, 2023, 6:52pm UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/5 "2023-08-05T18:52:07Z")

</div>

I’m fairly confident this is a bug. Unless the semantics of `.?` changed recently, it really is supposed to be equivalent to `catch unreachable`.

---

<div class="post-metadata">

**Author:** ![mlugg](https://ziggit.dev/user_avatar/ziggit.dev/mlugg/32/437_2.png) [@mlugg](https://ziggit.dev/u/mlugg)\
**Post date:** [August 5, 2023, 7:07pm UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/6 "2023-08-05T19:07:01Z")

</div>

It’s equivalent when the behavior is defined - we’re in UB land here, so anything goes, including “maybe break the program and maybe don’t”. `x.?` and `x catch unreachable` will likely be lowered by the compiler in slightly different (but semantically equivalent provided `x` is non-null) ways, allowing the UB to behave differently here.

---

<div class="post-metadata">

**Author:** ![kristoff](https://ziggit.dev/user_avatar/ziggit.dev/kristoff/32/9_2.png) [@kristoff](https://ziggit.dev/u/kristoff)\
**Post date:** [August 5, 2023, 9:47pm UTC](https://ziggit.dev/t/why-and-orelse-unreachable-behave-inconsistently-under-releasefast-releasesmall/1373/7 "2023-08-05T21:47:06Z")

</div>

good point
