c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

c2rust-refactor: reorganize_definitions needs to rewrite types in attributes

Open ahomescu opened this issue 1 month ago • 3 comments

reorganize_definitions on curl fails because it needs to rewrite the ty attribute for all bitfields that use one of the moved types. This transform (and probably others) do not reach into attributes to look for Rust types, c2rust-bitfields encodes type names as strings. For example:

    pub struct OutStruct {
        pub filename:  *mut ::core::ffi::c_char,
        pub stream:  *mut crate::stdlib::FILE,
        pub bytes:  crate::system_h::curl_off_t,
        pub init:  crate::system_h::curl_off_t,
        #[bitfield(name = "alloc_filename", ty = "bit", bits = "0..=0")]
        #[bitfield(name = "is_cd_filename", ty = "bit", bits = "1..=1")]
        #[bitfield(name = "s_isreg", ty = "bit", bits = "2..=2")]
        #[bitfield(name = "fopened", ty = "bit", bits = "3..=3")]
        pub alloc_filename_is_cd_filename_s_isreg_fopened:  [u8; 1],
        #[bitfield(padding)]
        pub c2rust_padding:  [u8; 7],
    }

In this structure we need to rewrite ty = "bit" into ty = "crate::curl_setup_once_h::bit" or import it into every submodule that has bitfields.

We could add a work-around to reorganize_definitions for this specific case, but it would be nice to do something more generic.

ahomescu avatar Dec 03 '25 02:12 ahomescu

Is it necessary for this to be a string, or could it be a path in the attribute (#[ty = crate::item::whatever] with no quotes)? Replacing arbitrary bits of strings seems more worrisome than if we can limit this to references to actual Rust entities. I'm not sure if this is an actual thing that's doable in attributes, though.

fw-immunant avatar Dec 03 '25 18:12 fw-immunant

@thedataking had an interesting idea if we want to enable reorganize_definitions by default: we could try to detect this (and inline assembly) in the transpiler, and call c2rust-refactor if none of those are present.

ahomescu avatar Dec 04 '25 02:12 ahomescu

Another thought, maybe we could add self:: to the type name and only trigger this logic in the case of string values containing a double-colon?

fw-immunant avatar Dec 04 '25 13:12 fw-immunant