Constrain Props lifetime to parent, not child scope lifetime
@@ -0,0 +1,24 @@
+use dioxus::prelude::*;
+
+fn main() {}
+fn app(cx: Scope) -> Element {
+ let count = vec![1, 2, 3];
+ render! {
+ unsafe_child_component {
+ borrowed: &count
+ }
+}
+#[derive(Props)]
+struct Testing<'a> {
+ borrowed: &'a Vec<u32>,
+fn unsafe_child_component<'a>(cx: Scope<'a, Testing<'a>>) -> Element<'a> {
+ cx.render(rsx! {
+ div { "{cx.props.borrowed:?}" }
+ })
@@ -0,0 +1,12 @@
+error[E0515]: cannot return value referencing local variable `count`
+ --> compile_tests/props_safety_temporary_values.rs:8:5
+ |
+8 | / render! {
+9 | | unsafe_child_component {
+10 | | borrowed: &count
+ | | ------ `count` is borrowed here
+11 | | }
+12 | | }
+ | |_____^ returns a value referencing data owned by the current function
+ = note: this error originates in the macro `render` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -79,4 +79,5 @@ pub fn fc_to_builder<'a, T: Properties + 'a>(_: fn(Scope<'a, T>) -> Element<'a>)
fn unsafe_props_fail() {
let t = trybuild::TestCases::new();
t.compile_fail("compile_tests/props_safety.rs");
+ t.compile_fail("compile_tests/props_safety_temporary_values.rs");
}
@@ -424,7 +424,9 @@ impl<'src> ScopeState {
fn_name: &'static str,
) -> DynamicNode<'src>
where
- P: Properties + 'child,
+ // The properties must be valid until the next bump frame
+ P: Properties + 'src,
+ // The current bump allocator frame must outlive the child's borrowed props
'src: 'child,
{
let vcomp = VProps::new(component, P::memoize, props);