Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Runtime crash when looping over polymorphic map that uses a struct as a key

Open EeroMutka opened this issue 3 years ago • 1 comments
trafficstars

The following code causes an access violation at second iteration of the for loop:

package main
import "core:fmt"

loop :: proc(m: map[$T]int) {
	for x, i in m {
		println(x)
	}
}

Key :: struct {
	a, b: int,
}

main :: proc() {
	foo: map[Key]int
	map_insert(&foo, Key{2, 5}, 1)
	map_insert(&foo, Key{3, 4}, 2)

	loop(foo)

	fmt.println("Hello")
}

Interestingly, changing the argument to m: $T/map[$K]int fixes the problem

Odin: dev-2022-04:d10d5471 OS: Windows 10 Home Basic (version: 21H2), build 19044.1586 CPU: AMD Ryzen 5 5600H with Radeon Graphics RAM: 16252 MiB

EeroMutka avatar Apr 02 '22 19:04 EeroMutka

This issue appears to be still reproducible on the latest Odin compiler, and the "workaround" of m: $T/map[$K]int still applies.

z64 avatar Sep 03 '22 10:09 z64