modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.27k stars 2.59k forks source link

[BUG] Using a default value for a List of functions attribute of a struct leads to an OOB error upon access. #3285

Open thatstoasty opened 3 months ago

thatstoasty commented 3 months ago

Bug description

Given a Struct like

struct A():
    var funcs: List[fn() -> None]

Specifying a default argument value for funcs in the __init__() function signature leads to an OOB error when attempting to access the functions.

Steps to reproduce

Example

fn test() -> None:
    return None

struct Dummy():
    var funcs: List[fn() -> None]

    fn __init__(inout self, funcs: List[fn() -> None] = List[fn() -> None](test)):
        self.funcs = funcs

fn main() raises:
    var d = Dummy()
    for func in d.funcs:
        func[]()

System information

- What OS did you do install Mojo on ? MacOS 14.5
- Provide version information for Mojo by pasting the output of `mojo -v` mojo 2024.7.2205 (9b0e6242)
- Provide Modular CLI version by pasting the output of `modular -v` modular 0.7.1 (28ddab26)
thatstoasty commented 3 months ago

Possibly related to https://github.com/modularml/mojo/issues/3126, but this issue still persists. It seems like it has something to do with compile time. When the List of functions is given as a default argument, the pointers to the functions show up as 0x0 when inspecting the List in the debugger. But they only show up as 0x0 when iterating over the list, the pointer addresses look correct up until that point.