rust-lang / rust-lang/rust-bindgen

[c++] Consider implementing Drop for types with destructors

Open
#1,840 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Consider the following C++ code:

#include "stdio.h"
class Inner {
public:
	Inner();
	~Inner();
};
Inner::Inner() {
	printf("constructing Inner\n");
}
Inner::~Inner() {
	printf("destructing Inner\n");
}
class Outer {
	Inner in;
public:
	Outer();
	~Outer();
};
Outer::Outer() {
	printf("constructing Outer\n");
}
Outer::~Outer() {
	printf("destructing Outer\n");
}

If you use bindgen to execute

let mut val = Outer::new();
val.destruct();

You get the output

constructing Inner
constructing Outer
destructing Outer
destructing Inner

Which is also the output of the C++ code

#include "wrapper.cpp"
int main() {
	Outer obj;
}

If we now remove the ~Outer() deconstructor, this C++ code outputs

constructing Inner
constructing Outer
destructing Inner

But the rust code

let mut val = Outer::new();
val.destruct();

no longer compilers because Outer::destruct does not exist and the rust code

let mut val = Outer::new();

just outputs

constructing Inner
constructing Outer

which might result in memory leaks or similar bugs.
A similar problem arises if you remove the default constructor.

Is it possible to automatically add constructors and destructors that call the constructors and destructors of the member variables?

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 by reproducing the examples with wrapper.cpp and the generated Rust bindings for Outer, both with and without an explicit destructor or default constructor. Trace how bindgen exposes Outer::new and Outer::destruct, then define completion as bindings that preserve member construction and destruction behavior without requiring methods absent from the C++ type.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.