Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constrain Props lifetime to parent, not child scope lifetime #1345

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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