Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array members converted to pointers when using alias template #1057

Open
m-ou-se opened this issue Oct 3, 2017 · 4 comments
Open

Array members converted to pointers when using alias template #1057

m-ou-se opened this issue Oct 3, 2017 · 4 comments

Comments

@m-ou-se
Copy link
Member

m-ou-se commented Oct 3, 2017

Input C/C++ Header

template<int N>
using vec = double[N];

struct Thing {
	vec<3> position;
	vec<4> orientation;
	double diameter;
};

Bindgen Invocation

$ bindgen --no-layout-tests test.hpp

Actual Results

pub type vec = *mut f64;
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Thing {
    pub position: vec,
    pub orientation: vec,
    pub diameter: f64,
}
impl Clone for Thing {
    fn clone(&self) -> Self { *self }
}

Expected Results

pub struct Thing {
    pub position: [f64; 3],
    pub orientation: [f64; 4],
    pub diameter: f64,
}

position and orientation are arrays of doubles. In the generated code, they are pointers.

@pepyakin
Copy link
Contributor

pepyakin commented Oct 4, 2017

Hi! Thank you for the report!
I think that it is related to this issue.

IIUC, bindgen have a strategy to convert all items that have non-type template parameters to opaque blobs.
I'm not entirely sure, but maybe we could handle arrays as a special case...

@m-ou-se
Copy link
Member Author

m-ou-se commented Oct 4, 2017

Structs with non-type template parameters indeed turn into blobs:

template<int N> struct vec { double a[N]; };

struct Thing {
	vec<3> position;
	vec<4> orientation;
	double diameter;
};

Turns into:

pub struct Thing {
    pub position: [u64; 3usize],
    pub orientation: [u64; 4usize],
    pub diameter: f64,
}

(Note the u64, not f64.) At least the lay-out matches the original struct, even though the members aren't easily usable.

However, with the alias template for an array as in this bug report, the resulting struct doesn't even match the original struct layout as the arrays/blobs are replaced with pointers.

@pepyakin
Copy link
Contributor

pepyakin commented Oct 4, 2017

Ah, I see, thanks for clarification!
I will look into it

@fitzgen
Copy link
Member

fitzgen commented Oct 4, 2017

Structs with non-type template parameters indeed turn into blobs:

This is the desired behavior here, and we need to figure out why it isn't happening with the template alias.

We don't support translating anything with non-type template parameters to anything other than opaque blobs. When Rust gains const generics, we can start revisiting this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants