rust-lang / rust-lang/rust-bindgen

bindgen gets confused with arrays in generics.

Open
#1,557 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-libclang bug
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
/// Header data with an inline length. Consumers that use HeaderWithLength as the
/// Header type in HeaderSlice can take advantage of ThinArc.
template<typename H>
struct StyleHeaderWithLength {
  /// The fixed-sized data.
  H header;
  /// The slice length.
  unsigned length;

  bool operator==(const StyleHeaderWithLength& other) const {
    return header == other.header &&
           length == other.length;
  }
  bool operator!=(const StyleHeaderWithLength& other) const {
    return header != other.header ||
           length != other.length;
  }
};

/// Structure to allow Arc-managing some fixed-sized data and a variably-sized
/// slice in a single allocation.
template<typename H, typename T>
struct StyleHeaderSlice {
  /// The fixed-sized data.
  H header;
  /// The dynamically-sized data.
  T slice;

  bool operator==(const StyleHeaderSlice& other) const {
    return header == other.header &&
           slice == other.slice;
  }
  bool operator!=(const StyleHeaderSlice& other) const {
    return header != other.header ||
           slice != other.slice;
  }
};

template<typename H, typename T>
using StyleHeaderSliceWithLength = StyleHeaderSlice<StyleHeaderWithLength<H>, T>;

/// The object allocated by an Arc<T>
template<typename T>
struct StyleArcInner {
  unsigned count;
  T data;

  bool operator==(const StyleArcInner& other) const {
    return count == other.count &&
           data == other.data;
  }
  bool operator!=(const StyleArcInner& other) const {
    return count != other.count ||
           data != other.data;
  }
};

template<typename H, typename T>
struct StyleThinArc {
  StyleArcInner<StyleHeaderSliceWithLength<H, T[1]>> *ptr;
};
Bindgen Invocation
$ bindgen input.h
Actual Results
// ...

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct StyleThinArc {
    pub ptr: *mut StyleArcInner<T>,
}

Which obviously doesn't 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

The reproduction is the C++ header shown in the issue; start by running bindgen input.h and comparing the generated StyleThinArc definition with the template declarations. Trace the generic-array handling that produces the invalid Rust type, then verify that the generated bindings compile for this input.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.