modular / modular/modular

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

Open
#3,285 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug mojo mojo-repo
Dominant language
Mojo
Stars
29.8k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Mojo reproduction with the shown Dummy struct, default List value, and main entry point. Compare access to d.funcs when using Dummy() with the behavior when the list is passed explicitly. Done means the default-valued List of functions can be iterated and its functions invoked without an out-of-bounds error.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.