Skip to content

Commit

Permalink
Fix macro hygine issues (#2585)
Browse files Browse the repository at this point in the history
* no_implicit_prelude in macro tests

* #[automatically_derived] and #[doc(hidden)]

* Span::call_site() -> Span::mixed_site()
  • Loading branch information
hamza1311 committed Apr 6, 2022
1 parent 8ca7ceb commit 2f3b90c
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 63 deletions.
9 changes: 7 additions & 2 deletions packages/yew-macro/src/derive_props/builder.rs
Expand Up @@ -48,7 +48,7 @@ impl ToTokens for PropsBuilder<'_> {

// Each builder step implements the `BuilderStep` trait and `step_generics` is used to
// enforce that.
let step_generic_param = Ident::new("YEW_PROPS_BUILDER_STEP", Span::call_site());
let step_generic_param = Ident::new("YEW_PROPS_BUILDER_STEP", Span::mixed_site());
let step_generics =
with_param_bounds(generics, step_generic_param.clone(), (*step_trait).clone());

Expand All @@ -62,7 +62,10 @@ impl ToTokens for PropsBuilder<'_> {
#[doc(hidden)]
#vis trait #step_trait {}

#(impl #step_trait for #step_names {})*
#(
#[automatically_derived]
impl #step_trait for #step_names {}
)*

#[doc(hidden)]
#vis struct #builder_name #step_generics
Expand All @@ -74,6 +77,7 @@ impl ToTokens for PropsBuilder<'_> {

#impl_steps

#[automatically_derived]
impl #impl_generics #builder_name<#generic_args> #where_clause {
#[doc(hidden)]
#vis fn build(self) -> #props_name #ty_generics {
Expand Down Expand Up @@ -189,6 +193,7 @@ impl PropsBuilder<'_> {
});

