odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.89k stars 606 forks source link

Printing element of self-containing map causes infinite recursion #2795

Open korvahkh opened 1 year ago

korvahkh commented 1 year ago

Running the following code:

package main

import "core:fmt"

T :: map[string]T

main :: proc() {
    t := T{"m" = T{}}
    fmt.println(t["m"])
}

results in an infinite recursion between fmt.fmt_value and fmt.fmt_named until the stack overflows.

It looks like this is caused by a bug in type info generation, where T's base type is indicated as being itself.

Note that this behavior is rather volatile, and seemingly insignificant changes cause this not to happen, for example:

main :: proc() {
    t := T{"m" = T{}}
    fmt.println(t)
    fmt.println(t["m"])
}

works as expected.

gingerBill commented 1 year ago

This bug is caused by the frontend and should result in a cycle, but for some reason it is not being checked. For that to work, it would need to be T :: distinct map[string]T IF that even makes any sense to begin with.