Skip to content

Commit 83a1aa6

Browse files
authored
Merge pull request #2515 from radcapricorn/fix19321
Fix issue 19321 merged-on-behalf-of: Jacob Carlborg <jacob-carlborg@users.noreply.github.com>
2 parents db18006 + ab107b5 commit 83a1aa6

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

spec/struct.dd

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,36 @@ void main()
12201220
---
12211221
)
12221222

1223-
$(P Unions may not have fields that have postblits.)
1223+
$(P Unions may have fields that have postblits. However, a union itself never has
1224+
a postblit. Copying a union does not result in postblit calls for any fields.
1225+
If those calls are desired, they must be inserted explicitly by the programmer:)
1226+
1227+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1228+
---
1229+
struct S
1230+
{
1231+
int count;
1232+
this(this)
1233+
{
1234+
++count;
1235+
}
1236+
}
1237+
1238+
union U
1239+
{
1240+
S s;
1241+
}
1242+
1243+
void main()
1244+
{
1245+
U a = U.init;
1246+
U b = a;
1247+
assert(b.s.count == 0);
1248+
b.s.__postblit;
1249+
assert(b.s.count == 1);
1250+
}
1251+
---
1252+
)
12241253

12251254
$(H2 $(LEGACY_LNAME2 StructDestructor, struct-destructor, Struct Destructors))
12261255

@@ -1229,7 +1258,46 @@ $(H2 $(LEGACY_LNAME2 StructDestructor, struct-destructor, Struct Destructors))
12291258
object.
12301259
)
12311260

1232-
$(P Unions may not have fields that have destructors.)
1261+
$(P Unions may have fields that have destructors. However, a union itself never has
1262+
a destructor. When a union goes out of scope, destructors for it's fields are not called.
1263+
If those calls are desired, they must be inserted explicitly by the programmer:)
1264+
1265+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1266+
---
1267+
struct S
1268+
{
1269+
~this()
1270+
{
1271+
import std.stdio;
1272+
writeln("S is being destructed");
1273+
}
1274+
}
1275+
1276+
union U
1277+
{
1278+
S s;
1279+
}
1280+
1281+
void main()
1282+
{
1283+
import std.stdio;
1284+
{
1285+
writeln("entering first scope");
1286+
U u = U.init;
1287+
scope (exit) writeln("exiting first scope");
1288+
}
1289+
{
1290+
writeln("entering second scope");
1291+
U u = U.init;
1292+
scope (exit)
1293+
{
1294+
writeln("exiting second scope");
1295+
destroy(u.s);
1296+
}
1297+
}
1298+
}
1299+
---
1300+
)
12331301

12341302
$(H2 $(LNAME2 StructInvariant, Struct Invariants))
12351303

0 commit comments

Comments
 (0)