Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

core.stdcpp.new_.cpp_delete unnecessarily requires destruction to be @nogc #3284

Merged
merged 2 commits into from
Dec 14, 2020

Conversation

n8sh
Copy link
Member

@n8sh n8sh commented Nov 23, 2020

No description provided.

@dlang-bot
Copy link
Contributor

dlang-bot commented Nov 23, 2020

Thanks for your pull request, @n8sh!

Bugzilla references

Auto-close Bugzilla Severity Description
21417 enhancement core.stdcpp.new_.cpp_delete unnecessarily requires destruction to be @nogc
21421 normal core.stdcpp.new_.cpp_delete does not work with classes

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "stable + druntime#3284"

@dlang-bot dlang-bot added the Enhancement New functionality label Nov 23, 2020
@@ -14,8 +14,6 @@ module core.stdcpp.new_;
import core.stdcpp.xutility : __cpp_sized_deallocation, __cpp_aligned_new;
import core.stdcpp.exception : exception;

@nogc:
Copy link
Member Author

Choose a reason for hiding this comment

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

Every function between here and where @nogc: is re-added is either a template or explicitly annotated @nogc.

Copy link
Contributor

@TurkeyMan TurkeyMan left a comment

Choose a reason for hiding this comment

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

Great catch!

@TurkeyMan
Copy link
Contributor

Is the parallel for construction also addressed here?
Also, are we confident that emplace and destroy will appropriately infer @nogc if the types they are interacting with have @nogc ctor/dtors?

@TurkeyMan
Copy link
Contributor

I'd like to see a unit test that demonstrates use with GC and also @nogc types so that we can prove inference is working properly.

@n8sh
Copy link
Member Author

n8sh commented Nov 24, 2020

Great catch!

Thanks but this wasn't a "catch" so much as something stumbled across naturally while trying to mix C++ and D!

@TurkeyMan
Copy link
Contributor

Same thing. It's great that you're using this.
I'd love to get some experience reports from you when you've has more time with it.

{
__gshared ulong numDeleted;
int x = 3;
~this() @nogc { ++numDeleted; }
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this class should test @nogc constructors too.

{
__gshared ulong numDeleted;
int x = 5;
~this() { if (++numDeleted < 0) throw new Exception("overflow"); }
Copy link
Contributor

Choose a reason for hiding this comment

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

No with-GC constructor test?

Copy link
Contributor

Choose a reason for hiding this comment

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

It should probably new something in the constructor...

MyClassNoGC c1 = cpp_new!MyClassNoGC(4);
assert(c1.x == 4);
assert(MyClassNoGC.numDeleted == 0);
cpp_delete(c1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems this doesn't infer @nogc. I guess destroy is not inferring correctly?

@n8sh n8sh force-pushed the issue-21417 branch 2 times, most recently from c7cdab6 to 440b1fd Compare December 12, 2020 01:27
Comment on lines 44 to 49
@nogc unittest
{
// TEST NOT COMPILING: compiler can't tell dtor of MyClassNoGC is @nogc.
if (0)
MyClassNoGC.init.__xdtor();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The problem wasn't in destroy it's here:

test\stdcpp\src\new_test.d(48): Error: @nogc function new_test.__unittest_L44_C7 cannot call non-@nogc destructor new_test.MyClassNoGC.~this

Copy link
Contributor

Choose a reason for hiding this comment

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

__xdtor is not the destructor; that's actually a function synthesised by the compiler.
Maybe there's a compiler bug where that function should be inferring some attributes but it's not?
It will also execute all the field destructors, and the super destructor, so if any of those are not @nogc, then that may be breaking the inference...?

@n8sh
Copy link
Member Author

n8sh commented Dec 14, 2020

Updated tests to test @nogc GC inference for struct instead of class. Making cpp_delete infer correct attributes for classes is left as an item for a future PR. As prior to this PR cpp_delete could not be used with classes at all there is no regression.

@dlang-bot dlang-bot merged commit 9a084ed into dlang:stable Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Enhancement New functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants