Skip to content

Commit e6e5910

Browse files
authoredJan 29, 2021
Clarify key_value_swap's edge case (#10799)
In compiler design especially, leaving behavior as "undefined" is a _strong_ condition that basically states that all possible integrity is violated; it's the kind of thing that happens when, say, dereferencing a pointer with unknown provenance, and most typically leads to a crash, but can result in all sorts of spectacular errors--thus, "it is undefined" how your program will melt down. The pure-Lua implementation of `key_value_swap` does not permit UB _per se_ (assuming the implementation of Lua itself is sound), but does deterministically choose the value to which a key is mapped (the last in visitation order wins--since visitation order is arbitrary, _some_ value _will_ be chosen). Most importantly, the program won't do something wildly unexpected.
1 parent 2760371 commit e6e5910

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed
 

Diff for: ‎doc/lua_api.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -3275,7 +3275,8 @@ Helper functions
32753275
* Appends all values in `other_table` to `table` - uses `#table + 1` to
32763276
find new indices.
32773277
* `table.key_value_swap(t)`: returns a table with keys and values swapped
3278-
* If multiple keys in `t` map to the same value, the result is undefined.
3278+
* If multiple keys in `t` map to the same value, it is unspecified which
3279+
value maps to that key.
32793280
* `table.shuffle(table, [from], [to], [random_func])`:
32803281
* Shuffles elements `from` to `to` in `table` in place
32813282
* `from` defaults to `1`

0 commit comments

Comments
 (0)
Please sign in to comment.