-
Notifications
You must be signed in to change notification settings - Fork 69
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
Emit useful error when attrs field is used and forward_attrs is missing #317
Comments
The problem here is that you didn't declare The error message is bad because we're hitting an Running #[automatically_derived]
#[allow(clippy::manual_unwrap_or_default)]
impl ::darling::FromDeriveInput for HelloArgs {
fn from_derive_input(
__di: &::darling::export::syn::DeriveInput,
) -> ::darling::Result<Self> {
let mut __errors = ::darling::Error::accumulator();
let mut __fwd_attrs: ::darling::export::Vec<::darling::export::syn::Attribute> = ::alloc::vec::Vec::new();
let mut attrs: ::darling::export::Option<_> = None;
__errors.finish()?;
::darling::export::Ok(HelloArgs {
ident: __di.ident.clone(),
attrs: attrs.expect("Errors were already checked"),
})
}
} We declare #[automatically_derived]
#[allow(clippy::manual_unwrap_or_default)]
impl ::darling::FromDeriveInput for HelloArgs {
fn from_derive_input(
__di: &::darling::export::syn::DeriveInput,
) -> ::darling::Result<Self> {
let mut __errors = ::darling::Error::accumulator();
let mut __fwd_attrs: ::darling::export::Vec<::darling::export::syn::Attribute> = ::alloc::vec::Vec::new();
let mut attrs: ::darling::export::Option<_> = None;
use ::darling::ToTokens;
for __attr in &__di.attrs {
match ::darling::export::ToString::to_string(
&__attr.path().clone().into_token_stream(),
)
.as_str()
{
_ => __fwd_attrs.push(__attr.clone()),
}
}
attrs = ::darling::export::Some(__fwd_attrs);
__errors.finish()?;
::darling::export::Ok(HelloArgs {
ident: __di.ident.clone(),
attrs: attrs.expect("Errors were already checked"),
})
}
} The enhancement here would be to add a check in |
Previously, this would result in a cryptic error due to an expect(...) failure if neither `attributes` nor `forward_attrs` were declared. This change is technically breaking, as it was previously possible to declare this field without using `forward_attrs`; if `attributes` was used the `attrs` field would be initialized to an empty array. That behavior was more likely to mask issues than be useful, but it's possible someone declared the field in order to manually populate it later. Fixes #317
Previously, this would result in a cryptic error due to an expect(...) failure if neither `attributes` nor `forward_attrs` were declared. This change is technically breaking, as it was previously possible to declare this field without using `forward_attrs`; if `attributes` was used the `attrs` field would be initialized to an empty array. That behavior was more likely to mask issues than be useful, but it's possible someone declared the field in order to manually populate it later. Fixes #317
I ran across an error ("Errors were already checked") using FromDeriveInput that seems a little difficult to understand and doesn't seem to have a good explanation anywhere. I narrowed it down to when args is included in the struct. Full repro:
I'm not sure if this is a bug in darling, I'm assuming that I'm probably using this wrong, but the error message doesn't make it easy to understand what do next. It's also problematic as it doesn't really point back to the
attrs
field as being the problem. Consider adding extra info about what this means and what is missing from the struct / config to make it correct, or documenting this in the docs (the only hit for this error message is the line of code where it's omitted).The text was updated successfully, but these errors were encountered: