-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic functional components (#1756)
* Generic functional components * Add some tests * Clippy * Fix generic-props-fail snapshot * Add docs for generic function components * Add some more docs * Fix trait bounds in docs * Fix docs * Improve lifetime error messages * Fix parsing for const generics * Use fully qualified path for pass test * Remove TODO message * Suggested change Co-authored-by: Simon <[email protected]> * Update test Co-authored-by: Simon <[email protected]> * Update test Co-authored-by: Simon <[email protected]> * Update stderr snapshots * Combine quote implementations * Fix warning about type alias bounds Co-authored-by: Simon <[email protected]>
- Loading branch information
Showing
8 changed files
with
220 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
packages/yew-functional-macro/tests/function_attr/generic-fail.stderr
This file was deleted.
Oops, something went wrong.
37 changes: 19 additions & 18 deletions
37
...macro/tests/function_attr/generic-fail.rs → ...ts/function_attr/generic-lifetime-fail.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
use yew::prelude::*; | ||
use yew_functional::function_component; | ||
|
||
#[derive(Clone, Properties, PartialEq)] | ||
struct Props { | ||
a: usize, | ||
} | ||
|
||
#[function_component(Comp)] | ||
const fn comp<P: Properties>(props: &P) -> Html { | ||
html! { | ||
<p> | ||
{ props.a } | ||
</p> | ||
} | ||
} | ||
|
||
fn main() {} | ||
use yew::prelude::*; | ||
use yew_functional::function_component; | ||
|
||
#[derive(Clone, Properties, PartialEq)] | ||
struct Props { | ||
a: usize, | ||
} | ||
|
||
#[function_component(Comp)] | ||
fn comp<'a>(props: &'a Props) -> Html { | ||
|
||
html! { | ||
<p> | ||
{ props.a } | ||
</p> | ||
} | ||
} | ||
|
||
fn main() {} |
5 changes: 5 additions & 0 deletions
5
packages/yew-functional-macro/tests/function_attr/generic-lifetime-fail.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: function components can't have generic lifetime parameters | ||
--> $DIR/generic-lifetime-fail.rs:10:8 | ||
| | ||
10 | fn comp<'a>(props: &'a Props) -> Html { | ||
| ^^^^ |
40 changes: 40 additions & 0 deletions
40
packages/yew-functional-macro/tests/function_attr/generic-pass.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#[derive(Clone, ::yew::Properties, PartialEq)] | ||
struct Props { | ||
a: usize, | ||
} | ||
|
||
#[::yew_functional::function_component(Comp)] | ||
fn comp<P>(_props: &P) -> ::yew::Html | ||
where | ||
P: ::yew::Properties + PartialEq, | ||
{ | ||
::yew::html! { | ||
<p></p> | ||
} | ||
} | ||
|
||
#[::yew_functional::function_component(Comp1)] | ||
fn comp1<T1, T2>(_props: &()) -> ::yew::Html { | ||
::yew::html! { | ||
<p></p> | ||
} | ||
} | ||
|
||
// TODO: uncomment when min_const_generics are in stable and Rust version in CI is bumped | ||
// #[::yew_functional::function_component(ConstGenerics)] | ||
// fn const_generics<const N: i32>() -> ::yew::Html { | ||
// ::yew::html! { | ||
// <div> | ||
// { N } | ||
// </div> | ||
// } | ||
// } | ||
|
||
fn compile_pass() { | ||
::yew::html! { <Comp<Props> a=10 /> }; | ||
::yew::html! { <Comp1<usize, usize> /> }; | ||
|
||
// ::yew::html! { <ConstGenerics<10> }; | ||
} | ||
|
||
fn main() {} |
34 changes: 34 additions & 0 deletions
34
packages/yew-functional-macro/tests/function_attr/generic-props-fail.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use yew::prelude::*; | ||
use yew_functional::function_component; | ||
|
||
#[derive(Clone, Properties, PartialEq)] | ||
struct Props { | ||
a: usize, | ||
} | ||
|
||
#[function_component(Comp)] | ||
fn comp<P>(_props: &P) -> Html | ||
where | ||
P: Properties + PartialEq, | ||
{ | ||
html! { | ||
<p></p> | ||
} | ||
} | ||
|
||
struct MissingTypeBounds; | ||
|
||
fn compile_fail() { | ||
// missing prop 'a' | ||
html! { <Comp<Props> /> }; | ||
|
||
// invalid type parameter | ||
html! { <Comp<INVALID> /> }; | ||
// parameter doesn't match bounds | ||
html! { <Comp<MissingTypeBounds> /> }; | ||
|
||
// missing type param | ||
html! { <Comp /> }; | ||
} | ||
|
||
fn main() {} |
58 changes: 58 additions & 0 deletions
58
packages/yew-functional-macro/tests/function_attr/generic-props-fail.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
error[E0412]: cannot find type `INVALID` in this scope | ||
--> $DIR/generic-props-fail.rs:26:19 | ||
| | ||
21 | fn compile_fail() { | ||
| - help: you might be missing a type parameter: `<INVALID>` | ||
... | ||
26 | html! { <Comp<INVALID> /> }; | ||
| ^^^^^^^ not found in this scope | ||
|
||
error[E0599]: no method named `build` found for struct `PropsBuilder<PropsBuilderStep_missing_required_prop_a>` in the current scope | ||
--> $DIR/generic-props-fail.rs:23:14 | ||
| | ||
4 | #[derive(Clone, Properties, PartialEq)] | ||
| ---------- method `build` not found for this | ||
... | ||
23 | html! { <Comp<Props> /> }; | ||
| ^^^^ method not found in `PropsBuilder<PropsBuilderStep_missing_required_prop_a>` | ||
| | ||
= help: items from traits can only be used if the trait is implemented and in scope | ||
= note: the following trait defines an item `build`, perhaps you need to implement it: | ||
candidate #1: `proc_macro::bridge::server::TokenStreamBuilder` | ||
|
||
error[E0599]: no function or associated item named `new` found for struct `yew::virtual_dom::vcomp::VChild<yew_functional::FunctionComponent<comp<MissingTypeBounds>>>` in the current scope | ||
--> $DIR/generic-props-fail.rs:28:14 | ||
| | ||
28 | html! { <Comp<MissingTypeBounds> /> }; | ||
| ^^^^ function or associated item not found in `yew::virtual_dom::vcomp::VChild<yew_functional::FunctionComponent<comp<MissingTypeBounds>>>` | ||
| | ||
::: $WORKSPACE/packages/yew-functional/src/lib.rs | ||
| | ||
| pub struct FunctionComponent<T: FunctionProvider + 'static> { | ||
| ----------------------------------------------------------- doesn't satisfy `_: yew::html::component::Component` | ||
| | ||
= note: the method `new` exists but the following trait bounds were not satisfied: | ||
`yew_functional::FunctionComponent<comp<MissingTypeBounds>>: yew::html::component::Component` | ||
|
||
error[E0277]: the trait bound `MissingTypeBounds: yew::html::component::properties::Properties` is not satisfied | ||
--> $DIR/generic-props-fail.rs:28:14 | ||
| | ||
28 | html! { <Comp<MissingTypeBounds> /> }; | ||
| ^^^^ the trait `yew::html::component::properties::Properties` is not implemented for `MissingTypeBounds` | ||
| | ||
= note: required because of the requirements on the impl of `yew_functional::FunctionProvider` for `comp<MissingTypeBounds>` | ||
|
||
error[E0277]: can't compare `MissingTypeBounds` with `MissingTypeBounds` | ||
--> $DIR/generic-props-fail.rs:28:14 | ||
| | ||
28 | html! { <Comp<MissingTypeBounds> /> }; | ||
| ^^^^ no implementation for `MissingTypeBounds == MissingTypeBounds` | ||
| | ||
= help: the trait `std::cmp::PartialEq` is not implemented for `MissingTypeBounds` | ||
= note: required because of the requirements on the impl of `yew_functional::FunctionProvider` for `comp<MissingTypeBounds>` | ||
|
||
error[E0107]: wrong number of type arguments: expected 1, found 0 | ||
--> $DIR/generic-props-fail.rs:31:14 | ||
| | ||
31 | html! { <Comp /> }; | ||
| ^^^^ expected 1 type argument |