Skip to content

Commit

Permalink
Add fully qualified primitives in proc macro (#2037)
Browse files Browse the repository at this point in the history
* Add primitive shadowing in macro tests

* Fix primitive types to be fully qualified
  • Loading branch information
mc1098 authored Sep 7, 2021
1 parent 1ab7779 commit 4c6da9a
Show file tree
Hide file tree
Showing 19 changed files with 650 additions and 38 deletions.
10 changes: 5 additions & 5 deletions packages/yew-macro/src/html_tree/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ impl ToTokens for HtmlElement {
return None;
}),
_ => Value::Dynamic(quote_spanned! {value.span()=> {
::yew::utils::__ensure_type::<bool>(#value);
::yew::utils::__ensure_type::<::std::primitive::bool>(#value);
#key
}}),
},
expr => Value::Dynamic(quote_spanned! {expr.span()=>
if #expr {
::std::option::Option::Some(
::std::borrow::Cow::<'static, str>::Borrowed(#key)
::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#key)
)
} else {
::std::option::Option::None
Expand Down Expand Up @@ -333,7 +333,7 @@ impl ToTokens for HtmlElement {
#[allow(clippy::redundant_clone, unused_braces)]
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_other(
::std::borrow::Cow::<'static, str>::Borrowed(#name),
::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#name),
#node_ref,
#key,
#attributes,
Expand Down Expand Up @@ -363,7 +363,7 @@ impl ToTokens for HtmlElement {
// doesn't return a valid value
quote_spanned! {expr.span()=> {
let mut #vtag_name = ::std::convert::Into::<
::std::borrow::Cow::<'static, str>
::std::borrow::Cow::<'static, ::std::primitive::str>
>::into(#expr);
if !#vtag_name.is_ascii() {
::std::panic!(
Expand All @@ -375,7 +375,7 @@ impl ToTokens for HtmlElement {
#vtag_name.to_mut().make_ascii_lowercase();

#[allow(clippy::redundant_clone, unused_braces, clippy::let_and_return)]
let mut #vtag = match ::std::convert::AsRef::<str>::as_ref(&#vtag_name) {
let mut #vtag = match ::std::convert::AsRef::<::std::primitive::str>::as_ref(&#vtag_name) {
"input" => {
::yew::virtual_dom::VTag::__new_textarea(
#value,
Expand Down
4 changes: 2 additions & 2 deletions packages/yew-macro/src/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::{Expr, Lit, LitStr};
/// Stringify a value at runtime.
fn stringify_at_runtime(src: impl ToTokens) -> TokenStream {
quote_spanned! {src.span()=>
::std::convert::Into::<::std::borrow::Cow::<'static, str>>::into(#src)
::std::convert::Into::<::std::borrow::Cow::<'static, ::std::primitive::str>>::into(#src)
}
}

Expand Down Expand Up @@ -71,7 +71,7 @@ impl Stringify for LitStr {

fn stringify(&self) -> TokenStream {
quote_spanned! {self.span()=>
::std::borrow::Cow::<'static, str>::Borrowed(#self)
::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#self)
}
}
}
Expand Down
38 changes: 37 additions & 1 deletion packages/yew-macro/tests/classes_macro/classes-pass.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

fn compile_pass() {
// multiple literals
::yew::classes!("one", "two");
Expand All @@ -16,7 +52,7 @@ fn compile_pass() {
// optional classes
::yew::classes!(
::std::option::Option::Some("one"),
::std::option::Option::None::<&'static str>,
::std::option::Option::None::<&'static ::std::primitive::str>,
);

// mixed types
Expand Down
58 changes: 47 additions & 11 deletions packages/yew-macro/tests/derive_props/pass.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
#![no_implicit_prelude]
#![recursion_limit = "128"]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

mod t1 {
#[derive(::std::clone::Clone, ::yew::Properties, ::std::cmp::PartialEq)]
pub struct Props<T: ::std::clone::Clone + ::std::default::Default + ::std::cmp::PartialEq> {
Expand All @@ -11,8 +47,8 @@ mod t1 {
fn optional_prop_generics_should_work() {
use ::yew::Properties;

Props::<bool>::builder().build();
Props::<bool>::builder().value(true).build();
Props::<::std::primitive::bool>::builder().build();
Props::<::std::primitive::bool>::builder().value(true).build();
}
}

Expand All @@ -34,9 +70,9 @@ mod t2 {
mod t3 {
#[derive(::std::clone::Clone, ::yew::Properties, ::std::cmp::PartialEq)]
pub struct Props {
b: i32,
b: ::std::primitive::i32,
#[prop_or_default]
a: i32,
a: ::std::primitive::i32,
}

fn order_is_alphabetized() {
Expand All @@ -60,8 +96,8 @@ mod t4 {
fn optional_prop_generics_should_work() {
use ::yew::Properties;

Props::<bool>::builder().build();
Props::<bool>::builder().value(true).build();
Props::<::std::primitive::bool>::builder().build();
Props::<::std::primitive::bool>::builder().value(true).build();
}
}

Expand All @@ -72,7 +108,7 @@ mod t5 {
T: ::std::clone::Clone + ::std::default::Default + ::std::cmp::PartialEq + 'a,
> {
#[prop_or_default]
static_value: &'static str,
static_value: &'static ::std::primitive::str,
value: &'a T,
}

Expand Down Expand Up @@ -134,7 +170,7 @@ mod t8 {
#[derive(::std::clone::Clone, ::yew::Properties, ::std::cmp::PartialEq)]
pub struct Props {
#[prop_or_else(|| 123)]
value: i32,
value: ::std::primitive::i32,
}

fn prop_or_else_closure_should_work() {
Expand Down Expand Up @@ -169,7 +205,7 @@ mod t9 {
use ::std::{assert_eq, result::Result::Ok};
use ::yew::Properties;

let props = Props::<i32>::builder().build();
let props = Props::<::std::primitive::i32>::builder().build();
assert_eq!(props.value, Ok(123));
Props::<i32>::builder().value(Ok(456)).build();
}
Expand Down Expand Up @@ -210,8 +246,8 @@ mod t12 {
fn optional_prop_generics_should_work() {
use ::yew::Properties;

Props::<bool>::builder().build();
Props::<bool>::builder().value(true).build();
Props::<::std::primitive::bool>::builder().build();
Props::<::std::primitive::bool>::builder().value(true).build();
}
}

Expand Down
42 changes: 39 additions & 3 deletions packages/yew-macro/tests/function_component_attr/generic-pass.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

#[derive(::std::clone::Clone, ::yew::Properties, ::std::cmp::PartialEq)]
struct Props {
a: usize,
a: ::std::primitive::usize,
}

#[::yew::function_component(Comp)]
Expand All @@ -23,7 +59,7 @@ fn comp1<T1, T2>(_props: &()) -> ::yew::Html {
}

#[::yew::function_component(ConstGenerics)]
fn const_generics<const N: i32>() -> ::yew::Html {
fn const_generics<const N: ::std::primitive::i32>() -> ::yew::Html {
::yew::html! {
<div>
{ N }
Expand All @@ -33,7 +69,7 @@ fn const_generics<const N: i32>() -> ::yew::Html {

fn compile_pass() {
::yew::html! { <Comp<Props> a=10 /> };
::yew::html! { <Comp1<usize, usize> /> };
::yew::html! { <Comp1<::std::primitive::usize, ::std::primitive::usize> /> };

::yew::html! { <ConstGenerics<10> /> };
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

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

#[::yew::function_component(Comp)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

#[::yew::function_component(Comp)]
fn comp() -> ::yew::Html {
::yew::html! {
Expand Down
Loading

0 comments on commit 4c6da9a

Please sign in to comment.