From d559fdaeab7a3f69541aae0c61e6d6672f348e9e Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sat, 12 Aug 2023 19:19:46 -0500 Subject: [PATCH] fix borrowed props with temp values --- .../props_safety_temporary_values.rs | 24 +++++++++++++++++++ .../props_safety_temporary_values.stderr | 12 ++++++++++ packages/core/src/properties.rs | 1 + packages/core/src/scopes.rs | 4 +++- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 packages/core/compile_tests/props_safety_temporary_values.rs create mode 100644 packages/core/compile_tests/props_safety_temporary_values.stderr diff --git a/packages/core/compile_tests/props_safety_temporary_values.rs b/packages/core/compile_tests/props_safety_temporary_values.rs new file mode 100644 index 000000000..c332f63c4 --- /dev/null +++ b/packages/core/compile_tests/props_safety_temporary_values.rs @@ -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, +} + +fn unsafe_child_component<'a>(cx: Scope<'a, Testing<'a>>) -> Element<'a> { + cx.render(rsx! { + div { "{cx.props.borrowed:?}" } + }) +} diff --git a/packages/core/compile_tests/props_safety_temporary_values.stderr b/packages/core/compile_tests/props_safety_temporary_values.stderr new file mode 100644 index 000000000..c1ef443c9 --- /dev/null +++ b/packages/core/compile_tests/props_safety_temporary_values.stderr @@ -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) diff --git a/packages/core/src/properties.rs b/packages/core/src/properties.rs index f754f8b68..3f0d9ef97 100644 --- a/packages/core/src/properties.rs +++ b/packages/core/src/properties.rs @@ -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"); } diff --git a/packages/core/src/scopes.rs b/packages/core/src/scopes.rs index 5ada89069..17e499d62 100644 --- a/packages/core/src/scopes.rs +++ b/packages/core/src/scopes.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);