rust-lang / rust-lang/rust-bindgen

Generics missing when `allowlist_recursively` is false

Open
#3,373 1 comment 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

Take this example C++ code:

#pragma once

template <typename Type> class Vector2 {
  Type x;
  Type y;
};

template <typename Type> class Rectangle {
public:
  using Vector2Type = Vector2<Type>;

  Vector2Type p0;
  Vector2Type p1;
};

using Vector2f = Vector2<float>;
using Rectanglef = Rectangle<float>;

The bindings generated by bindgen when allowlist_recursively is false are:

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Vector2<Type> {
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<Type>>,
    x: Type,
    y: Type,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Rectangle<Type> {
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<Type>>,
    pub p0: Rectangle_Vector2Type,
    pub p1: Rectangle_Vector2Type,
}
pub type Rectangle_Vector2Type = Vector2<Type>;
pub type Vector2f = Vector2<f32>;
pub type Rectanglef = Rectangle<f32>;

The generic type from Rectangle_Vector2Type has been dropped and the code is no longer valid. Setting allowlist_recursively to true will generate the correct code:

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Rectangle<Type> {
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<Type>>,
    pub p0: Rectangle_Vector2Type<Type>,
    pub p1: Rectangle_Vector2Type<Type>,
}
pub type Rectangle_Vector2Type<Type> = Vector2<Type>;

Adding in more allowlist types, like Vector2Type or Rectangle::Vector2Type still does not prevent the generic from being generated correctly.

Bindgen version: 0.72.1

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

Reproduce the C++ example with bindgen 0.72.1 using both values of allowlist_recursively and compare the generated aliases and fields. Trace how generic arguments are retained for the nested Rectangle::Vector2Type alias, then add a regression test for the false setting. Done means the generated bindings preserve and remain valid without recursive allowlisting.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.