Skip to content

[Rust codegen] Incorrect types for u8 arrays & duplicate field when parsing #81

@Redstoneguy129

Description

@Redstoneguy129

Problem 1

When an IDL contains event types with "type": "[u8, 32]" fields, the generated Rust code fails to compile:

mismatched types expected reference &u8 found reference &[u8; 32] (rustc E0308) hint: Actual error occurred here
pub padding0: [u8; 32],
{
    "name": "padding_0",
    "type": { "array": ["u8", 32] }
}

Problem 2

Rust codegen removes "_" character from fields. This can lead to duplicate field issues when a badly written Anchor program has two fields in the same account "padding_0" and "_padding_0":

pub struct MeteoraDammMigrationMetadata {
pub discriminator: [u8; 8],
...
/// !!! BE CAREFUL to use tomestone field, previous is pool creator
pub padding0: [u8; 32],
...
/// padding
pub padding0: u8,
...
{
    "name": "padding_0",
    "type": { "array": ["u8", 32] }
}
...
{ "name": "_padding_0", "docs": ["padding"], "type": "u8" },

Solution

Codegen with correct types and allow "_" character if its index 0 of a field name.

Workaround

Manually changed field name in IDL, however you shouldn't have to do this in the first place.

IDL and Program

dbcij3LWUppWqq96dh6gJWwBifmcGfLSB5D4DuSMaqN
https://solscan.io/account/dbcij3LWUppWqq96dh6gJWwBifmcGfLSB5D4DuSMaqN#programIdl

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