Repository: DioxusLabs/dioxus

Issue #536: Fermi Error at Optlevel 2-3
Created: 2022-08-30T19:16:30Z
Problem
When compiling a simple programm using dioxus with fermi and the web feature enabled, using trunk,
this error message appears and the application does not work properly as soon as the page is opened in a browser

  Error Message
panicked at 'called Result::unwrap() on an Err value: Any { .. }', /home/misha/.cargo/registry/src/github.com-1ecc6299db9ec823/fermi-0.2.1/src/root.rs:47:43
Stack:
getImports/imports.wbg.__wbg_new_693216e109162396@http://localhost:8080/frontend-a85016710e7cf20c.js:338:21
console_error_panic_hook::hook::hccdc65123d386add@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[223]:0x1fd63
core::ops::function::Fn::call::h03b5ba35aba6f2f3@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[1013]:0x3ab25
std::panicking::rust_panic_with_hook::h84feca33bd4bd229@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[418]:0x30cb3
std::panicking::begin_panic_handler::{{closure}}::hd2eacd3bb9ff1eab@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[493]:0x34113
std::sys_common::backtrace::__rust_end_short_backtrace::h976699518d897fb1@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[845]:0x3a2c6
rust_begin_unwind@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[687]:0x38a98
core::panicking::panic_fmt::hc171d095bc4a492d@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[689]:0x38b19
core::result::unwrap_failed::h68ab818eb89182b6@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[515]:0x34cdf
fermi::root::AtomRoot::register::hf7d7317a0f91fa2a@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[210]:0x1dde5
fermi::hooks::read::use_read::hb21da33d9e04cfca@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[488]:0x33dfd
frontend::Component::ha848ed4cf3cb4e81@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[848]:0x3a2f8
<dioxus_core::nodes::VComponentProps as dioxus_core::nodes::AnyProps>::render::he92fd34f9b06d3e8@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[678]:0x38836
dioxus_core::scopes::ScopeArena::run_scope::hc59c6e8afeb6c864@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[180]:0x1893d
dioxus_core::diff::DiffState::create_node::h5273c7db041d8d99@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[138]:0x8b2b
dioxus_core::virtual_dom::VirtualDom::rebuild::hc9371f0c64a5a9bc@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[182]:0x190c6
<core::future::from_generator::GenFuture as core::future::future::Future>::poll::h2c32271d32ce7fa9@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[133]:0x3d3a
wasm_bindgen_futures::task::singlethread::Task::run::h300c65dbd2dfe601@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[388]:0x2f27f
wasm_bindgen_futures::queue::Queue::new::{{closure}}::h580e790085673073@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[227]:0x20616
<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h412ac59d3b7c064a@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[795]:0x39ded
__wbg_adapter_24@http://localhost:8080/frontend-a85016710e7cf20c.js:246:10
real@http://localhost:8080/frontend-a85016710e7cf20c.js:190:20
promise callbackgetImports/imports.wbg.__wbg_then_18da6e5453572fc8@http://localhost:8080/frontend-a85016710e7cf20c.js:904:33
js_sys::Promise::then::h845cd8a514441641@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[906]:0x3a66b
wasm_bindgen_futures::queue::Queue::schedule_task::h6319dc014492e3a9@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[329]:0x2ae7a
wasm_bindgen_futures::task::singlethread::Task::spawn::h25873dcb0a7f64b7@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[562]:0x361cf
dioxus_web::launch::h47bece2a21662e15@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[444]:0x32132
frontend::main::h25b739b51f73a32c@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[1010]:0x3ab0e
std::sys_common::backtrace::__rust_begin_short_backtrace::h7ec7bb65dbbfea46@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[1011]:0x3ab16
std::rt::lang_start::{{closure}}::h5ab029a62639367a@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[716]:0x390b9
std::rt::lang_start_internal::he9e674c0ac64f681@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[447]:0x3233f
main@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[715]:0x39098
@http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:wasm-function[921]:0x3a742
finalizeInit@http://localhost:8080/frontend-a85016710e7cf20c.js:947:10
init@http://localhost:8080/frontend-a85016710e7cf20c.js:976:12
async@http://localhost:8080/:12:54
frontend-a85016710e7cf20c.js:351:13
__wbg_error_09919627ac0992f5 http://localhost:8080/frontend-a85016710e7cf20c.js:351
hccdc65123d386add http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:130678
h03b5ba35aba6f2f3 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:240421
h84feca33bd4bd229 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:199859
hd2eacd3bb9ff1eab http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:213267
h976699518d897fb1 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:238278
rust_begin_unwind http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:232088
hc171d095bc4a492d http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:232217
h68ab818eb89182b6 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:216287
hf7d7317a0f91fa2a http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:122341
hb21da33d9e04cfca http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:212477
ha848ed4cf3cb4e81 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:238328
<dioxus_core::nodes::VComponentProps as dioxus_core::nodes::AnyProps>::render::he92fd34f9b06d3e8 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:231478
hc59c6e8afeb6c864 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:100669
h5273c7db041d8d99 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:35627
hc9371f0c64a5a9bc http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:102598
<core::future::from_generator::GenFuture as core::future::future::Future>::poll::h2c32271d32ce7fa9 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:15674
h300c65dbd2dfe601 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:193151
h580e790085673073 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:132630
<dyn core::ops::function::FnMut<(A,)>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h412ac59d3b7c064a http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:237037
__wbg_adapter_24 http://localhost:8080/frontend-a85016710e7cf20c.js:246
real http://localhost:8080/frontend-a85016710e7cf20c.js:190
(Async: promise callback)
__wbg_then_18da6e5453572fc8 http://localhost:8080/frontend-a85016710e7cf20c.js:904
h845cd8a514441641 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:239211
h6319dc014492e3a9 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:175738
h25873dcb0a7f64b7 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:221647
h47bece2a21662e15 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:205106
h25b739b51f73a32c http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:240398
h7ec7bb65dbbfea46 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:240406
h5ab029a62639367a http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:233657
he9e674c0ac64f681 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:205631
main http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:233624
 http://localhost:8080/frontend-a85016710e7cf20c_bg.wasm:239426
