modularml / mojo

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

[BUG]: mojo crash: Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed. #1865

Open junweizeng opened 7 months ago

junweizeng commented 7 months ago

Bug description

mojo crashes on the following code (i.e. test.mojo).

fn a for b in c struct d

Steps to reproduce

$ mojo build test.mojo
/path/to/test.mojo:1:6: error: expected '(' for argument list
fn a for b in c struct d
     ^
/path/to/test.mojo:1:6: error: 'for' statement must be on its own line
fn a for b in c struct d
     ^
/path/to/test.mojo:1:17: error: 'struct' statement must be on its own line
fn a for b in c struct d
                ^
/path/to/test.mojo:1:17: error: struct inside a function not supported here
fn a for b in c struct d
                ^
mojo: /__w/modular/modular/third-party/llvm-project/llvm/include/llvm/Support/Casting.h:650: decltype(auto) llvm::dyn_cast(const From &) [To = M::KGEN::LIT::PValue, From = llvm::PointerUnion<mlir::Operation *, M::KGEN::LIT::PValue, M::KGEN::LIT::SRValue, M::KGEN::LIT::SBValue, M::KGEN::LIT::MBValue, M::KGEN::LIT::MRValue, M::KGEN::LIT::MLValue>]: Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed.
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":1:1)
    >> fn a for b in c struct d
       ^.......................
    >> 
       <
2.      Crash parsing statement at loc("/path/to/test.mojo":1:6)
    >> fn a for b in c struct d
            ^..................
    >> 
       <
3.      Crash parsing statement at loc("/path/to/test.mojo":1:17)
    >> fn a for b in c struct d
                       ^.......
    >> 
       <
 #0 0x000055e4765c8e47 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x62ae47)
 #1 0x000055e4765c6a1e (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x628a1e)
 #2 0x000055e4765c951f (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x62b51f)
 #3 0x00007f3a882e8520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f3a8833ca7c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007f3a8833ca7c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007f3a8833ca7c pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007f3a882e8476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007f3a882ce7f3 abort ./stdlib/abort.c:81:7
 #9 0x00007f3a882ce71b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007f3a882dfe96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x000055e4769cf4dd (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa314dd)
#12 0x000055e476a02c28 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa64c28)
#13 0x000055e476a4e272 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xab0272)
#14 0x000055e476a49657 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xaab657)
#15 0x000055e476a54158 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xab6158)
#16 0x000055e476a53238 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xab5238)
#17 0x000055e476a49657 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xaab657)
#18 0x000055e476a49406 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xaab406)
#19 0x000055e4769eb07a (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa4d07a)
#20 0x000055e476a048b4 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa668b4)
#21 0x000055e476a04fc8 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa66fc8)
#22 0x000055e4769b2cfb (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa14cfb)
#23 0x000055e4769b3059 (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0xa15059)
#24 0x000055e476571e0f (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5d3e0f)
#25 0x000055e47657423b (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5d623b)
#26 0x000055e47656eddf (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5d0ddf)
#27 0x000055e47656d65d (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5cf65d)
#28 0x00007f3a882cfd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#29 0x00007f3a882cfe40 call_init ./csu/../csu/libc-start.c:128:20
#30 0x00007f3a882cfe40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#31 0x000055e47656cfae (/home/jwzeng/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x5cefae)
Aborted

System information

- Ubuntu 22.04 LTS
- mojo 24.1.0 (55ec12d6)
- modular 0.5.1 (1b608e3d)
junweizeng commented 5 months ago

Here's another test case that seems to trigger the same bug.

Steps to reproduce

$ cat test.mojo
fn a() : if 2 : struct b
$
$ mojo build test.mojo
/path/to/test.mojo:1:10: error: 'if' statement must be on its own line
fn a() : if 2 : struct b
         ^
/path/to/test.mojo:1:17: error: 'struct' statement must be on its own line
fn a() : if 2 : struct b
                ^
/path/to/test.mojo:1:17: error: struct inside a function not supported here
fn a() : if 2 : struct b
                ^
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":1:4)
    >> fn a() : if 2 : struct b
          ^....................
    >> 
       <
2.      Crash parsing statement at loc("/path/to/test.mojo":1:10)
    >> fn a() : if 2 : struct b
                ^..............
    >> 
       <
