Skip to content

Added codegen for destructors #542

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

Closed

Conversation

nikhilshagri
Copy link
Contributor

Fixes #529

cc @emilio

@highfive
Copy link

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! I think it requires a bit more work though, since I don't think in this state it compiles.

Also, this needs a test in bindgen-integration, you can do something like:

// Test.h
// inside class Test {...
  static bool sDestructorCalled;
  ~Test();
// lib.rs
impl Drop for Test {
    fn drop(&mut self) {
        self.destruct()
    }
}

{
    let test = Test::new();
}

assert!(Test_sDestructorCalled);

Or something like that.

@@ -52,3 +52,13 @@ fn bindgen_test_layout_UnionWithDtor() {
"Alignment of field: " , stringify ! ( UnionWithDtor ) , "::"
, stringify ! ( mBar ) ));
}
extern "C" {
#[link_name = "_ZN13UnionWithDtorD1Ev"]
pub fn UnionWithDtor_~UnionWithDtor(this: *mut UnionWithDtor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can possibly compile? We need to mangle this name.

Copy link
Contributor Author

@nikhilshagri nikhilshagri Feb 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't that a valid identifier? I think Rust accepts all valid Unicode strings....

Edit: hmm...seems not, just tried using the identifier on playground.

@fitzgen
Copy link
Member

fitzgen commented Mar 6, 2017

@cynicaldevil how's this going? Anything we can do to help get this across the finish line? Questions we can answer?

@nikhilshagri
Copy link
Contributor Author

@fitzgen I have my midterms right now, but finally got a break in between, so I can work on this now. Any suggestions on what the mangled name of the destructor should be?

@fitzgen
Copy link
Member

fitzgen commented Mar 6, 2017

Any suggestions on what the mangled name of the destructor should be?

_bindgen_destructor_<class-or-struct-name> should be OK.

@nikhilshagri
Copy link
Contributor Author

So I'm trying to name the function as *class_name*__bindgen__destructor so that it is consistent with how the constructor is named, and I'm using the canonical_name method to get the whole string, remove the part after the tilde and append __bindgen_destructor__ to it. Is there a better way of doing this?

@fitzgen
Copy link
Member

fitzgen commented Mar 8, 2017

@cynicaldevil is the canonical_name returning a name with the ~ in it? That is not what I would expect.

The Item::name method returns a NameOptions which gives a little bit more control over the name.

I would have expected format!("_bindgen_desctructor_{}", item.canonical_name()) to Just Work, and I'm a bit surprised it isn't...

@emilio
Copy link
Contributor

emilio commented Mar 8, 2017

It isn't because the name of the destructor starts with ~. We can strip it out while parsing though.

@nikhilshagri
Copy link
Contributor Author

How can we strip away the ~ during parsing? I can't find anywhere to do it except in codegen.

@fitzgen
Copy link
Member

fitzgen commented Mar 13, 2017

Presumably in the Function/FunctionSig parsing code in src/ir/function.rs. I'm not really convinced that's any better or worse than in codegen, however.

I suggest just getting it working and then we can look at the code in review and decide if we think there is a better way or not.

@nikhilshagri
Copy link
Contributor Author

@fitzgen After adding countless debug! statements to understand the flow of execution, I managed to get it to work as intended :)

@fitzgen
Copy link
Member

fitzgen commented Mar 14, 2017

@cynicaldevil It looks like this is failing a few tests with libclang 3.8 on Travis CI:

 extern "C" {
-    #[link_name = "_ZN7nsSlotsD1Ev"]
+    #[link_name = "_ZN7nsSlotsD0Ev"]
     pub fn nsSlots_nsSlots_destructor(this: *mut nsSlots);
 }

Do these test expectations just need to be updated or do we need to ignore these tests with libclang <= 3.8?

If the latter, you can add // bindgen-unstable to the top of the tests.

@nikhilshagri
Copy link
Contributor Author

I don't know why this is happening, all my expectations are already up-to-date, and I am using clang 3.9.

@gsingh93
Copy link

Bump. I'm waiting for this feature :)

@emilio
Copy link
Contributor

emilio commented Apr 3, 2017

Ok, I just looked into why tests are failing. That test has a virtual destructor, that we should avoid generating. So I'm going to pull this and land it with that fix.

@emilio
Copy link
Contributor

emilio commented Apr 3, 2017

(I mean, it's not technically wrong to generate it, but I'd rather just do the proper thing for virtual methods instead)

@emilio emilio mentioned this pull request Apr 3, 2017
@emilio
Copy link
Contributor

emilio commented Apr 3, 2017

Superseded by #608, thanks for this work @cynicaldevil! :)

@emilio emilio closed this Apr 3, 2017
bors-servo pushed a commit that referenced this pull request Apr 3, 2017
Destructor codegen

Based on #542, and on top of #606, with a bunch more tests and fixes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants