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
Give a friendly error for enums with fields #45
Conversation
Right now we generate invalid code, and the error message is pretty inscrutable. Instead, generate a nice descriptive user-facing error.
fb4f0a6
to
581303d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ✅
let fields = variant.fields; | ||
|
||
match fields { | ||
Fields::Named(_) | Fields::Unnamed(_) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny nit: since tuple variants don't really have fields / or rather, it may not be seen as such by some users, I'd replace the fields
mention for Fields::Unnamed
with something else. Maybe something like associated data / payloads, or even simpler: "only unit variants are supported". Since this is subjective, feel free to disregard this 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great feedback - thanks!
num_enum_derive/src/lib.rs
Outdated
let mut span = variant.span(); | ||
// Sadly `Span::join` is a nightly-only API at the moment - see https://github.com/rust-lang/rust/issues/54725 | ||
if let Some(span_with_fields) = span.join(variant.fields.span()) { | ||
span = span_with_fields; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, if we were to use Error::new_spanned(variant
over Error::new(variant.span()
(the die!
macro currently leads us to the latter; maybe we could try to feature a die_spanned_at
variant for the former), then we'd have a correctly spanned error message, even on stable Rust. Indeed, new_spanned
uses a hack to achieve this:
// imbued with the _start_ of the span
// vvvvvvvvvvvvvv
compile_error! { "…" }
// ^^^^^^^
// imbued with the _end_ of the span
effectively yielding an error which does span over the whole range. Demo using new_spanned(variant
vs. new(variant.span()
:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, TIL! This is amazing - done - thanks for the suggestion!
Right now we generate invalid code, and the error message is pretty
inscrutable. Instead, generate a nice descriptive user-facing error.