rust-lang / rust-lang/rust-bindgen

bitfield_align_2.rs test fails to build on Windows

Open
#1,009 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-bitfields A-testing bug I-bogus-codegen
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

cargo test under tests\expectations fails today when building bitfield_align_2.rs.

Error message:

C:\Users\andre\Projects\rust-bindgen\tests\expectations [master ≡]> cargo test
   Compiling tests_expectations v0.1.0 (file:///C:/Users/andre/Projects/rust-bindgen/tests/expectations)
error[E0512]: transmute called with differently sized types: u64 (64 bits) to i32 (32 bits)
  --> tests\bitfield_align_2.rs:93:18
   |
93 |         unsafe { ::std::mem::transmute(val as u64) }
   |                  ^^^^^^^^^^^^^^^^^^^^^ transmuting between 64 bits and 32 bits

error: aborting due to previous error(s)

error: Could not compile `tests_expectations`.
Build failed, waiting for other jobs to finish...
error: build failed

This is the function being built:

#[inline]
    pub fn ptr(&self) -> ::std::os::raw::c_long {
        let mut unit_field_val: u64 = unsafe { ::std::mem::uninitialized() };
        unsafe {
            ::std::ptr::copy_nonoverlapping(
                &self._bitfield_1 as *const _ as *const u8,
                &mut unit_field_val as *mut u64 as *mut u8,
                ::std::mem::size_of::<u64>(),
            )
        };
        let mask = 18446744073709551612u64 as u64;
        let val = (unit_field_val & mask) >> 2usize;
        unsafe { ::std::mem::transmute(val as u64) }
    }

The problem is that on Windows c_long is 32-bits, whereas on all other platforms (as defined by Rust), it is 64-bits. We need to somehow change the as operator to narrow the type to the size of the platform representation of the type.

The file being translated is bitfield_align_2.h

// bindgen-flags: --rustified-enum .*
enum MyEnum {
    ONE,
    TWO,
    THREE,
    FOUR
};

struct TaggedPtr {
    enum MyEnum tag : 2;
    long   ptr : 62;
};

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 with tests/bitfield_align_2.rs and its input tests/bitfield_align_2.h, then run cargo test under tests/expectations on Windows to reproduce the c_long transmute failure. Compare the generated binding's behavior on Windows and other platforms; done means this expectation test builds successfully on all affected platforms.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
compilers, devtools, operating-systems, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.