rust-lang / rust-lang/rust-bindgen

bindgen cannot generate `Default` trait for enum

Open
#2,748 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

I don't know if this is a bug, or it is by design.

Input C/C++ Header

A minimal header

typedef enum cudaError_enum {
    CUDA_SUCCESS = 0,
    CUDA_ERROR_INVALID_VALUE = 1,
    CUDA_ERROR_OUT_OF_MEMORY = 2,
};
Bindgen Invocation

With bindgen 0.69.4.

bindgen \
    --default-enum-style=newtype \
    --with-derive-default \
    header.h
Actual Results
impl cudaError_enum {
    pub const CUDA_SUCCESS: cudaError_enum = cudaError_enum(0);
}
impl cudaError_enum {
    pub const CUDA_ERROR_INVALID_VALUE: cudaError_enum = cudaError_enum(1);
}
impl cudaError_enum {
    pub const CUDA_ERROR_OUT_OF_MEMORY: cudaError_enum = cudaError_enum(2);
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct cudaError_enum(pub ::std::os::raw::c_uint);
Expected Results

Currently I have to use

bindgen \
    --default-enum-style=newtype \
    --with-derive-default \
    --with-derive-custom .*_enum=Default \ 
    header.h

to generate

impl cudaError_enum {
    pub const CUDA_SUCCESS: cudaError_enum = cudaError_enum(0);
}
impl cudaError_enum {
    pub const CUDA_ERROR_INVALID_VALUE: cudaError_enum = cudaError_enum(1);
}
impl cudaError_enum {
    pub const CUDA_ERROR_OUT_OF_MEMORY: cudaError_enum = cudaError_enum(2);
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Default)]
pub struct cudaError_enum(pub ::std::os::raw::c_uint);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with header.h and the bindgen 0.69.4 command using --default-enum-style=newtype and --with-derive-default. Trace how these options handle generated enum newtypes, then add or update coverage so the output includes Default without --with-derive-custom; done means the generated struct derives Default as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.