Skip to content

[BUG] mojo compilation segfault, metaprogramming program #5727

@robbwu

Description

@robbwu

Bug description

Actual behavior

Compiling this program

def main():
    comptime K = 32
    # We want to loop from 1 up to K/2 (e.g., 1, 2, 4, 8, 16)

    # 1. Define the recursive compile-time function
    fn unroll_pow2[limit: Int, current: Int = 1]():
        @parameter
        if current <= limit:
            # --- Your Loop Body Here ---
            print(current)
            # ---------------------------

            # Recursive step: double the current value
            unroll_pow2[limit, current * 2]()

    # 2. Invoke the loop
    # Since 'K' is known at compile time, this unrolls completely.
    unroll_pow2[K // 2, 1]()

When compiling or running it segfaults:

$ mojo build crash.mojo
Please submit a bug report to https://github.com/modular/modular/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.      Program arguments: mojo build crash.mojo
[13424:13425:20260102,101830.175431:ERROR directory_reader_posix.cc:42] opendir /home/pwu/tmp/mojoprobe/.pixi/envs/default/share/max/crashdb/attachments/9c48f34b-cdb0-414b-9c9f-beacb77fad7e: No such file or directory (2)
 #0 0x000065249a18b72b (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x7a8372b)
 #1 0x000065249a188f7a (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x7a80f7a)
 #2 0x000065249a18c2d7 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x7a842d7)
 #3 0x000073e44d242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00006524965b23c5 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3eaa3c5)
 #5 0x0000652496228f2a (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3b20f2a)
 #6 0x0000652496228f2a (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3b20f2a)
 #7 0x00006524965b2bbc (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3eaabbc)
 #8 0x00006524964ab126 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3da3126)
 #9 0x00006524962326df (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3b2a6df)
#10 0x000073e44d299ee8 __pthread_once_slow ./nptl/pthread_once.c:118:7
#11 0x00006524964ab4b7 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3da34b7)
#12 0x00006524964ab547 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3da3547)
#13 0x0000652496a91f0d (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x4389f0d)
#14 0x000065249758b21b (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x4e8321b)
#15 0x000065249758834d (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x4e8034d)
#16 0x0000652496484ec3 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3d7cec3)
#17 0x00006524964845f6 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3d7c5f6)
#18 0x000065249621e935 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3b16935)
#19 0x0000652496245e0b (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3b3de0b)
#20 0x000073e44d229d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#21 0x000073e44d229e40 call_init ./csu/../csu/libc-start.c:128:20
#22 0x000073e44d229e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#23 0x000065249621d7e5 (/home/pwu/tmp/mojoprobe/.pixi/envs/default/bin/mojo+0x3b157e5)
Segmentation fault (core dumped)

It crashes on macOS and Linux, for latest mojo nightly

$ mojo --version
Mojo 0.26.1.0.dev2026010205 (8f7596d8)

and stable and version 0.25.7 as well.

Expected behavior

No segfault in compilation

Steps to reproduce

  • Please paste or attach the .mojo files that are needed to reproduce the issue.
  • If you are able to reduce the reproduction steps to a small example, this would be much appreciated!
  • Please quote the command line you used to compile and/or run the example.
  • If you see a crash or an error message, please paste it in the "actual behavior" above.
  • If using the Godbolt / Compiler Explorer, share the example link.
  • Include anything else that might help us debug the issue. Your insights and observations are welcome!

System information

  • Provide the system information by running pixi info.
  • Provide version information for MAX (includes Mojo) by pasting the output of pixi list max.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssue needs to be routed/triaged to a particular team stillbugSomething isn't workingmojoIssues that are related to mojo

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions