rust-lang / rust-lang/rust-bindgen

bindgen disregards c++ using directives.

Open
#2,208 2 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

Input C/C++ Header

#include <compare>, found in gcc 10.0+, see link:

namespace std _GLIBCXX_VISIBILITY(default)
{
  // [cmp.categories], comparison category types

  namespace __cmp_cat
  {
    using type = signed char;

    enum class _Ord : type { equivalent = 0, less = -1, greater = 1 };

    enum class _Ncmp : type { _Unordered = 2 };

    struct __unspec
    {
      constexpr __unspec(__unspec*) noexcept { }
    };
  }
  // snip
}
Bindgen Invocation

bindgen flags:

        opaque_types = [
            "std::.*",
        ],

bindgen output:

        pub mod __cmp_cat {
            #[allow(unused_imports)]
            use self::super::super::super::root;
            pub type type_ = u8;
            pub const _Ord_equivalent: root::std::__cmp_cat::_Ord = 0;
            pub const _Ord_less: root::std::__cmp_cat::_Ord = -1;
            pub const _Ord_greater: root::std::__cmp_cat::_Ord = 1;
            pub type _Ord = root::std::__cmp_cat::type_;
            pub const _Ncmp__Unordered: root::std::__cmp_cat::_Ncmp = 2;
            pub type _Ncmp = root::std::__cmp_cat::type_;
            #[repr(C)]
            #[repr(align(1))]
            #[derive(Debug, Copy, Clone)]
            pub struct __unspec {
                pub _bindgen_opaque_blob: u8,
            }
        }
Actual Results
error[E0600]: cannot apply unary operator `-` to type `u8`
    --> xxx/lib.rs:1763:63
     |
1763 |             pub const _Ord_less: root::std::__cmp_cat::_Ord = -1;
     |                                                               ^^
     |                                                               |
     |                                                               cannot apply unary operator `-`
     |                                                               help: you may have meant the maximum value of `u8`: `u8::MAX`
     |
     = note: unsigned values cannot be negated
Expected Results

Bindgen should use pub tye type_ = i8 (instead of u8), because c++ has: using type = signed char;, hence the code should compile.

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

Start by reproducing the provided C++ header and bindgen configuration, then trace how the using type = signed char alias is translated into the generated Rust type. Add a regression test if the repository has a suitable bindgen test location; done means the alias becomes i8 and the negative _Ord_less constant compiles.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.