modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo
Other
21.92k stars 2.53k forks source link

[BUG] A crash happened in the mojo parser. #2661

Open Haislich opened 1 month ago

Haislich commented 1 month ago

Bug description

I was trying to understand how Optional works on mojo and got the formatter to crash.

Steps to reproduce

fn add[
    offset: Int = 0
](owned a: Int, b: Int, *, c: Optional[Int] = Optional[Int](None)) -> Int:
    a += 1
    if c.value == NoneType:
        return offset + a + b
    return a + b

fn main():
    print(add(1, 2))
<unknown>:0: error: caller input parameter #2 has type '!lit.lifetime<?>' but callee expected type '!lit.lifetime<*(0,0)>'
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 -o /build/prog /source/prog.mojo
1.  Crash resolving decl body at loc("/source/prog.mojo":1:4)
    >> fn add[
          ^...
    >>     offset: Int = 0
    >> ](owned a: Int, b: Int, *, c: Optional[Int] = Optional[Int](None)) -> Int:
    >>     a += 1
    >>     if c.value == NoneType:
       ..........................<
2.  Crash parsing statement at loc("/source/prog.mojo":5:5)
    >>     if c.value == NoneType:
           ^.....................<
 #0 0x00005567daed4487 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x128c487)
 #1 0x00005567daed22de llvm::sys::RunSignalHandlers() (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x128a2de)
 #2 0x00005567daed4b1f SignalHandler(int) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x128cb1f)
 #3 0x00007f7877907520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00005567db484ef3 mlir::Type::getContext() const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x183cef3)
 #5 0x00005567dc71300c M::KGEN::LIT::getUnboundSpecializedSignature(M::KGEN::LIT::LITSignatureType, M::KGEN::ParameterExprArrayAttr) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x2acb00c)
 #6 0x00005567dc714d53 M::KGEN::LIT::FuncOp::getBoundReference(M::KGEN::ParameterExprArrayAttr) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x2accd53)
 #7 0x00005567db27bae6 M::KGEN::LIT::ParamBindings::getBoundConstAttrFor(M::KGEN::LIT::FuncOp, llvm::StringRef, M::KGEN::LIT::ExprNode const*) const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x1633ae6)
 #8 0x00005567db27dba1 M::KGEN::LIT::OverloadSet::getBoundConstantAttr() const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x1635ba1)
 #9 0x00005567db27e396 M::KGEN::LIT::OverloadSet::getDirectSymbol(M::KGEN::LIT::ASTType) const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x1636396)
#10 0x00005567db27e448 M::KGEN::LIT::OverloadSet::emitAsCValue(M::KGEN::LIT::ExprEmitter&, M::KGEN::LIT::ValueDest&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x1636448)
#11 0x00005567db2b71b0 M::KGEN::LIT::ExprEmitter::emitCValue(M::KGEN::LIT::ASTExprAnd<M::KGEN::LIT::AnyValue>, M::KGEN::LIT::ValueDest&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x166f1b0)
#12 0x00005567db2b833c M::KGEN::LIT::ExprEmitter::emitCValue(M::KGEN::LIT::ASTExprAnd<M::KGEN::LIT::AnyValue>, M::KGEN::LIT::ExprContext, M::KGEN::LIT::ASTType) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x167033c)
#13 0x00005567db27f709 M::KGEN::LIT::ExprEmitter::emitNamedMethodCall(llvm::StringRef, M::KGEN::LIT::CallOperands const&, M::KGEN::LIT::ValueDest&, M::KGEN::LIT::CallSyntax, M::KGEN::LIT::ExprNode const*) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x1637709)
#14 0x00005567db2d46eb emitBinOpCall(M::KGEN::LIT::ASTExprAnd<M::KGEN::LIT::AnyValue>, M::KGEN::LIT::ASTExprAnd<M::KGEN::LIT::AnyValue>, M::KGEN::LIT::ExprNode::Kind, M::KGEN::LIT::ValueDest&, M::KGEN::LIT::ExprNode const*, M::KGEN::LIT::ExprEmitter&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x168c6eb)
#15 0x00005567db2dc5cb M::KGEN::LIT::ChainedCmpOpNode::emitIR(M::KGEN::LIT::ValueDest&, M::KGEN::LIT::ExprEmitter&) const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x16945cb)
#16 0x00005567db2c00e0 M::KGEN::LIT::ExprEmitter::emitExprCValue(M::KGEN::LIT::ExprNode const*, M::KGEN::LIT::ExprContext) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x16780e0)
#17 0x00005567db2c1205 M::KGEN::LIT::ExprEmitter::emitExprI1(M::KGEN::LIT::ExprNode const*, M::KGEN::LIT::ExprContext) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x1679205)
#18 0x00005567db3056fc (anonymous namespace)::StmtParser::parseElif(mlir::Location, M::KGEN::LIT::LexerCursor, unsigned long)::$_11::operator()(mlir::Location) const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x16bd6fc)
#19 0x00005567db2ff82a (anonymous namespace)::StmtParser::parseSuite(long)::$_7::operator()(bool, unsigned long) const (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x16b782a)
#20 0x00005567db2f9f68 (anonymous namespace)::StmtParser::parseSuite(long) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x16b1f68)
#21 0x00005567db2f9d96 M::KGEN::LIT::ParserBase::parseSuite(M::KGEN::LIT::ASTDecl&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x16b1d96)
#22 0x00005567db297917 M::KGEN::LIT::DeclResolver::resolveBody(M::KGEN::LIT::FuncOp, M::KGEN::LIT::Lexer&, M::KGEN::LIT::ASTDecl&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x164f917)
#23 0x00005567db2a948f M::KGEN::LIT::DeclResolver::resolve(M::KGEN::LIT::ASTDecl&, M::KGEN::LIT::DeclResolvedness, llvm::SMLoc) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x166148f)
#24 0x00005567db2a992d M::KGEN::LIT::DeclResolver::resolveAllReferencedFrom(M::KGEN::LIT::ASTDecl&, bool) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x166192d)
#25 0x00005567db2b2096 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)>) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x166a096)
#26 0x00005567db2b24fd 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>>>*) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x166a4fd)
#27 0x00005567dae0727c 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&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x11bf27c)
#28 0x00005567dae08a34 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&)>) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x11c0a34)
#29 0x00005567dae04d69 build(M::State const&) (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x11bcd69)
#30 0x00005567dae0372c main (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x11bb72c)
#31 0x00007f78778eed90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#32 0x00007f78778eee40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#33 0x00005567dae030ee _start (/opt/modular/pkg/packages.modular.com_max/bin/mojo+0x11bb0ee)

