rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Inadequate recognition of inline namespaces

Open dtolnay opened this issue 1 year ago • 1 comments

The following is minimized from Xcode's C++ standard library implementation.

// namespace.h

#pragma once
#define BEGIN_NAMESPACE namespace repro { inline namespace __1 {
#define END_NAMESPACE } }
// main.cc

#include "namespace.h"

BEGIN_NAMESPACE

class duration {};

END_NAMESPACE
$ bindgen main.cc --enable-cxx-namespaces
/* automatically generated by rust-bindgen 0.70.1 */

#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
    #[allow(unused_imports)]
    use self::super::root;
    pub mod repro {
        #[allow(unused_imports)]
        use self::super::super::root;
        pub mod __1 {
            #[allow(unused_imports)]
            use self::super::super::super::root;
            #[repr(C)]
            #[derive(Debug, Copy, Clone)]
            pub struct duration {
                pub _address: u8,
            }
            #[allow(clippy::unnecessary_operation, clippy::identity_op)]
            const _: () = {
                ["Size of duration"][::std::mem::size_of::<duration>() - 1usize];
                ["Alignment of duration"][::std::mem::align_of::<duration>() - 1usize];
            };
        }
    }
}

Bindgen is incorrectly generating root::repro::__1::duration instead of root::repro::duration. If the 2 defines are placed into main.cc instead of namespace.h, the behavior is different.

#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
    #[allow(unused_imports)]
    use self::super::root;
    pub mod repro {
        #[allow(unused_imports)]
        use self::super::super::root;
        #[repr(C)]
        #[derive(Debug, Copy, Clone)]
        pub struct duration {
            pub _address: u8,
        }
        #[allow(clippy::unnecessary_operation, clippy::identity_op)]
        const _: () = {
            ["Size of duration"][::std::mem::size_of::<duration>() - 1usize];
            ["Alignment of duration"][::std::mem::align_of::<duration>() - 1usize];
        };
    }
}

As of current master, the logic by which bindgen decides whether a namespace is inline is here:

https://github.com/rust-lang/rust-bindgen/blob/af26991da8161f4d401d0d0532a05079449e7379/bindgen/ir/context.rs#L2267-L2286

It should be using https://docs.rs/clang-sys/1.8.1/clang_sys/fn.clang_Cursor_isInlineNamespace.html instead, which I confirmed fixes this bug.

dtolnay avatar Oct 15 '24 23:10 dtolnay

As a workaround, adding a non-macro-generated namespace repro::inline __1 {} to the top of any affected files (before includes) avoids the bug.

dtolnay avatar Oct 16 '24 08:10 dtolnay