Skip to content

Keep the integer literal radices of C and C++ in generated Rust #3236

Open
@miikkas

Description

@miikkas

It would be great if the Rust code generated by bindgen would keep the number bases of the integer literals in the input C and C++ code. After all, the writer of the C or C++ code probably had a good reason for using a specific radix.

C++141, C232, and Rust3 all support binary, octal, and hexadecimal base numbers, with C++14 and C23 sharing the same syntax. A one-to-one mapping with Rust seems to be possible.

For example, given the following code working in both C23 and C++14:

#define BIN_LIT 0b10
#define OCT_LIT 010
#define HEX_LIT 0x10

the generated Rust bindings would be:

pub const BIN_LIT: u32 = 0b10;
pub const OCT_LIT: u32 = 0o10;
pub const HEX_LIT: u32 = 0x10;

The bases of consts and enums would be kept as well.

A bit of not too serious motivation

Just for the reference, if someone finds this interesting. I ran the following command in a fresh git clone of the Linux kernel repository, finding lines in .h files only that have a #define with a hexadecimal literal later on that line:

rg "\#define.+\b0x" --type h | wc -l

Currently, that's 4 519 652 such lines.

Footnotes

  1. ISO/IEC 14882:2014 draft N3054, chapter 6.4.4.1 Integer constants

  2. ISO/IEC 9899:2023 draft N3797, chapter 2.14.2 Integer literals

  3. Rust Reference: 2.6 Tokens, Number literals, 8.2.1 Literal expressions, Integer literals expressions

Activity

miikkas

miikkas commented on Jun 18, 2025

@miikkas
Author

I have actually made an attempt at a prototype for this, which I'll submit as a PR shortly.

changed the title [-]Keep the integer literal radices of C in generated Rust[/-] [+]Keep the integer literal radices of C and C++ in generated Rust[/+] on Jun 26, 2025
miikkas

miikkas commented on Jul 12, 2025

@miikkas
Author

This feature would presumably be somewhat invasive: Many projects updating bindgen would possibly need to update lots of generated bindings with literal values changed to use the original radix. This, in turn, might lead to a lot of work in those projects to review and test changes, or cause them to postpone updating bindgen.

If that is a concern, to reduce churn and allow some breathing room for projects using bindgen, the introduction of the feature could be phased by adding an option to Builder that would default to disabling the feature in the first version where it is introduced, then moving on to defaulting to enabling it in the next version, then deprecating the option in the next, and so on. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @miikkas

      Issue actions

        Keep the integer literal radices of C and C++ in generated Rust · Issue #3236 · rust-lang/rust-bindgen