mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.43k stars 315 forks source link

Enums initialized with other enum constants don't take renaming into account #999

Open jsgf opened 2 months ago

jsgf commented 2 months ago

Given the Rust file foo.rs:

#[repr(u32)]
pub enum A {
    Apple = 1,
    Pear = 2,
    Banana = 3,
}

#[repr(u32)]
pub enum B {
    A = A::Apple as u32,
    B = A::Pear as u32,
    C = A::Banana as u32,
}

and the cbindgen.toml config

language = "C"
cpp_compat = false

[export]
include = ["A", "B"]
item_types = ["enums"]

[enum]
rename_variants = "QualifiedScreamingSnakeCase"

The command cbindgen -c cbindgen.toml foo.rs generates:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

enum A {
  A_APPLE = 1,
  A_PEAR = 2,
  A_BANANA = 3,
};
typedef uint32_t A;

enum B {
  B_A = (uint32_t)A_Apple,
  B_B = (uint32_t)A_Pear,
  B_C = (uint32_t)A_Banana,
};
typedef uint32_t B;

Note that B_A is initialized from A_Apple rather than A_APPLE, ignoring the rename_variants = "QualifiedScreamingSnakeCase" config option.