-
Notifications
You must be signed in to change notification settings - Fork 61
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
Error when attributes have been misused #105
Comments
I'd like to take a shot at this since it bit me. :) I've got it reporting a better error that points to the enum variant with the - SnafuAttribute::Source(..) => { /* Report this isn't valid here? */ }
+ SnafuAttribute::Source(..) => {
+ return Err(vec![syn::Error::new(
+ name.span(),
+ "'source' attribute is only valid on individual fields; attempted to place on this variant:",
+ )])
+ } ( Here's the result, with the bad code from https://gist.github.com/tjkirch/8917dd863bc284db20384fb303651ea6:
If this seems OK, I can do something similar for the other "Report this isn't valid here" comments. |
Wonderful, thank you!
That’s a tough question. In an ideal world, we’d have the ability to add The other thing I could think of would be to point at the attribute itself, so that it was very clear which attribute we are talking about. This would probably involve keeping some span information around from attribute parse time. That being said, I think that any error will save people some heartache, so I’m down with getting messages in and iterating.
One thing to note is that I’d like to try and report as many errors in one pass as possible. For example, this should generate two errors, one for #[derive(Snafu)]
enum X {
#[snafu(source)]
#[snafu(backtrace)]
Variant,
} I’m still figuring out great patterns for this kind of work. In another branch, I have a macro that allows multiple back-to-back errors to happen. In this case, we might want a vector to collect all the errors in and then return it; something like: let mut errors = Vec::new();
for attr in attributes_from_syn(variant.attrs)? { … }
if !errors.is_empty() { return Err(errors) } |
Feel free to open a draft PR early. Unfortunately for you, I’m fairly nit-picky in my reviews 😇 |
First, I tried a Vec for the entire |
Yep, that was my eventual idea, but I wasn’t sure how complicated it would end up being, so I didn’t want to bring it up. For example, that |
It was trivial to do the larger scope in the end. I didn't look through all of the existing errors, but I did see some that might fit better into the Vec than as an immediate return. I left that for separate consideration, though. |
Find and fix all the
Report this isn't valid here
comments. Super annoying!The text was updated successfully, but these errors were encountered: