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

Wrong borrowing of expression in template! #262

Closed
emi2k01 opened this issue Sep 23, 2021 · 2 comments · Fixed by #304
Closed

Wrong borrowing of expression in template! #262

emi2k01 opened this issue Sep 23, 2021 · 2 comments · Fixed by #304
Labels
A-macro Area: macros A-view Area: view! macro C-bug Category: bug, something isn't working good first issue Good for newcomers

Comments

@emi2k01
Copy link
Contributor

emi2k01 commented Sep 23, 2021

Describe the bug
Given an expression with multiple rvalues a, b, c, the macro will expand to &a....c instead of &(a....c)

To Reproduce
Steps to reproduce the behavior:

use sycamore::prelude::*;

#[component(App<G>)]
fn app() -> Template<G> {
    template! {
        div {
            (0+1)
        }
    }
}

fn main() {
    sycamore::render(|| template! { App() });
}

The macro will expand 0+1 to IntoTemplate::create(&0+1) instead of IntoTemplate::create(&(0+1)).

Replacing 0+1 with (0+1) so that the end result looks like ((0+1)) makes it work.

Expected behavior
Borrow the whole expression. Not only the first rvalue.

I think the macro should always wrap the expansion of the inner expression in parenthesis.

Environment

  • Sycamore: master
@emi2k01 emi2k01 added the C-bug Category: bug, something isn't working label Sep 23, 2021
@lukechu10 lukechu10 added A-macro Area: macros good first issue Good for newcomers A-view Area: view! macro difficulty: trivial labels Sep 24, 2021
@emi2k01
Copy link
Contributor Author

emi2k01 commented Oct 4, 2021

I forgot to write an update on this issue.

After I submitted the issue, I planned to write a MR but, while reading the sycamore-macro, I found a lot of code that used &__el or similar. This can be found in the code for the attributes parser, the element parser, and probably other parts of the code.

I don't know if all instances of this should be wrapped in parenthesis.

Look at this:

> grep "(&__el|&#)" -ER packages/sycamore-macro/
packages/sycamore-macro/src/template/mod.rs:                    ::sycamore::template::IntoTemplate::create(&#splice)
packages/sycamore-macro/src/template/element.rs:                        ::sycamore::generic_node::GenericNode::append_child(&__el, &#element);
packages/sycamore-macro/src/template/element.rs:                            &__el,
packages/sycamore-macro/src/template/element.rs:                                &__el,
packages/sycamore-macro/src/template/element.rs:                                ::sycamore::template::IntoTemplate::create(&#splice),
packages/sycamore-macro/src/template/element.rs:                                ::sycamore::generic_node::GenericNode::append_child(&__el, &__marker);
packages/sycamore-macro/src/template/element.rs:                                ::sycamore::generic_node::GenericNode::append_child(&__el, &__marker);
packages/sycamore-macro/src/template/element.rs:                                ::sycamore::generic_node::GenericNode::append_child(&__el, &__marker);
packages/sycamore-macro/src/template/element.rs:                                    &__el,
packages/sycamore-macro/src/template/element.rs:                                   &__el,
packages/sycamore-macro/src/template/element.rs:                                       ::sycamore::template::IntoTemplate::create(&#splice)
packages/sycamore-macro/src/template/attributes.rs:                        &::std::string::ToString::to_string(&#expr)
packages/sycamore-macro/src/template/attributes.rs:                        ::sycamore::generic_node::GenericNode::set_class_name(&__el, #quoted_text);
packages/sycamore-macro/src/template/attributes.rs:                            &__el,
packages/sycamore-macro/src/template/attributes.rs:                            let __el = ::std::clone::Clone::clone(&__el);
packages/sycamore-macro/src/template/attributes.rs:                        ::sycamore::generic_node::GenericNode::set_attribute(&__el, #name, "");
packages/sycamore-macro/src/template/attributes.rs:                        ::sycamore::generic_node::GenericNode::remove_attribute(&__el, #name);
packages/sycamore-macro/src/template/attributes.rs:                            let __el = ::std::clone::Clone::clone(&__el);
packages/sycamore-macro/src/template/attributes.rs:                            let __el = ::std::clone::Clone::clone(&__el);
packages/sycamore-macro/src/template/attributes.rs:                                    &__el,
packages/sycamore-macro/src/template/attributes.rs:                            &__el,
packages/sycamore-macro/src/template/attributes.rs:                        &__el,
packages/sycamore-macro/src/template/attributes.rs:                        ::sycamore::rt::JsValue::as_bool(&#event_target_prop).unwrap()
packages/sycamore-macro/src/template/attributes.rs:                        ::sycamore::rt::JsValue::as_string(&#event_target_prop).unwrap()
packages/sycamore-macro/src/template/attributes.rs:                        let __el = ::std::clone::Clone::clone(&__el);
packages/sycamore-macro/src/template/attributes.rs:                                &__el,
packages/sycamore-macro/src/template/attributes.rs:                                &#convert_into_jsvalue_fn,
packages/sycamore-macro/src/template/attributes.rs:                        &__el,
packages/sycamore-macro/src/template/attributes.rs:                        &#expr,
packages/sycamore-macro/src/template/attributes.rs:                        ::std::clone::Clone::clone(&__el),

I don't know if there are more of these.

@lukechu10
Copy link
Member

These don't need to be parenthesized because it references a variable. &__el is expanded into &__el so there can be no ambiguities about that there. The only place that needs to be changed is when an expression is expanded so when there is something like &#expr which should become something like &(#expr). In fact, I am somewhat surprised that quote! doesn't automatically handle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro Area: macros A-view Area: view! macro C-bug Category: bug, something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants