@@ -32,3 +32,106 @@ unittest
32
32
s.c = cast (MyStruct* )__cpp_new(MyStruct.sizeof);
33
33
cpp_delete(s);
34
34
}
35
+
36
+ @nogc unittest
37
+ {
38
+ // Test cpp_new and cpp_delete for a struct infer @nogc.
39
+ import core.stdcpp.new_ : cpp_new, cpp_delete;
40
+ extern (C++ ) static struct MyStructNoGC
41
+ {
42
+ __gshared int numDeleted;
43
+ int x = 3 ;
44
+ this (int x) @nogc { this .x = x; }
45
+ ~this () @nogc { ++ numDeleted; }
46
+ }
47
+
48
+ MyStructNoGC* c1 = cpp_new! MyStructNoGC(4 );
49
+ assert (c1.x == 4 );
50
+ assert (MyStructNoGC.numDeleted == 0 );
51
+ cpp_delete(c1);
52
+ assert (MyStructNoGC.numDeleted == 1 );
53
+ }
54
+
55
+ /+
56
+ // BUG: @nogc not being applied to __xdtor for extern(C++) class.
57
+ extern(C++) class MyClassNoGC
58
+ {
59
+ __gshared int numDeleted;
60
+ int x = 3;
61
+ this(int x) @nogc { this.x = x; }
62
+ ~this() @nogc { ++numDeleted; }
63
+ }
64
+
65
+ @nogc unittest
66
+ {
67
+ // Test cpp_new and cpp_delete for a class infer @nogc.
68
+ import core.stdcpp.new_: cpp_new, cpp_delete;
69
+
70
+ MyClassNoGC c1 = cpp_new!MyClassNoGC(4);
71
+ assert(c1.x == 4);
72
+ assert(MyClassNoGC.numDeleted == 0);
73
+ cpp_delete(c1);
74
+ assert(MyClassNoGC.numDeleted == 1);
75
+ }
76
+ +/
77
+
78
+ unittest
79
+ {
80
+ import core.stdcpp.new_ : cpp_new, cpp_delete;
81
+
82
+ // Test cpp_new & cpp_delete are callable with a struct whose destructor
83
+ // is not @nogc.
84
+ {
85
+ extern (C++ ) static struct MyStructGC
86
+ {
87
+ __gshared int numDeleted;
88
+ int x = 5 ;
89
+ this (int x)
90
+ {
91
+ if (x == int .min)
92
+ throw new Exception (" forbidden number" );
93
+ this .x = x;
94
+ }
95
+ ~this ()
96
+ {
97
+ if (++ numDeleted < 0 )
98
+ throw new Exception (" overflow in dtor" );
99
+ }
100
+ }
101
+ static assert (! is (typeof (() @nogc => cpp_new! MyStructGC(6 ))));
102
+ MyStructGC* c2 = cpp_new! MyStructGC(6 );
103
+ assert (c2.x == 6 );
104
+ static assert (! is (typeof (() @nogc => cpp_delete(c2))));
105
+ assert (MyStructGC.numDeleted == 0 );
106
+ cpp_delete(c2);
107
+ assert (MyStructGC.numDeleted == 1 );
108
+ }
109
+
110
+ // Test cpp_new & cpp_delete are callable with a class whose destructor
111
+ // is not @nogc.
112
+ {
113
+ extern (C++ ) static class MyClassGC
114
+ {
115
+ __gshared int numDeleted;
116
+ int x = 5 ;
117
+ this (int x)
118
+ {
119
+ if (x == int .min)
120
+ throw new Exception (" forbidden number x" );
121
+ this .x = x;
122
+ }
123
+ ~this ()
124
+ {
125
+ if (++ numDeleted < 0 )
126
+ throw new Exception (" overflow in dtor for x" );
127
+ }
128
+ }
129
+ static assert (! is (typeof (() @nogc => cpp_new! MyClassGC(6 ))));
130
+ MyClassGC c2 = cpp_new! MyClassGC(6 );
131
+ assert (c2.x == 6 );
132
+ static assert (! is (typeof (() @nogc => cpp_delete(c2))));
133
+ assert (MyClassGC.numDeleted == 0 );
134
+ cpp_delete(c2);
135
+ assert (MyClassGC.numDeleted == 1 );
136
+ }
137
+ }
0 commit comments