Skip to content

derive(ArrowField) breaks in macros #85

@douglas-raillard-arm

Description

@douglas-raillard-arm

I'm trying to either:

  • apply #[derive(ArrowField)] to a struct defined in a macro_rules
  • apply my macro_rules on a struct using #[derive(ArrowField)]

The struct def is parsed using this little book of rust macros:
https://veykril.github.io/tlborm/decl-macros/building-blocks/parsing.html#struct

use arrow2_convert::ArrowField;

macro_rules! mymacro {
    (
        $( #[$meta:meta] )*
        //  ^~~~attributes~~~~^
            $vis:vis struct $name:ident {
                $(
                    $( #[$field_meta:meta] )*
                    //          ^~~~field attributes~~~!^
                        $field_vis:vis $field_name:ident : $field_ty:ty
                    //          ^~~~~~~~~~~~~~~~~a single field~~~~~~~~~~~~~~~^
                ),*
                    $(,)? }
    ) => {
        $( #[$meta] )*
            $vis struct $name {
                $(
                    $( #[$field_meta] )*
                        $field_vis $field_name : $field_ty
                ),*
            }
    }
}

mymacro! {
    #[derive(ArrowField)]
    struct Foo2 {
        myfield: u8,
    }
}

Sadly this fails with:

error: proc-macro derive panicked
  --> src/main.rs:97:14
   |
97 |     #[derive(ArrowField)]
   |              ^^^^^^^^^^
   |
   = help: message: Only types are supported atm

error: could not compile `dataframe` due to previous error

The only macro that seemed to work is this one:

macro_rules! mymacro2 {
    ($($tts:tt)*) => { $($tts)* }
}

Other derive macros such as JsonSchema don't seem to have this issue:
https://docs.rs/schemars/latest/schemars/

EDIT: This also fails, but differently:

mymacro! {
    struct Foo2 {
        myfield: u8,
    }
}

#[derive(ArrowField)]
struct Foo3(Foo2);

fails with:

error: proc-macro derive panicked
   --> src/main.rs:114:10
    |
114 | #[derive(ArrowField)]
    |          ^^^^^^^^^^
    |
    = help: message: called `Option::unwrap()` on a `None` value

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions