compiler: fix ranging over maps with particular map types

Some map keys are hard to compare, such as floats. They are stored as if
the map keys are of interface type instead of the key type itself. This
makes working with them in the runtime package easier: they are compared
as regular interfaces.

Iterating over maps didn't care about this special case though. It just
returns the key, value pair as it is stored in the map. This is buggy,
and this commit fixes this bug.
This commit is contained in:
Ayke van Laethem
2021-12-08 00:01:04 +01:00
committed by Ron Evans
parent 449bfe04f3
commit b13c993565
4 changed files with 69 additions and 10 deletions
+10 -1
View File
@@ -80,15 +80,24 @@ func main() {
println("itfMap[true]:", itfMap[true])
delete(itfMap, 8)
println("itfMap[8]:", itfMap[8])
for key, value := range itfMap {
if key == "eight" {
println("itfMap: found key \"eight\":", value)
}
}
// test map with float keys
floatMap := map[float32]int{
42: 84,
42: 84,
3.14: 6,
}
println("floatMap[42]:", floatMap[42])
println("floatMap[43]:", floatMap[43])
delete(floatMap, 42)
println("floatMap[42]:", floatMap[42])
for k, v := range floatMap {
println("floatMap key, value:", k, v)
}
// test maps with struct keys
structMap := map[namedFloat]int{
+2
View File
@@ -63,9 +63,11 @@ itfMap["eight"]: 800
itfMap[[2]int{5, 2}]: 52
itfMap[true]: 1
itfMap[8]: 0
itfMap: found key "eight": 800
floatMap[42]: 84
floatMap[43]: 0
floatMap[42]: 0
floatMap key, value: +3.140000e+000 6
structMap[{"tau", 6.28}]: 5
structMap[{"Tau", 6.28}]: 0
structMap[{"tau", 3.14}]: 0