@@ -1357,9 +1357,28 @@ module ChapelDomain {
1357
1357
}
1358
1358
1359
1359
/*
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.
1363
1382
*/
1364
1383
record unsafeAssignManager {
1365
1384
pragma " no doc"
@@ -1406,11 +1425,11 @@ module ChapelDomain {
1406
1425
}
1407
1426
1408
1427
/*
1409
- Returns ' true' if this manager has runtime safety checks enabled.
1428
+ Returns `` true`` if this manager has runtime safety checks enabled.
1410
1429
*/
1411
- inline proc checks return _checks;
1430
+ inline proc checks param return _checks;
1412
1431
1413
- // Type overload. Not documented, but provided for convenience .
1432
+ // Called by implementation code .
1414
1433
pragma " no doc"
1415
1434
proc type isClassReferenceNil (const ref x) {
1416
1435
if isClassType(x.type ) {
@@ -1422,10 +1441,8 @@ module ChapelDomain {
1422
1441
}
1423
1442
}
1424
1443
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"
1429
1446
proc isClassReferenceNil (const ref x) {
1430
1447
return this .type .isClassReferenceNil(x);
1431
1448
}
@@ -1530,6 +1547,7 @@ module ChapelDomain {
1530
1547
}
1531
1548
}
1532
1549
1550
+ pragma " no doc"
1533
1551
proc deinit () {
1534
1552
_ensureNoLongerManagingThis();
1535
1553
}
@@ -1562,12 +1580,12 @@ module ChapelDomain {
1562
1580
/*
1563
1581
Initialize a newly added array element at an index with a new value.
1564
1582
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 .
1569
1587
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`.
1571
1589
*/
1572
1590
proc initialize (arr: [ ?d] , idx, in value: arr.eltType) {
1573
1591
@@ -1685,21 +1703,47 @@ module ChapelDomain {
1685
1703
}
1686
1704
1687
1705
/*
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
1692
1719
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
+ }
1697
1736
1698
1737
.. note::
1699
1738
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
1703
1747
*/
1704
1748
proc ref unsafeAssign (const ref dom: domain , param checks: bool = false ) {
1705
1749
return new unsafeAssignManager(_lhsInstance= _value,
0 commit comments