16588, “ronawho”, “Update Map documentation to note that modifying a map while iterating is illegal”, “2020-10-15T17:43:54Z”
Removing from or adding to a Map (and set and probably other types) while iterating over it is illegal. The following example is invalid, but will generally work since we won’t end up resizing the underlying data structure for this particular example:
use Map;
var m: map(string, int);
m.add("one", 1); m.add("two", 2);
for k in m {
if k == "one" { m.remove(k); }
if k == "two" { m.add("three", 3); }
}
writeln(m);
I think we need to make the map (and friends) documentation more clear that modifications while iterating are invalid. I also wonder if we can add a check for this if bounds checking is enabled. Something like the following (incomplete) checking seems to work:
diff --git a/modules/standard/Map.chpl b/modules/standard/Map.chpl
index 536fda7e08..ce197df2c5 100644
--- a/modules/standard/Map.chpl
+++ b/modules/standard/Map.chpl
@@ -76,6 +76,8 @@ module Map {
pragma "no doc"
var _lock$ = if parSafe then new _LockWrapper() else none;
+ var iterating: if boundsChecking then bool else none;