The output on my local machine is the following:

[49200:49200:20240515,120325.643597:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[49200:49200:20240515,120325.643646:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
[49200:49201:20240515,120325.644407:ERROR directory_reader_posix.cc:42] opendir /home/haislich/.modular/crashdb/attachments/cf926f79-6998-415a-af3c-ba615ad60a81: No such file or directory (2)
 #0 0x0000556f85446d57 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1291d57)
 #1 0x0000556f85444bae (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x128fbae)
 #2 0x0000556f854473ef (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x12923ef)
 #3 0x00007f71db0c3520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000556f85de7286 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1c32286)
 #5 0x0000556f86740b23 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x258bb23)
 #6 0x0000556f8673bb38 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2586b38)
 #7 0x0000556f86b3aef3 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2985ef3)
 #8 0x0000556f86b3e9fa (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x29899fa)
 #9 0x0000556f85e2fa40 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1c7aa40)
#10 0x0000556f858fb6ed (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x17466ed)
#11 0x0000556f85e2f670 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1c7a670)
#12 0x0000556f85e2f260 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1c7a260)
#13 0x0000556f86c0c3c2 (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2a573c2)
#14 0x0000556f86c0c26a (/home/haislich/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2a5726a)
#15 0x00007f71db3a8253 (/lib/x86_64-linux-gnu/libstdc++.so.6+0xdc253)
#16 0x00007f71db115ac3 (/lib/x86_64-linux-gnu/libc.so.6+0x94ac3)
#17 0x00007f71db1a7850 (/lib/x86_64-linux-gnu/libc.so.6+0x126850)
mojo crashed!
Please file a bug report.
Segmentation fault

System information

- What OS did you do install Mojo on ?
Ubuntu running on wsl2 

> Linux Spotto 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

> Versione WSL: 2.1.5.0
> Versione kernel: 5.15.146.1-2
> Versione WSLg: 1.0.60
> Versione MSRDC: 1.2.5105
> Versione Direct3D: 1.611.1-81528511
> Versione DXCore: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
> Versione di Windows: 10.0.22631.3527

- Provide version information for Mojo by pasting the output of `mojo -v`
> mojo 2024.5.1413 (8aa90515)
- Provide Modular CLI version by pasting the output of `modular -v`
> modular 0.8.0 (39a426b5)
Haislich commented 1 month ago

Commenting

if c.value == NoneType:
        return offset + a + b

Seems to fix. I have no clue why the formatter crashes though.

ematejska commented 2 weeks ago

In the latest nightly the error is a bit better but still doesn't parse:

error: TODO: partial application of member methods is not yet supported

    if c.value == NoneType:

       ^

mojo: error: failed to parse the provided Mojo source module
ematejska commented 2 weeks ago

Dup of https://github.com/modularml/mojo/issues/2655

ematejska commented 2 weeks ago

Please call c.value() to workaround this