# Unimplemented! macro optimization bug

**URL:** <https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648>\
**Category:** Uncategorized\
**Created:** [January 23, 2017, 4:57pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648 "2017-01-23T16:57:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DenisKolodin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/deniskolodin/32/1251_2.png) [@DenisKolodin](https://internals.rust-lang.org/u/DenisKolodin)\
**Post date:** [January 23, 2017, 4:57pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/1 "2017-01-23T16:57:31Z")

</div>

I saw a bug with optimization for `unimplemented!` macro 😕

I have a piece of code which never reach an arm with `unimplemented!` call, and it’s absolutely true for debug build, but… when I try to build the release it **reaches (!) unreachable arm** and do `unimplemented!` panic.

I believe there is no bug in my code, because if I change unreachable arm to `return` expression bug disappears. Release build works well and the second arm never reached again.

```rust
match model.find_resource(entry.key()) {
    Some(res) => {
        let record = ResourceRecord::new(
            res.initial_value,
            res.lower_limit,
            res.upper_limit);
        entry.insert(record)
    },
    None => {
        // unimplemented!()
        return Err(E::from(Error::ResourceNotFound(format!("resource '{}' lost", entry.key()))));
    },
}

```

I didn’t create an issue, because I haven’t an good example to reproduce it. The real code where I saw it is large and proprietary. But I believe there is inconstant optimizer’s behavior.

Did anyone see something similar?

I use: nightly-x86\_64-pc-windows-gnu (default) rustc 1.16.0-nightly (9761b17d5 2017-01-21)

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [January 23, 2017, 5:29pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/2 "2017-01-23T17:29:17Z")

</div>

While compiler bugs are always a possibility, most of the time, issues that look like compiler bugs are the result of undefined behavior in your code. There are two ways this can result in differing behavior between unoptimized and optimized builds: sometimes the compiler makes an optimization that is valid according to the language rules but not what you expected; sometimes it’s just random differences in code generation (e.g. stack layout) that cause things like use-after-free to have different observed behavior.

In Rust, it should only be possible to invoke undefined behavior using `unsafe`, though there are a fair number of known bugs. Of course, the code triggering undefined behavior doesn’t have to be anywhere near the code where visible symptoms occur.

Sorry for the very general advice, but without more details on your code there’s no way to investigate further. One thing you can try is running your code under valgrind to identify certain classes of errors.

---

<div class="post-metadata">

**Author:** ![seanmonstar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seanmonstar/32/36_2.png) [@seanmonstar](https://internals.rust-lang.org/u/seanmonstar)\
**Post date:** [January 23, 2017, 5:55pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/3 "2017-01-23T17:55:48Z")

</div>

You can see that unimplemented just expands to a panic, here: [https://github.com/rust-lang/rust/blob/1.14.0/src/libcore/macros.rs#L513](https://github.com/rust-lang/rust/blob/1.14.0/src/libcore/macros.rs#L513)

Could it be that your change to return an `Err` does actually get triggered, but the `Err` is never inspected or is ignored, and so it seems like it doesn’t happen? For example, if you change it to an `Err`, and then all the places that call your method could `your_method(key).unwrap()`.

---

<div class="post-metadata">

**Author:** ![DenisKolodin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/deniskolodin/32/1251_2.png) [@DenisKolodin](https://internals.rust-lang.org/u/DenisKolodin)\
**Post date:** [January 23, 2017, 6:27pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/4 "2017-01-23T18:27:35Z")

</div>

Interesting idea! I’ve changed it to `.unwrap()`. If I build it with `cargo build` and run the program works without panic (it completes long simulation process), but it panics and fails if I build the program with `cargo build --release`. I don’t change the code, but behavior is different. Strange thing 😁

---

<div class="post-metadata">

**Author:** ![DenisKolodin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/deniskolodin/32/1251_2.png) [@DenisKolodin](https://internals.rust-lang.org/u/DenisKolodin)\
**Post date:** [January 23, 2017, 7:02pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/5 "2017-01-23T19:02:51Z")

</div>

To be honest I use a lot of context switching in the program. But optimizer breaks something if panics included.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [January 23, 2017, 7:33pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/6 "2017-01-23T19:33:20Z")

</div>

I think we’re going to need a minimized, self-contained test case to help you any further, and probably this conversation should be happening on the users board.

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [January 23, 2017, 7:37pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/7 "2017-01-23T19:37:09Z")

</div>

What exactly do you mean by context switching?

---

<div class="post-metadata">

**Author:** ![DenisKolodin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/deniskolodin/32/1251_2.png) [@DenisKolodin](https://internals.rust-lang.org/u/DenisKolodin)\
**Post date:** [January 23, 2017, 7:57pm UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/8 "2017-01-23T19:57:23Z")

</div>

I use [https://github.com/rustcc/coroutine-rs](https://github.com/rustcc/coroutine-rs) library and **lua coroutines** both.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:27am UTC](https://internals.rust-lang.org/t/unimplemented-macro-optimization-bug/4648/9 "2019-03-25T08:27:45Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