token_stream.extend(quote! {
#[automatically_derived]
#( #extra_attrs )*
impl #impl_generics #builder_name<#current_step_arguments> #where_clause {
#(#optional_prop_fn)*
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/derive_props/field.rs
Expand Up @@ -191,7 +191,7 @@ impl PropField {
Ok(PropAttr::Option)
} else {
let ident = named_field.ident.as_ref().unwrap();
let wrapped_name = format_ident!("{}_wrapper", ident, span = Span::call_site());
let wrapped_name = format_ident!("{}_wrapper", ident, span = Span::mixed_site());
Ok(PropAttr::Required { wrapped_name })
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/derive_props/generics.rs
Expand Up @@ -84,7 +84,7 @@ fn new_param_bounds(param_ident: Ident, param_bounds: Ident) -> GenericParam {
GenericParam::Type(TypeParam {
attrs: Vec::new(),
ident: param_ident,
colon_token: Some(Token![:](Span::call_site())),
colon_token: Some(Token![:](Span::mixed_site())),
bounds: param_bounds,
eq_token: None,
default: None,
Expand Down
6 changes: 3 additions & 3 deletions packages/yew-macro/src/derive_props/mod.rs
Expand Up @@ -80,7 +80,7 @@ impl ToTokens for DerivePropsInput {
} = self;

// The wrapper is a new struct which wraps required props in `Option`
let wrapper_name = format_ident!("{}Wrapper", props_name, span = Span::call_site());
let wrapper_name = format_ident!("{}Wrapper", props_name, span = Span::mixed_site());
let wrapper = PropsWrapper::new(
&wrapper_name,
generics,
Expand All @@ -90,8 +90,8 @@ impl ToTokens for DerivePropsInput {
tokens.extend(wrapper.into_token_stream());

// The builder will only build if all required props have been set
let builder_name = format_ident!("{}Builder", props_name, span = Span::call_site());
let builder_step = format_ident!("{}BuilderStep", props_name, span = Span::call_site());
let builder_name = format_ident!("{}Builder", props_name, span = Span::mixed_site());
let builder_step = format_ident!("{}BuilderStep", props_name, span = Span::mixed_site());
let builder = PropsBuilder::new(
&builder_name,
&builder_step,
Expand Down
2 changes: 2 additions & 0 deletions packages/yew-macro/src/derive_props/wrapper.rs
Expand Up @@ -26,13 +26,15 @@ impl ToTokens for PropsWrapper<'_> {
let wrapper_default_setters = self.default_setters();

let wrapper = quote! {
#[doc(hidden)]
#(#extra_attrs)*
struct #wrapper_name #generics
#where_clause
{
#(#wrapper_field_defs)*
}

#[automatically_derived]
impl #impl_generics ::std::default::Default for #wrapper_name #ty_generics #where_clause {
fn default() -> Self {
#wrapper_name #turbofish_generics {
Expand Down
2 changes: 2 additions & 0 deletions packages/yew-macro/src/hook/mod.rs
Expand Up @@ -162,6 +162,7 @@ pub fn hook_impl(hook: HookFn) -> syn::Result<TokenStream> {
#args_ident: (#(#input_types,)*),
}

#[automatically_derived]
impl #impl_generics ::yew::functional::Hook for #hook_struct_name #ty_generics #where_clause {
type Output = #output_type;

Expand All @@ -172,6 +173,7 @@ pub fn hook_impl(hook: HookFn) -> syn::Result<TokenStream> {
}
}

#[automatically_derived]
impl #impl_generics #hook_struct_name #ty_generics #where_clause {
fn new(#inputs) -> Self {
#hook_struct_name {
Expand Down
8 changes: 4 additions & 4 deletions packages/yew-macro/src/html_tree/html_component.rs
Expand Up @@ -166,17 +166,17 @@ impl HtmlComponent {
if punct.as_char() == '>' {
break;
} else if punct.as_char() == ',' {
args.push_punct(Token![,](Span::call_site()))
args.push_punct(Token![,](Span::mixed_site()))
}
}
}

Some((
PathArguments::AngleBracketed(AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Token![<](Span::call_site()),
lt_token: Token![<](Span::mixed_site()),
args,
gt_token: Token![>](Span::call_site()),
gt_token: Token![>](Span::mixed_site()),
}),
cursor,
))
Expand All @@ -191,7 +191,7 @@ impl HtmlComponent {
let mut post_colons_cursor = cursor;
if let Some(c) = Self::double_colon(post_colons_cursor) {
if colons_optional {
leading_colon = Some(Token![::](Span::call_site()));
leading_colon = Some(Token![::](Span::mixed_site()));
}
post_colons_cursor = c;
} else if !colons_optional {
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/html_tree/html_dashed_name.rs
Expand Up @@ -60,7 +60,7 @@ impl Peek<'_, Self> for HtmlDashedName {
if punct.as_char() == '-' {
let (ident, i_cursor) = p_cursor.ident()?;
cursor = i_cursor;
extended.push((Token![-](Span::call_site()), ident));
extended.push((Token![-](Span::mixed_site()), ident));
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/html_tree/mod.rs
Expand Up @@ -243,7 +243,7 @@ impl HtmlChildrenTree {
};
}

let vec_ident = Ident::new("__yew_v", Span::call_site());
let vec_ident = Ident::new("__yew_v", Span::mixed_site());
let add_children_streams = children.iter().map(|child| {
if let Some(node_iterator_stream) = child.to_node_iterator_stream() {
quote! {
Expand Down
@@ -1,21 +1,21 @@
use yew::prelude::*;
#![no_implicit_prelude]

#[derive(Clone, Properties, PartialEq)]
#[derive(::yew::prelude::Properties, ::std::prelude::rust_2021::PartialEq,)]
struct Props {
a: usize,
}

#[function_component]
fn Comp(props: &Props) -> Html {
html! {
#[::yew::prelude::function_component]
fn Comp(props: &Props) -> ::yew::prelude::Html {
::yew::prelude::html! {
<p>
{ props.a }
</p>
}
}

fn main() {
let _ = html! {
let _ = ::yew::prelude::html! {
<Comp a={0} />
};
}
@@ -1,25 +1,25 @@
use yew::prelude::*;
#![no_implicit_prelude]

#[derive(Properties, Debug)]
#[derive(::yew::prelude::Properties, ::std::fmt::Debug)]
pub struct CompProps<A> {
#[prop_or_default]
_phantom: std::marker::PhantomData<A>,
_phantom: ::std::marker::PhantomData<A>,
}

impl<A> PartialEq for CompProps<A> {
impl<A> ::std::cmp::PartialEq for CompProps<A> {
fn eq(&self, _rhs: &Self) -> bool {
true
}
}

#[function_component(Comp)]
pub fn comp<A = ()>(_props: &CompProps<A>) -> Html {
todo!()
#[::yew::prelude::function_component(Comp)]
pub fn comp<A = ()>(_props: &CompProps<A>) -> ::yew::prelude::Html {
::std::todo!()
}

#[function_component(App)]
pub fn app() -> Html {
html! { <Comp /> } // No generics here.
#[::yew::prelude::function_component(App)]
pub fn app() -> ::yew::prelude::Html {
::yew::prelude::html! { <Comp /> } // No generics here.
}

fn main() {}
4 changes: 2 additions & 2 deletions packages/yew-macro/tests/hook_attr/hook-lifetime-pass.rs
@@ -1,6 +1,6 @@
use yew::prelude::*;
#![no_implicit_prelude]

#[hook]
#[::yew::functional::hook]
fn use_as_is<'a>(input: &'a ()) -> &'a () {
input
}
Expand Down
15 changes: 8 additions & 7 deletions packages/yew-macro/tests/hook_attr/hook_macro-pass.rs
@@ -1,8 +1,9 @@
use yew::prelude::*;
#![no_implicit_prelude]

#[hook]
pub fn use_some_macro_inner(val: &str) -> String {
use_state(|| val.to_owned()).to_string()
#[::yew::functional::hook]
pub fn use_some_macro_inner(val: &str) -> ::std::string::String {
let state = ::yew::functional::use_state(|| ::std::borrow::ToOwned::to_owned(val));
::std::string::ToString::to_string(&*state)
}

macro_rules! use_some_macro {
Expand All @@ -14,12 +15,12 @@ macro_rules! use_some_macro {
};
}

#[function_component]
fn Comp() -> Html {
#[::yew::functional::function_component]
fn Comp() -> ::yew::Html {
let a = use_some_macro!();
let b = use_some_macro!("b");

html! {
::yew::html! {
<div>{a}{b}</div>
}
}
Expand Down
50 changes: 25 additions & 25 deletions packages/yew-macro/tests/html_macro/html-if-pass.rs
@@ -1,35 +1,35 @@
use yew::prelude::*;
#![no_implicit_prelude]

fn compile_pass_lit() {
html! { if true {} };
html! { if true { <div/> } };
html! { if true { <div/><div/> } };
html! { if true { <><div/><div/></> } };
html! { if true { { html! {} } } };
html! { if true { { { let _x = 42; html! {} } } } };
html! { if true {} else {} };
html! { if true {} else if true {} };
html! { if true {} else if true {} else {} };
html! { if let Some(text) = Some("text") { <span>{ text }</span> } };
html! { <><div/>if true {}<div/></> };
html! { <div>if true {}</div> };
::yew::html! { if true {} };
::yew::html! { if true { <div/> } };
::yew::html! { if true { <div/><div/> } };
::yew::html! { if true { <><div/><div/></> } };
::yew::html! { if true { { ::yew::html! {} } } };
::yew::html! { if true { { { let _x = 42; ::yew::html! {} } } } };
::yew::html! { if true {} else {} };
::yew::html! { if true {} else if true {} };
::yew::html! { if true {} else if true {} else {} };
::yew::html! { if let ::std::option::Option::Some(text) = ::std::option::Option::Some("text") { <span>{ text }</span> } };
::yew::html! { <><div/>if true {}<div/></> };
::yew::html! { <div>if true {}</div> };
}

fn compile_pass_expr() {
let condition = true;

html! { if condition {} };
html! { if condition { <div/> } };
html! { if condition { <div/><div/> } };
html! { if condition { <><div/><div/></> } };
html! { if condition { { html! {} } } };
html! { if condition { { { let _x = 42; html! {} } } } };
html! { if condition {} else {} };
html! { if condition {} else if condition {} };
html! { if condition {} else if condition {} else {} };
html! { if let Some(text) = Some("text") { <span>{ text }</span> } };
html! { <><div/>if condition {}<div/></> };
html! { <div>if condition {}</div> };
::yew::html! { if condition {} };
::yew::html! { if condition { <div/> } };
::yew::html! { if condition { <div/><div/> } };
::yew::html! { if condition { <><div/><div/></> } };
::yew::html! { if condition { { ::yew::html! {} } } };
::yew::html! { if condition { { { let _x = 42; ::yew::html! {} } } } };
::yew::html! { if condition {} else {} };
::yew::html! { if condition {} else if condition {} };
::yew::html! { if condition {} else if condition {} else {} };
::yew::html! { if let ::std::option::Option::Some(text) = ::std::option::Option::Some("text") { <span>{ text }</span> } };
::yew::html! { <><div/>if condition {}<div/></> };
::yew::html! { <div>if condition {}</div> };
}

fn main() {}

1 comment on commit 2f3b90c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: 2f3b90c Previous: 8ca7ceb Ratio
yew-struct-keyed 01_run1k 188.318 208.3555 0.90
yew-struct-keyed 02_replace1k 193.585 225.2485 0.86
yew-struct-keyed 03_update10th1k_x16 397.521 398.384 1.00
yew-struct-keyed 04_select1k 88.763 75.3695 1.18
yew-struct-keyed 05_swap1k 102.404 102.787 1.00
yew-struct-keyed 06_remove-one-1k 30.794 34.772499999999994 0.89
yew-struct-keyed 07_create10k 2496.348 2793.7484999999997 0.89
yew-struct-keyed 08_create1k-after1k_x2 418.7985 473.5455 0.88
yew-struct-keyed 09_clear1k_x8 199.747 221.926 0.90
yew-struct-keyed 21_ready-memory 1.4005584716796875 1.4005584716796875 1
yew-struct-keyed 22_run-memory 1.6621437072753906 1.66949462890625 1.00
yew-struct-keyed 23_update5-memory 1.698455810546875 1.7012710571289062 1.00
yew-struct-keyed 24_run5-memory 1.949310302734375 1.714313507080078 1.14
yew-struct-keyed 25_run-clear-memory 1.421672821044922 1.421672821044922 1
yew-struct-keyed 31_startup-ci 1731.35 1731.306 1.00
yew-struct-keyed 32_startup-bt 28.525999999999996 35.414 0.81
yew-struct-keyed 34_startup-totalbytes 330.5498046875 330.5498046875 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.