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

Make <LitStr as FromMeta>::from_string no longer return an error #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Adhalianna
Copy link

The FromMeta implementation for syn::LitStr is now more complete fixing difficult to debug macro
panics that would occur when the type was used to build the macro.

Without this commit I would get the following panics:

error: proc-macro derive panicked
 --> models/src/lib.rs:1:44
  |
1 | #[derive(serde::Deserialize, Debug, Clone, jayapi::AsResource)]
  |                                            ^^^^^^^^^^^^^^^^^^
  |
  = help: message: called `Result::unwrap()` on an `Err` value: Error { kind: UnexpectedType("string"), locations: [], span: None }

for my derive macro built using darling. One of the structs nested on the one that had derived darling::FromDeriveInput used syn::LitStr type as its field. With this commit which actually adds only from_string function, lines 579-581, (the rest is copy-paste from the from_meta_lit! macro) the panic is gone.

I am not familiar with syn so I guess there might be a smarter way of creating LitStr from a &str but this works for me perfectly fine.

The `FromMeta` implementation for `syn::LitStr` is
now more complete fixing difficult to debug macro
panics that would occur when the type was used to
build the macro.
@TedDriggs
Copy link
Owner

How/why are you getting into the situation that this is being called with a &str while the receiver field wants a LitStr?

I'm torn on this, because the point of using a LitStr receiver field rather than a String would be to keep the span information, so we're adding a new impl in order to allow someone to wrap a String without that span information.

@TedDriggs TedDriggs changed the title Fix panics occuring when syn::LitStr was used Make <LitStr as FromMeta>::from_string no longer return an error Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants