modularml / mojo

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

[BUG]: Crash passing parameterized function to Tuple #1893

Open Benny-Nottonson opened 7 months ago

Benny-Nottonson commented 7 months ago

Bug description

Parser throws the issue cannot use parameterized function without binding all its parameters, but assigning a parameter or indicating it is unbound crashes the compiler.

Steps to reproduce

struct Node[T: DType, B: Int, C: Int]:
    ...

@register_passable("trivial")
struct Add:
    @staticmethod
    fn fw[T: DType](node: Node[T, *_], parent: Node[T, *_]):
        ...

    @staticmethod
    fn bw[T: DType](node: Node[T, *_], parent: Node[T, *_]):
        ...

alias UnaryFn = fn [T: DType](node: Node[T, *_], parent: Node[T, *_]) -> None
alias BinaryFn = fn [T: DType](node: Node[T, *_], parent1: Node[T, *_], parent2: Node[T, *_]) -> None
alias Op_Tuple = Tuple[UnaryFn, BinaryFn]

@register_passable("trivial")
struct KERNELS:
    @staticmethod
    fn get(code: Int) -> Op_Tuple:
        if code == 0:
            return Op_Tuple(Add.fw[DType.float32], _)

System information

Mojo v24.1.0, Modular v0.5.1, Ubuntu 22.04 WSL
junweizeng commented 7 months ago

Here's the reduced code that can reproduce the crash.

Steps to reproduce:

$ cat test.mojo
struct a[b: DType, c: Int]:
    pass

struct d:
    @staticmethod
    fn e(f: a) :
         d.e[DType.float32]
$
$ mojo build test.mojo
parameter deduction failure in a context that doesn't allow deduction
UNREACHABLE executed at /__w/modular/modular/KGEN/lib/MojoParser/CallEmission.cpp:622!
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.      Program arguments: mojo build test.mojo
1.      Crash resolving decl body at loc("/path/to/test.mojo":6:5)
    >>     fn e(f: a) :
           ^...........
    >>          d.e[DType.float32]
       ...........................<
2.      Crash parsing statement at loc("/path/to/test.mojo":7:10)
    >>          d.e[DType.float32]
                ^.................<
 #0 0x00005624f4ab6e47 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x62ae47)
 #1 0x00005624f4ab4a1e (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x628a1e)
 #2 0x00005624f4ab751f (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x62b51f)
 #3 0x00007ff178951520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007ff1789a5a7c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007ff1789a5a7c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007ff1789a5a7c pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007ff178951476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007ff1789377f3 abort ./stdlib/abort.c:81:7
 #9 0x00005624f4a9194f (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x60594f)
#10 0x00005624f4f67be9 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xadbbe9)
#11 0x00005624f4f5d714 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xad1714)
#12 0x00005624f4f5ea29 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xad2a29)
#13 0x00005624f4f61130 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xad5130)
#14 0x00005624f4f60f6c (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xad4f6c)
#15 0x00005624f4f61f72 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xad5f72)
#16 0x00005624f4f620af (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xad60af)
#17 0x00005624f4efe109 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa72109)
#18 0x00005624f4eff57c (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa7357c)
#19 0x00005624f4f07790 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa7b790)
#20 0x00005624f4f3d1b0 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xab11b0)
#21 0x00005624f4f37558 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xaab558)
#22 0x00005624f4f37406 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xaab406)
#23 0x00005624f4ed907a (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa4d07a)
#24 0x00005624f4ef28b4 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa668b4)
#25 0x00005624f4ef2fc8 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa66fc8)
#26 0x00005624f4ea0cfb (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa14cfb)
#27 0x00005624f4ea1059 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa15059)
#28 0x00005624f4a5fe0f (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5d3e0f)
#29 0x00005624f4a6223b (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5d623b)
#30 0x00005624f4a5cddf (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5d0ddf)
#31 0x00005624f4a5b65d (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5cf65d)
#32 0x00007ff178938d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#33 0x00007ff178938e40 call_init ./csu/../csu/libc-start.c:128:20
#34 0x00007ff178938e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#35 0x00005624f4a5afae (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5cefae)
Aborted
lattner commented 6 months ago

Verified, this still fails on head:

parameter deduction failure in a context that doesn't allow deduction
UNREACHABLE executed at /Users/clattner/Projects/modular/KGEN/lib/MojoParser/CallEmission.cpp:571!