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

Confusing error message for component with children when no children are supplied #1581

Closed
1 of 3 tasks
kellpossible opened this issue Sep 22, 2020 · 3 comments
Closed
1 of 3 tasks
Labels
A-yew-macro Area: The yew-macro crate bug macro Issues relating to our procedural or declarative macros

Comments

@kellpossible
Copy link
Contributor

I have a component with

struct Props {
    children: Children,
}

and when using the component with no children:

<MyComponent>
</MyComponent>

I get the following error message:

no method named `build` found for struct `my_component::PropsBuilder<my_component:PropsBuilderStep_missing_required_prop_children>` in the current scope
method not found in `my_component::PropsBuilder<my_component::PropsBuilderStep_missing_required_prop_children>`rustc(E0599)
my_component.rs(8, 17): method `build` not found for this
lib.rs(101, 1): Error originated from macro here

Expected behavior
I would expect that the component should work without children supplied, or at least provide a more informative error message.

Environment:

  • Yew version: v0.17
  • Rust version: 1.47.0-beta.3
  • Target, if relevant: wasm32-unknown-unknown
  • Web library: web-sys

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • I don't have time to fix this right now, but maybe later
@siku2
Copy link
Member

siku2 commented Sep 22, 2020

This is a hard problem to solve.
The biggest problem here is that the html! macro has no knowledge of the Properties struct.
A consequence of this is that we have no idea whether the component has a children field or not.
In return, this means that we cannot simply pass an empty Children because that would break props without a children field.

Improving the error message is also, for the same reasons, a daunting task.

There's still hope though. We can turn children into a special case in the Properties derive macro and use setter methods to set it.
A method set_empty_children() is generated for all props (with or without children). If the props struct doesn't have a children field this method is a no-op, but if it does, an empty Children value is used.

In case there is at least one child element, another method called set_children is used. This method is only generated for props which have a children field so we still get a compile error when trying to pass children to components that don't accept any.


EDIT: I should also mention that one can use #[prop_or_default] to allow optional children. My comment is focused on how to make it optional by default.

@siku2 siku2 added the macro Issues relating to our procedural or declarative macros label Sep 22, 2020
@kay-is
Copy link

kay-is commented Nov 28, 2020

I had a similar issue.

pub struct Props {
  pub children: Children,
  pub active: bool,
}

This threw an error at call-site when I didn't pass an active prop, which makes sense.

The strange thing was the actual error message "no method named children found for struct ..."

I had the impression I did something wrong with the children prop because the example on the Yew website uses a tuple struct, but in the end, it was because the active prop wasn't set to optional.

@mc1098 mc1098 added the A-yew-macro Area: The yew-macro crate label Sep 20, 2021
@mc1098
Copy link
Contributor

mc1098 commented Sep 20, 2021

Closing this in favour of #1004 as it came first and is about the same problem :)
This issue will be linked in the thread so these useful comments won't be lost ❤️ - keeping it all contained in one issue helps yew track the issues more easily and helps others keep track of progress.

@mc1098 mc1098 closed this as completed Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew-macro Area: The yew-macro crate bug macro Issues relating to our procedural or declarative macros
Projects
None yet
Development

No branches or pull requests

4 participants