DioxusLabs / dioxus

Fullstack app framework for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
20.05k stars 764 forks source link

Weird error with new signals lifetimes #2036

Closed marc2332 closed 5 months ago

marc2332 commented 5 months ago

Problem

I am getting an error with signals that seems unnecessary and it used to compile just fine before.

Code:

#[allow(non_snake_case)]
#[component]
fn Comp(value: i32) -> Element {
    rsx!(
        label {
            "{value}"
        }
    )
}

fn app() -> Element {
    let count = use_signal(|| 0);

    rsx!(
        Comp {
            value: *count.read()
        }
    )
}

Error:

> cargo run --example counter
    Blocking waiting for file lock on build directory
   Compiling examples v0.0.0 (D:\Projects\freya)
error[E0597]: `count` does not live long enough
  --> examples\counter.rs:27:21
   |
23 |     let count = use_signal(|| 0);
   |         ----- binding `count` declared here
...
27 |             value: *count.read()
   |                     ^^^^^-------
   |                     |
   |                     borrowed value does not live long enough
   |                     a temporary with access to the borrow is created here ...
...
30 | }
   | -
   | |
   | `count` dropped here while still borrowed
   | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `generational_box::references::GenerationalRef<Ref<'_, i32>>`
   |
   = note: the temporary is part of an expression at the end of a block;
           consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
   |
25 ~     let x = rsx!(
26 |         Comp {
27 |             value: *count.read()
28 |         }
29 ~     ); x
   |

For more information about this error, try `rustc --explain E0597`.
error: could not compile `examples` (example "counter") due to previous error

Expected behavior

Compile

Environment: