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

EnumVariant parsing can fail with empty delimiters #92

Open
benluelo opened this issue Jan 5, 2025 · 0 comments
Open

EnumVariant parsing can fail with empty delimiters #92

benluelo opened this issue Jan 5, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@benluelo
Copy link

benluelo commented Jan 5, 2025

virtue/src/parse/body.rs

Lines 210 to 239 in 1ecc013

Some(TokenTree::Punct(p)) if p.as_char() == '=' => {
assume_punct(stream.next(), '=');
match stream.next() {
Some(TokenTree::Literal(lit)) => {
value = Some(lit);
}
Some(TokenTree::Punct(p)) if p.as_char() == '-' => match stream.next() {
Some(TokenTree::Literal(lit)) => {
match lit.to_string().parse::<i64>() {
Ok(val) => value = Some(Literal::i64_unsuffixed(-val)),
Err(_) => {
return Err(Error::custom_at(
"parse::<i64> failed",
lit.span(),
))
}
};
}
token => return Error::wrong_token(token.as_ref(), "literal"),
},
token => return Error::wrong_token(token.as_ref(), "literal"),
}
}
Some(TokenTree::Punct(p)) if p.as_char() == ',' => {
// next field
}
None => {
// group done
}
token => return Error::wrong_token(token, "group, comma or ="),

if a derive macro using virtue is applied to code generated by a macro_rules! macro, token trees are wrapped in an empty delimiter and this code fails:

macro_rules! wrapper_enum {
    (
        $(#[$meta:meta])*
        pub enum $Enum:ident {
            $(
                $(#[$inner_meta:meta])*
                $Variant:ident = $discriminant:literal,
            )+
        }
    ) => {
        $(#[$meta])*
        pub enum $Enum {
            $(
                $(#[$inner_meta])*
                $Variant = $discriminant,
            )+
        }
    };
}

wrapper_enum! {
    #[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
    pub enum HashOp {
        #[default]
        NoHash = 0,
        Sha256 = 1,
        Sha512 = 2,
        Keccak256 = 3,
        Ripemd160 = 4,
        Bitcoin = 5,
        Sha512256 = 6,
        Blake2b512 = 7,
        Blake2s256 = 8,
        Blake3 = 9,
    }
}
error: Invalid rust syntax, expected literal, got Some(Group { delimiter: None, stream: TokenStream [Literal { kind: Integer, symbol: "0", suffix: None, span: #0 bytes(129261..129262) }], span: #1458 bytes(276324..276337) })
   --> lib/unionlabs/src/macros.rs:282:28
    |
282 |                   $Variant = $discriminant,
    |                              ^^^^^^^^^^^^^
    |
   ::: lib/unionlabs/src/cosmos/ics23/hash_op.rs:3:1
    |
3   | / wrapper_enum! {
4   | |     #[model(proto(protos::cosmos::ics23::v1::HashOp))]
5   | |     #[derive(Default)]
6   | |     #[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
...   |
19  | |     }
20  | | }
    | |_- in this macro invocation
    |
    = note: this error originates in the macro `wrapper_enum` (in Nightly builds, run with -Z macro-backtrace for more info)

this can be solved in this case by changing $discriminant:literal to $discriminant:tt, but this may not always be possible.

@VictorKoenders VictorKoenders added the bug Something isn't working label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants