Skip to content

Shape validation should never panic #365

Description

@TedDriggs

I'm writing a proc_macro_derive macro to target unit type enums.
I'm deriving darling::FromDeriveInput on my model struct, here named TargetEnum, using the #[darling(supports(enum_unit))] to validate the shape of the target, as follows:

#[derive(FromDeriveInput)]
#[darling(supports(enum_unit))]
struct TargetEnum {
    ident: Ident,
    data: Data<TargetVariant, ()>,
}

Attempting to derive on structs, named fields variants enums, tuple variants enums or newtype variants enums works as expected (clear error message + "Expected" hint), respectively1:

error: Unsupported shape `struct`. Expected enum with no fields.
 --> src/main.rs:3:10
  |
3 | #[derive(Variants)]
  |          ^^^^^^^^
  |
  = note: this error originates in the derive macro `Variants` (in Nightly builds, run with -Z macro-backtrace for more info)
error: Unsupported shape `named fields`. Expected no fields.
 --> src/main.rs:3:10
  |
3 | #[derive(Variants)]
  |          ^^^^^^^^
  |
  = note: this error originates in the derive macro `Variants` (in Nightly builds, run with -Z macro-backtrace for more info)
error: Unsupported shape `unnamed fields`. Expected no fields.
 --> src/main.rs:3:10
  |
3 | #[derive(Variants)]
  |          ^^^^^^^^
  |
  = note: this error originates in the derive macro `Variants` (in Nightly builds, run with -Z macro-backtrace for more info)
error: Unsupported shape `one unnamed field`. Expected no fields.
 --> src/main.rs:3:10
  |
3 | #[derive(Variants)]
  |          ^^^^^^^^
  |
  = note: this error originates in the derive macro `Variants` (in Nightly builds, run with -Z macro-backtrace for more info)

Now, I would like to grant downstream users with a clear error message even in the remote possibility they attempt to derive such macro on a unit type. However, the attempt results in the following, undescriptive panic:

error: proc-macro derive panicked
 --> src/main.rs:3:10
  |
3 | #[derive(Variants)]
  |          ^^^^^^^^
  |
  = help: message: internal error: entered unreachable code

Am I doing something wrong or is this to be considered a bug?
In doubt I'm opening a discussion rather than an issue.

P.S. Thank you for such useful library! Made my idea extremely simpler to implement.

Originally posted by @marcoradocchia in #364

Footnotes

  1. Variants is my macro's name ↩

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions