From 45682ccf36f4df3bed3e71b1a1c076bb1d85f50c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 9 Nov 2023 15:16:43 +0100 Subject: [PATCH] Ensure cloned types impl ImplicitClone --- Cargo.lock | 5 +- Cargo.toml | 7 ++- src/autoprops.rs | 4 +- .../function_component_attr/autoprops-fail.rs | 3 + .../autoprops-fail.stderr | 56 +++++++++---------- .../function_component_attr/autoprops-pass.rs | 2 +- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index edf412f..92aa796 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -600,9 +600,9 @@ dependencies = [ [[package]] name = "implicit-clone" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "789a53d49d2276908b1fcccb9dfcf65c7a6e884c1df639f26aeff4f142b3ef9e" +checksum = "7a50fed6f6aa778e7946f697a88e474b4b4a83fb0849cec1ac65fa8e869319dd" dependencies = [ "indexmap", ] @@ -1201,6 +1201,7 @@ dependencies = [ name = "yew-autoprops" version = "0.3.0" dependencies = [ + "implicit-clone", "proc-macro2", "quote", "rustversion", diff --git a/Cargo.toml b/Cargo.toml index 7a1d4e6..5140745 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ [dev-dependencies] - yew = { version = "0.21" } - trybuild = "1" - rustversion = "1" + yew = { version = "0.21" } + implicit-clone = "0.4.6" + trybuild = "1" + rustversion = "1" diff --git a/src/autoprops.rs b/src/autoprops.rs index dd0a1d5..c206c0f 100644 --- a/src/autoprops.rs +++ b/src/autoprops.rs @@ -141,7 +141,9 @@ impl Autoprops { syn::FnArg::Typed(syn::PatType { pat, ty, .. }) if !matches!(**ty, syn::Type::Reference(_)) => { - Some(quote! { let #pat = ::std::clone::Clone::clone(#pat); }) + Some(quote! { + let #pat = ::implicit_clone::ImplicitClone::implicit_clone(#pat); + }) } _ => None, }) diff --git a/tests/function_component_attr/autoprops-fail.rs b/tests/function_component_attr/autoprops-fail.rs index 4e90f63..f8d98ad 100644 --- a/tests/function_component_attr/autoprops-fail.rs +++ b/tests/function_component_attr/autoprops-fail.rs @@ -1,4 +1,5 @@ use yew::prelude::*; +use yew::html::ImplicitClone; use yew_autoprops::*; #[autoprops] @@ -47,6 +48,8 @@ fn TypeIsNotClone(stuff: NotClonable) -> Html { #[derive(Clone)] struct NotPartialEq(u32); +impl ImplicitClone for NotPartialEq {} + #[autoprops] #[function_component] fn TypeIsNotPartialEq(stuff: NotPartialEq) -> Html { diff --git a/tests/function_component_attr/autoprops-fail.stderr b/tests/function_component_attr/autoprops-fail.stderr index d87685c..e8394c1 100644 --- a/tests/function_component_attr/autoprops-fail.stderr +++ b/tests/function_component_attr/autoprops-fail.stderr @@ -1,72 +1,72 @@ error: function components can't accept a receiver - --> tests/function_component_attr/autoprops-fail.rs:6:23 + --> tests/function_component_attr/autoprops-fail.rs:7:23 | -6 | fn CantAcceptReceiver(&self, b: bool) -> Html { +7 | fn CantAcceptReceiver(&self, b: bool) -> Html { | ^^^^^ error: could not find #[function_component] attribute in function declaration (#[autoprops] must be placed *before* #[function_component]) - --> tests/function_component_attr/autoprops-fail.rs:13:1 + --> tests/function_component_attr/autoprops-fail.rs:14:1 | -13 | fn not_a_function_component(b: bool) -> Html { +14 | fn not_a_function_component(b: bool) -> Html { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: expected a reference to a `Properties` type (try: `&bool`) - --> tests/function_component_attr/autoprops-fail.rs:21:25 + --> tests/function_component_attr/autoprops-fail.rs:22:25 | -21 | fn wrong_attrs_order(b: bool) -> Html { +22 | fn wrong_attrs_order(b: bool) -> Html { | ^^^^ error: expected identifier, found keyword `let` - --> tests/function_component_attr/autoprops-fail.rs:28:22 + --> tests/function_component_attr/autoprops-fail.rs:29:22 | -28 | #[function_component(let)] +29 | #[function_component(let)] | ^^^ error: cannot use `_` as field name - --> tests/function_component_attr/autoprops-fail.rs:61:21 + --> tests/function_component_attr/autoprops-fail.rs:64:21 | -61 | fn InvalidFieldName(_: u32) -> Html { +64 | fn InvalidFieldName(_: u32) -> Html { | ^ error[E0412]: cannot find type `CompPrivateTest` in this scope - --> tests/function_component_attr/autoprops-fail.rs:83:22 + --> tests/function_component_attr/autoprops-fail.rs:86:22 | -83 | let _ = html! { }; +86 | let _ = html! { }; | ^^^^^^^^^^^^^^^ not found in this scope | note: struct `crate::private_module::CompPrivateTest` exists but is inaccessible - --> tests/function_component_attr/autoprops-fail.rs:69:5 + --> tests/function_component_attr/autoprops-fail.rs:72:5 | -69 | #[::yew::function_component(CompPrivateTest)] +72 | #[::yew::function_component(CompPrivateTest)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not accessible = note: this error originates in the attribute macro `::yew::function_component` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `NotClonable: Clone` is not satisfied - --> tests/function_component_attr/autoprops-fail.rs:40:19 +error[E0277]: the trait bound `NotClonable: ImplicitClone` is not satisfied + --> tests/function_component_attr/autoprops-fail.rs:41:19 | -38 | #[autoprops] +39 | #[autoprops] | ------------ required by a bound introduced by this call -39 | #[function_component] -40 | fn TypeIsNotClone(stuff: NotClonable) -> Html { - | ^^^^^ the trait `Clone` is not implemented for `NotClonable` +40 | #[function_component] +41 | fn TypeIsNotClone(stuff: NotClonable) -> Html { + | ^^^^^ expected an implementor of trait `ImplicitClone` | -help: consider annotating `NotClonable` with `#[derive(Clone)]` - | -36 | #[derive(Clone)] +help: consider borrowing here | +41 | fn TypeIsNotClone(&stuff: NotClonable) -> Html { + | + error[E0369]: binary operation `==` cannot be applied to type `NotPartialEq` - --> tests/function_component_attr/autoprops-fail.rs:52:30 + --> tests/function_component_attr/autoprops-fail.rs:55:30 | -52 | fn TypeIsNotPartialEq(stuff: NotPartialEq) -> Html { +55 | fn TypeIsNotPartialEq(stuff: NotPartialEq) -> Html { | ^^^^^^^^^^^^ | note: an implementation of `PartialEq<_>` might be missing for `NotPartialEq` - --> tests/function_component_attr/autoprops-fail.rs:48:1 + --> tests/function_component_attr/autoprops-fail.rs:49:1 | -48 | struct NotPartialEq(u32); +49 | struct NotPartialEq(u32); | ^^^^^^^^^^^^^^^^^^^ must implement `PartialEq<_>` help: consider annotating `NotPartialEq` with `#[derive(PartialEq)]` | -48 | #[derive(PartialEq)] +49 | #[derive(PartialEq)] | diff --git a/tests/function_component_attr/autoprops-pass.rs b/tests/function_component_attr/autoprops-pass.rs index 7715a4f..45ca1d9 100644 --- a/tests/function_component_attr/autoprops-pass.rs +++ b/tests/function_component_attr/autoprops-pass.rs @@ -77,7 +77,7 @@ fn comp_single_generic() -> ::yew::Html { #[::yew::function_component(CompGenerics)] fn comp_generics(b: T1, a: &T2) -> ::yew::Html where - T1: ::std::cmp::PartialEq + ::std::clone::Clone, + T1: ::std::cmp::PartialEq + ::implicit_clone::ImplicitClone, T2: ::std::cmp::PartialEq, { let _: T1 = b;