Skip to content

Destructor codegen #608

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

Merged
merged 10 commits into from
Apr 3, 2017
Merged

Destructor codegen #608

merged 10 commits into from
Apr 3, 2017

Conversation

emilio
Copy link
Contributor

@emilio emilio commented Apr 3, 2017

Based on #542, and on top of #606, with a bunch more tests and fixes.

@highfive
Copy link

highfive commented Apr 3, 2017

warning Warning warning

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

@emilio
Copy link
Contributor Author

emilio commented Apr 3, 2017

r? @fitzgen

@emilio
Copy link
Contributor Author

emilio commented Apr 3, 2017

Hm... So this still fails for 3.8. The reason for that is that the symbol produced is for the D0 destructor group, instead of D1. We definitely want the D1 group, so I'm going to change the symbol manually to point to the correct one.

See http://stackoverflow.com/questions/6613870/gnu-gcc-g-why-does-it-generate-multiple-dtors/6614369#6614369 for a quick description of the problem.

@emilio
Copy link
Contributor Author

emilio commented Apr 3, 2017

Added a commit that should fix that.

@emilio emilio force-pushed the destructor-codegen branch from 3c8dafb to f5188a6 Compare April 3, 2017 13:00
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Sweet! Thanks!

Before landing, please:

  • add tests for when we're explicitly not generating ctors and dtors

  • squash to preserve bisect-ability, since tests started failing in the middle of the commit series

Thanks again!

src/ir/comp.rs Outdated
@@ -434,6 +445,11 @@ impl CompInfo {
&self.constructors
}

/// Get this type's destructor.
pub fn destructor(&self) -> &Option<ItemId> {
&self.destructor
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any reason to avoid copies here; it'll just make using this slightly more annoying. Instead we should do:

pub fn destructor(&self) -> Option<ItemId> {
        self.destructor
}

since Option<T> is Copy if T is Copy.

@@ -0,0 +1,6 @@
// bindgen-flags: --generate types,constructors,functions
Copy link
Member

Choose a reason for hiding this comment

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

We should also test when we are not generating constructors.

src/ir/comp.rs Outdated
@@ -446,8 +451,8 @@ impl CompInfo {
}

/// Get this type's destructor.
pub fn destructor(&self) -> &Option<ItemId> {
&self.destructor
pub fn destructor(&self) -> Option<(bool, ItemId)> {
Copy link
Member

Choose a reason for hiding this comment

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

That bool is a little gross. As a calller of this API, now I have to remember wait does true mean virtual or not? Why not reuse MethodKind here?

Also, the doc comment should be updated to describe the extra information exposed.

// Add a suffix to avoid colliding with constructors. This would be
// technically fine (since we handle duplicated functions/methods),
// but seems easy enough to handle it here.
name.push_str("_destructor");
Copy link
Member

Choose a reason for hiding this comment

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

We definitely don't want to allow bindings users to mix up overloaded constructors and destructors!

@@ -0,0 +1,7 @@
// bindgen-flags: --generate types,destructors,functions
Copy link
Member

Choose a reason for hiding this comment

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

Again, we should also test not generating destructors.

@@ -21,6 +21,15 @@ Test::Test(double foo)
, m_double(foo)
{}

AutoRestoreBool::AutoRestoreBool(bool* ptr)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding an integration test!

//
// FIXME(emilio): Can a legit symbol in other ABIs end with this string?
// I don't think so, but if it can this would become a linker error
// anyway, not an invalid free at runtime.
Copy link
Member

Choose a reason for hiding this comment

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

With weak symbols it could be a runtime error (I'm dealing with this in the SM bindings + mozglue and it is so frustrating), but still memory safe.

This only applies to the Itanium C++ ABI, which pretty much means not windows, so we could if !cfg!(target_os = "windows") { ... } all this stuff.

Copy link
Member

Choose a reason for hiding this comment

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

Also, we could parse the symbol (using my cpp_demangle crate) and check if it is the a deleting destructor or not.

https://docs.rs/cpp_demangle/0.2.1/cpp_demangle/ast/enum.CtorDtorName.html

I would just need to expose a getter for a parsed symbol's AST.

How formal and proper do we want to make these hacks? ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem of cfg! checks is basically #593, so I'd prefer to remove the existing ones instead of adding newer ones.

@emilio emilio force-pushed the destructor-codegen branch from f5188a6 to 0dc7bcd Compare April 3, 2017 23:05
@emilio
Copy link
Contributor Author

emilio commented Apr 3, 2017

Instead of squashing, I moved the symbol hack to the beginning of the PR (GitHub stills shows it chronologically, which sucks, but it's the parent of all-but-the-first commit). That should preserve bisectability.

I've left a note about cpp_demangle.

@bors-servo r=fitzgen

@bors-servo
Copy link

📌 Commit 1f53966 has been approved by fitzgen

@bors-servo
Copy link

⌛ Testing commit 1f53966 with merge 5e85271...

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.
@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: fitzgen
Pushing 5e85271 to master...

@bors-servo bors-servo merged commit 1f53966 into rust-lang:master Apr 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants