Skip to content

Commit

Permalink
Merge pull request #1345 from Demonthos/fix-borrowed-props
Browse files Browse the repository at this point in the history
Constrain Props lifetime to parent, not child scope lifetime
  • Loading branch information
jkelleyrtp authored Aug 22, 2023
2 parents 882c3c9 + b2f9430 commit a38860e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/core/compile_tests/props_safety_temporary_values.rs
Original file line number Diff line number Diff line change
@@ -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:?}" }
})
}
12 changes: 12 additions & 0 deletions packages/core/compile_tests/props_safety_temporary_values.stderr
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions packages/core/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
4 changes: 3 additions & 1 deletion packages/core/src/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a38860e

Please sign in to comment.