3.      Crash parsing statement at loc("/path/to/test.mojo":1:17)
    >> fn a() : if 2 : struct b
                       ^.......
    >> 
       <
 #0 0x000055a62ce92347 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x128d347)
 #1 0x000055a62ce9019e llvm::sys::RunSignalHandlers() (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x128b19e)
 #2 0x000055a62ce929df SignalHandler(int) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x128d9df)
 #3 0x00007f797cd9f520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000055a62d22fabf M::KGEN::LIT::ASTDecl::getContext() const (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x162aabf)
 #5 0x000055a62d2651c8 M::KGEN::LIT::DeclResolver::addErroneousDecl(llvm::StringRef, llvm::SMLoc, M::KGEN::LIT::ASTDecl*) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16601c8)
 #6 0x000055a62d2bd764 (anonymous namespace)::StmtParser::parseSuite(long)::$_7::operator()(bool, unsigned long) const (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16b8764)
 #7 0x000055a62d2b83d7 (anonymous namespace)::StmtParser::parseSuite(long) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16b33d7)
 #8 0x000055a62d2bdccf (anonymous namespace)::StmtParser::parseSuite(long)::$_7::operator()(bool, unsigned long) const (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16b8ccf)
 #9 0x000055a62d2b83d7 (anonymous namespace)::StmtParser::parseSuite(long) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16b33d7)
#10 0x000055a62d2b8106 M::KGEN::LIT::ParserBase::parseSuite(M::KGEN::LIT::ASTDecl&) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16b3106)
#11 0x000055a62d255677 M::KGEN::LIT::DeclResolver::resolveBody(M::KGEN::LIT::FuncOp, M::KGEN::LIT::Lexer&, M::KGEN::LIT::ASTDecl&) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1650677)
#12 0x000055a62d2670bf M::KGEN::LIT::DeclResolver::resolve(M::KGEN::LIT::ASTDecl&, M::KGEN::LIT::DeclResolvedness, llvm::SMLoc) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x16620bf)
#13 0x000055a62d26755d M::KGEN::LIT::DeclResolver::resolveAllReferencedFrom(M::KGEN::LIT::ASTDecl&, bool) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x166255d)
#14 0x000055a62d26fcc6 importMojoImpl(M::LLCL::Runtime&, llvm::StringRef, llvm::SourceMgr&, M::KGEN::LIT::SharedState&, mlir::TimingScope&, llvm::SmallVectorImpl<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*, llvm::function_ref<M::KGEN::LIT::ASTDecl& (mlir::ModuleOp)>) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x166acc6)
#15 0x000055a62d27012d M::KGEN::LIT::importMojoFile(M::LLCL::Runtime&, llvm::SourceMgr&, M::KGEN::LIT::ParserConfig&, mlir::TimingScope&, llvm::SmallVectorImpl<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>*) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x166b12d)
#16 0x000055a62cdc513c mlir::OwningOpRef<mlir::ModuleOp> llvm::function_ref<mlir::OwningOpRef<mlir::ModuleOp> (M::KGEN::LIT::ParserConfig&, mlir::TimingScope&)>::callback_fn<build(M::State const&)::$_0>(long, M::KGEN::LIT::ParserConfig&, mlir::TimingScope&) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x11c013c)
#17 0x000055a62cdc68f4 M::invokeMojoParser(M::State const&, llvm::opt::InputArgList const&, M::KGEN::CompilationOptions&, mlir::MLIRContext*, M::LLCL::Runtime&, llvm::opt::OptSpecifier, llvm::opt::OptSpecifier, llvm::opt::OptSpecifier, llvm::function_ref<mlir::OwningOpRef<mlir::ModuleOp> (M::KGEN::LIT::ParserConfig&, mlir::TimingScope&)>) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x11c18f4)
#18 0x000055a62cdc2c29 build(M::State const&) (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x11bdc29)
#19 0x000055a62cdc15ec main (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x11bc5ec)
#20 0x00007f797cd86d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#21 0x00007f797cd86e40 call_init ./csu/../csu/libc-start.c:128:20
#22 0x00007f797cd86e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#23 0x000055a62cdc0fae _start (/home/jwzeng/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x11bbfae)
mojo crashed!
Please file a bug report.
Segmentation fault

System information

$ lsb_release -a
LSB Version:    core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 22.04 LTS
Release:        22.04
Codename:       jammy
$ mojo -v
mojo 2024.4.2923 (f54d89e3)
$ modular -v
modular 0.7.2 (d0adc668)
Brian-M-J commented 3 months ago

The first example doesn't seem to reproduce the crash anymore, but the second example does (I ran them on the Mojo playground as of 5th July 2024).