Skip to content

Commit b558a9e

Browse files
Merge pull request #19524 from dlongnecke-cray/domain-unsafe-assign-docs
Adjust the wording of `unsafeAssign()` method docs (#19524) Reword the docs for the `unsafeAssign()` method as well as the description of the `unsafeAssignManager` type and its `initialize()` method. While here, also "no doc" the `isClassReferenceNil` method and mark the `checks` method as returning `param`. Reviewed by @vasslitvinov. Thanks! Signed-off-by: David Longnecker <[email protected]>
2 parents 28c0a24 + 6ff887f commit b558a9e

File tree

1 file changed

+70
-26
lines changed

1 file changed

+70
-26
lines changed

modules/internal/ChapelDomain.chpl

+70-26
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,28 @@ module ChapelDomain {
13571357
}
13581358

13591359
/*
1360-
This manager is returned by ``unsafeAssign()`` and can be used in
1361-
managed blocks to help resize arrays of non-default-initializable
1362-
elements.
1360+
An instance of this type is a context manager that can be used in
1361+
manage statements to resize arrays of non-default-initializable
1362+
element types after resizing their underlying domain.
1363+
1364+
Using an instance of this type in a manage statement will cause a
1365+
domain assignment to occur before executing the statement body. The
1366+
left-hand-side of the assignment is the receiver domain that had
1367+
``unsafeAssign()`` called on it, while the right-hand-side is the
1368+
`dom` formal of the same call.
1369+
1370+
If the assignment adds new indices to the assigned domain, then
1371+
corresponding elements are added to arrays declared over it.
1372+
If an array's element type is non-default-initializable, then any
1373+
newly added elements remain uninitialized.
1374+
1375+
The ``initialize()`` method can be used within the manage statement
1376+
body to initialize new elements of non-default-initializable arrays
1377+
declared over the assigned domain.
1378+
1379+
The new elements of default-initializable arrays over the assigned
1380+
domain will be default-initialized. They can be set to desired
1381+
values as usual, for example using an assignment operator.
13631382
*/
13641383
record unsafeAssignManager {
13651384
pragma "no doc"
@@ -1406,11 +1425,11 @@ module ChapelDomain {
14061425
}
14071426

14081427
/*
1409-
Returns 'true' if this manager has runtime safety checks enabled.
1428+
Returns ``true`` if this manager has runtime safety checks enabled.
14101429
*/
1411-
inline proc checks return _checks;
1430+
inline proc checks param return _checks;
14121431

1413-
// Type overload. Not documented, but provided for convenience.
1432+
// Called by implementation code.
14141433
pragma "no doc"
14151434
proc type isClassReferenceNil(const ref x) {
14161435
if isClassType(x.type) {
@@ -1422,10 +1441,8 @@ module ChapelDomain {
14221441
}
14231442
}
14241443

1425-
/*
1426-
Check if a given class reference is ``nil`` without triggering
1427-
runtime nilability checks.
1428-
*/
1444+
// TODO: Make 'nonNilClass == nil' avoid runtime nil checks.
1445+
pragma "no doc"
14291446
proc isClassReferenceNil(const ref x) {
14301447
return this.type.isClassReferenceNil(x);
14311448
}
@@ -1530,6 +1547,7 @@ module ChapelDomain {
15301547
}
15311548
}
15321549

1550+
pragma "no doc"
15331551
proc deinit() {
15341552
_ensureNoLongerManagingThis();
15351553
}
@@ -1562,12 +1580,12 @@ module ChapelDomain {
15621580
/*
15631581
Initialize a newly added array element at an index with a new value.
15641582
1565-
If checks is ``true`` and the array element at `idx` has already
1566-
been initialized, this method will halt. If checks is ``false``,
1567-
then this method will overwrite the memory at `arr[idx]` in a
1568-
potentially unsafe manner.
1583+
If `checks` is ``true`` and the array element at `idx` has already
1584+
been initialized, this method will halt. If `checks` is ``false``,
1585+
then calling this method on an already initialized element will
1586+
result in undefined behavior.
15691587
1570-
It is an error if `idx` is not a valid indice in `arr`.
1588+
It is an error if `idx` is not a valid index in `arr`.
15711589
*/
15721590
proc initialize(arr: [?d], idx, in value: arr.eltType) {
15731591

@@ -1685,21 +1703,47 @@ module ChapelDomain {
16851703
}
16861704

16871705
/*
1688-
Perform an unsafe assignment on this domain within the scope of a
1689-
manage statement. Within the managed scope, arrays of non-default-
1690-
initializable (e.g., an array of non-nilable classes) types
1691-
declared over this domain will not initialize new elements.
1706+
Returns an instance of :type:`unsafeAssignManager`.
1707+
1708+
The returned context manager can be used in a manage statement to
1709+
assign the indices of `dom` into the receiver domain. Within the body
1710+
of the manage statement, the manager can initialize elements of
1711+
non-default-initializable arrays declared over the receiver domain.
1712+
1713+
If `checks` is ``true``, this method will guarantee that:
1714+
1715+
- Newly added elements of any non-default-initializable arrays
1716+
declared over the receiver domain have been initialized by the
1717+
end of the manage statement
1718+
- Newly added elements are only initialized once
16921719
1693-
The context manager returned by this method can be used to initialize
1694-
arrays of non-default-initializable elements. If `checks` is true,
1695-
the manager will ensure that all new elements have been initialized
1696-
in non-default-initializable arrays declared over this domain.
1720+
These guarantees hold only for initialization done through calls to
1721+
the ``initialize()`` method on the context manager. Performing
1722+
any other operation on a newly added array element causes undefined
1723+
behavior until after ``initialize()`` has been called.
1724+
1725+
For example:
1726+
1727+
.. code-block:: chapel
1728+
1729+
var D = {0..0};
1730+
var A: [D] shared C = [new shared C(0)];
1731+
manage D.unsafeAssign({0..1}, checks=true) as mgr {
1732+
// 'D' has a new index '1', so 'A' has a new element at '1',
1733+
// which we need to initialize:
1734+
mgr.initialize(A, 1, new shared C(1));
1735+
}
16971736
16981737
.. note::
16991738
1700-
Checks are not currently supported for arrays with
1701-
non-default-initializable element types that are not non-nilable
1702-
classes.
1739+
Checks are not currently supported for arrays of
1740+
non-default-initializable element types other than arrays of
1741+
non-nilable classes.
1742+
1743+
:arg dom: The domain to assign to the receiver
1744+
:arg checks: If this manager should provide runtime safety checks
1745+
1746+
:returns: A :type:`unsafeAssignManager` for use in manage statements
17031747
*/
17041748
proc ref unsafeAssign(const ref dom: domain, param checks: bool=false) {
17051749
return new unsafeAssignManager(_lhsInstance=_value,

0 commit comments

Comments
 (0)