Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 17a41fc

Browse files
committedMar 11, 2025·
Fix issue #4184: The spec should warn about using copy constructors.
Until dlang/dmd#20970 is fixed, we need to be warning against the use of copy constructors rather than warning against the use of postblit constructors. So, I fixed the warning and put both it and the description of what happens when mixing postblit constructors and copy constructors together on both the section for postblit constructors and the one for copy constructors. I suspect that move constructors merit a similar warning, but I'm not sure what their current state is, so this just fixes the warning for postblit constructors and copy constructors.
1 parent 508ad62 commit 17a41fc

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed
 

‎spec/struct.dd

+39-8
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,26 @@ $(H3 $(LNAME2 field-init, Field initialization inside a constructor))
11061106

11071107
$(H2 $(LEGACY_LNAME2 StructCopyConstructor, struct-copy-constructor, Struct Copy Constructors))
11081108

1109+
$(P $(RED Warning:) The plan is for
1110+
$(RELATIVE_LINK2 struct-copy-constructor, copy constructors) to replace
1111+
$(RELATIVE_LINK2 struct-postblit, postblit constructors), leaving postblit
1112+
constructors only in legacy code. However, because the compiler hooks in
1113+
druntime which deal with dynamic arrays and associative arrays have not yet
1114+
all been updated to support copy constructors properly
1115+
($(HTTPS github.com/dlang/dmd/issues/20970, issue #20970)), any type that might
1116+
be used in a dynamic array or associative array should use a postblit
1117+
constructor rather than a copy constructor. The copy constructor will not
1118+
be called in all the cases where it should be for the elements of a dynamic
1119+
array or the keys or values in an associative array. Postblit constructors
1120+
do not have this problem.)
1121+
1122+
For backward compatibility reasons, a `struct` that explicitly defines both
1123+
a copy constructor and a postblit will only use the postblit for implicit
1124+
copying. However, if the postblit is disabled, the copy constructor will be
1125+
used. If a struct defines a copy constructor (user-defined or generated)
1126+
and has fields that define postblits, a deprecation will be issued,
1127+
informing that the postblit will have priority over the copy constructor.)
1128+
11091129
$(P Copy constructors are used to initialize a `struct` instance from
11101130
another instance of the same type. A `struct` that defines a copy constructor
11111131
is not $(RELATIVE_LINK2 POD, POD).)
@@ -1365,14 +1385,25 @@ $(GNAME Postblit):
13651385
$(D this $(LPAREN) this $(RPAREN)) $(GLINK2 function, MemberFunctionAttributes)$(OPT) $(GLINK2 function, FunctionBody)
13661386
)
13671387

1368-
$(P $(RED Warning): The postblit is considered legacy and is not recommended for new code.
1369-
Code should use $(RELATIVE_LINK2 struct-copy-constructor, copy constructors)
1370-
defined in the previous section. For backward compatibility reasons, a `struct` that
1371-
explicitly defines both a copy constructor and a postblit will only use the postblit
1372-
for implicit copying. However, if the postblit is disabled, the copy constructor will
1373-
be used. If a struct defines a copy constructor (user-defined or generated) and has
1374-
fields that define postblits, a deprecation will be issued, informing that the postblit
1375-
will have priority over the copy constructor.)
1388+
$(P $(RED Warning:) The plan is for
1389+
$(RELATIVE_LINK2 struct-copy-constructor, copy constructors) to replace
1390+
$(RELATIVE_LINK2 struct-postblit, postblit constructors), leaving postblit
1391+
constructors only in legacy code. However, because the compiler hooks in
1392+
druntime which deal with dynamic arrays and associative arrays have not yet
1393+
all been updated to support copy constructors properly
1394+
($(HTTPS github.com/dlang/dmd/issues/20970, issue #20970)), any type that might
1395+
be used in a dynamic array or associative array should use a postblit
1396+
constructor rather than a copy constructor. The copy constructor will not
1397+
be called in all the cases where it should be for the elements of a dynamic
1398+
array or the keys or values in an associative array. Postblit constructors
1399+
do not have this problem.)
1400+
1401+
For backward compatibility reasons, a `struct` that explicitly defines both
1402+
a copy constructor and a postblit will only use the postblit for implicit
1403+
copying. However, if the postblit is disabled, the copy constructor will be
1404+
used. If a struct defines a copy constructor (user-defined or generated)
1405+
and has fields that define postblits, a deprecation will be issued,
1406+
informing that the postblit will have priority over the copy constructor.)
13761407

13771408
$(P $(I Copy construction) is defined as initializing
13781409
a struct instance from another instance of the same type.

0 commit comments

Comments
 (0)
Please sign in to comment.