rust-diplomat / diplomat

Experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code
https://rust-diplomat.github.io/book/
Other
512 stars 48 forks source link

C++ code should only add decl header dependencies for struct fields #514

Closed Manishearth closed 2 months ago

Manishearth commented 3 months ago

Currently the C++ code includes decl headers in .d.hpp files for all types found in the class, including in methods.

I think this will be a problem for self-containedness when we have two structs referencing each other

struct Foo {
 a: Bar;
}

struct Bar { a: u8 }

impl Foo {
 fn get_bar() -> Bar {}
}

impl Bar {
 fn get_foo() -> Foo {}
}

You can see the problem this generates with the following two files, when you attempt to compile b.hpp. It goes away if you remove the #include "a.hpp" in b.hpp.

// a.hpp
#ifndef A_HPP
#define A_HPP
#include "b.hpp"
class B;
class A {
    B b;
    B toB();
};
#endif

// b.hpp
#ifndef B_HPP
#define B_HPP
#include "a.hpp"
class A;
class B {
    int x;
    A toA();
};
#endif

We should keep track of when we are generating struct fields vs methods and only add header dependencies for the former.