finalizeInit http://localhost:8080/frontend-a85016710e7cf20c.js:947
init http://localhost:8080/frontend-a85016710e7cf20c.js:976
 http://localhost:8080/:12

This only happens when rusts opt-level is set to 2 or 3 (either manually in cargo.toml or by doing a release build, which defaults to 3)
On all other opt-levels, no error message appears and the application behaves as intended
Steps To Reproduce
I uploaded a minnimum Setup to trigger the error in this repo
Alternatively, the code in the main.rs is
use dioxus::prelude::*;

fn main() {
    dioxus::web::launch(app);
}

fn app(cx: Scope) -> Element {
    let atom = use_read(&cx, ATOM);

    cx.render(rsx! {
        Component{},
    })
}

pub struct S {
    pub x: String,
}

pub static ATOM: Atom<Vec<S>> = |_| Vec::new();
pub static OTHER_ATOM: Atom<Vec<String>> = |_| Vec::new();

pub fn Component(cx: Scope) -> Element {
    let other_atom = use_read(&cx, OTHER_ATOM);
    None
}
And otherwise its just a minimal dioxus application for the web.
Of note, the error can be avoided by removing either of the use_read use_read expressions,
changing the type in other atom to a numeric type (i64), the type in ATOM to some simple type like string,
removing the field x of struct S or changing its type to a numeric type.
So overall very peculiar.
It could also be that this is in fact not a bug in dioxus but a bug in
the rust compiler/optimizer/wasm toolchain, though i think that that is less likely than a dioxus bug.
Expected behavior
The behavior for any opt-level is the same, the application works without an error message
Screenshots
Environment:

Dioxus version: 0.2.4
Rust version: 1.63.0
OS info: Archlinux (same on Windows 11 and MacOS)
App platform: web

Duplicate issue #706: AtomState has strange behavior in release build
Created: 2023-01-03T04:58:58Z
use dioxus::{
    fermi::{use_atom_state, AtomState},
    prelude::*,
};

fn main() {
    dioxus::desktop::launch(app);
}

#[derive(Default)]
struct Value {
    v: usize,
}

#[derive(Props)]
struct TestProps<'a> {
    id: &'a str,
    value: &'a AtomState<Value>,
}

#[allow(non_snake_case)]
fn Test<'a>(cx: Scope<'a, TestProps<'a>>) -> Element {
    let id = cx.props.id;
    let value = cx.props.value;

    cx.render(rsx! {
        div {
            button {
                onclick: move |_| {value.set(Value {v:1})},
                "{id}"
            }
            label {
                [format_args!("{}", value.v)],
            }
        }
    })
}

static VALUE1: Atom<Value> = |_| Value::default();
static VALUE2: Atom<Value> = |_| Value::default();

fn app(cx: Scope) -> Element {
    let value1 = use_atom_state(&cx, VALUE1);
    let value2 = use_atom_state(&cx, VALUE2);
    rsx!(cx,
        div {
            Test {
                id: "value1",
                value: value1,
            }
            Test {
                id: "value2",
                value: value2,
            }
        }
    )
}
rust toolchain is update.
run "cargo run"

run "cargo run --release"

Closing comment (2023-01-03T16:04:10Z): This seems related to #536 and statics merging with higher opt-levels. The fix merged in that case relies on the TypeId of the atom which would not help here.
#562 Uses a different mechanism to generate unique ids that could solve this issue