Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mojo]: Simple python like function crashes REPL #804

Closed
cheburakshu opened this issue Sep 14, 2023 · 7 comments
Closed

[Mojo]: Simple python like function crashes REPL #804

cheburakshu opened this issue Sep 14, 2023 · 7 comments
Assignees
Labels
mojo Issues that are related to mojo mojo-lang Tag for all issues related to language.

Comments

@cheburakshu
Copy link

cheburakshu commented Sep 14, 2023

Issue description

vscode ➜ ~/llama2.mojo (master) $ mojo -v
mojo 0.2.1 (64d14e85)
vscode ➜ ~/llama2.mojo (master) $ mojo
Welcome to Mojo! 🔥
Expressions are delimited by a blank line.
Type `:mojo help` for further assistance.
1> def a(*b):
2.     ...
3. 
lldb: /__w/modular/modular/third-party/llvm-project/llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(From &) [To = M::KGEN::PointerType, From = mlir::Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
[14554:14554:20230914,230825.266481:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[14554:14554:20230914,230825.266757:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
[User] error: The Mojo REPL has crashed and attempted recovery. If the REPL behaves inconsistently, please restart to ensure correct behavior.

expression failed to parse (no further compiler diagnostics)
1> 

@cheburakshu cheburakshu added the modular-cli modular tool issues label Sep 14, 2023
@Nusab19
Copy link

Nusab19 commented Sep 15, 2023

try adding %python in the top ( line 1 )

Not sure if it'll work ( cuz I wasn't able to install mojo ), but saw this in the examples

@cheburakshu
Copy link
Author

this is also native mojo code, not just python (note def requires no typing), should ideally work.

@zbowling zbowling changed the title [Modular CLI]: Simple python like function crashes REPL [Mojo]: Simple python like function crashes REPL Sep 18, 2023
@zbowling zbowling added mojo Issues that are related to mojo and removed modular-cli modular tool issues labels Sep 18, 2023
@ematejska ematejska added the mojo-tooling Tag for all issues related to repl, lldb, lsp, vscode extension. label Sep 18, 2023
@walter-erquinigo walter-erquinigo removed the mojo-tooling Tag for all issues related to repl, lldb, lsp, vscode extension. label Sep 18, 2023
@walter-erquinigo
Copy link
Contributor

I was able to reproduce this error. It seems that the compiler is not handling correctly the vararg in the function declaration.

@walter-erquinigo walter-erquinigo added the mojo-lang Tag for all issues related to language. label Sep 18, 2023
@Mogball
Copy link
Collaborator

Mogball commented Sep 21, 2023

Thanks for filing! Seems like a parser crash. @laszlokindrat can you take a look?

@laszlokindrat
Copy link
Contributor

Thank you for opening this issue! This is a known limitation of the current system, but the compiler shouldn't have crashed; in the next release, it will raise an error message instead.

The workaround currently is to pass variadic arguments of dynamic types as borrowed:

def a(borrowed *b): pass

@cheburakshu
Copy link
Author

Can you make print friendly to developers? It is simply too hard to use it without having to refer to docs?

Attempt 1: Just print a.

4> def foo(borrowed *a):
5.     print(a)
6. 
[User] error: Expression [4]:6:10: no matching function in call to 'print': 
    print(a)
    ~~~~~^~~

Expression [0]:1:1: candidate not viable: callee expects 0 arguments, but 1 was specified
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'DType'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'String'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'StringRef'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'StringLiteral'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'Bool'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'FloatLiteral'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'Int'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 2 input parameters but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 2 input parameters but 0 were provided (4 more notes omitted.)
from memory.unsafe import Pointer
^


expression failed to parse (no further compiler diagnostics)
4> 

Attempt 2: Print length of a

6> def foo(borrowed *a):
7.     print(len(a))
8. 
[User] error: Expression [5]:6:14: no matching function in call to 'len': 
    print(len(a))
          ~~~^~~

Expression [0]:1:1: candidate not viable: callee expects 2 input parameters but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 2 input parameters but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 1 input parameter but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 1 input parameter but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'String'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'StringRef'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: argument #0 cannot be converted from 'variadic<pointer<!kgen.declref<_"$builtin"::_"$object"::_object>>>' to 'StringLiteral'
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 2 input parameters but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 1 input parameter but 0 were provided
from memory.unsafe import Pointer
^

Expression [0]:1:1: candidate not viable: callee expects 1 input parameter but 0 were provided (3 more notes omitted.)
from memory.unsafe import Pointer
^


expression failed to parse (no further compiler diagnostics)
6> 

@laszlokindrat
Copy link
Contributor

laszlokindrat commented Sep 22, 2023

We have an issue related this: #793 and we are working on making variadics more ergonomic! The closest thing you can do right now is something like this:

def foo(borrowed *a: Int):
    print("number of variadic args:", len(VariadicList(a)))
    for i in range(len(VariadicList(a))):
        print("arg #" + String(i) + ":", a[i])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mojo Issues that are related to mojo mojo-lang Tag for all issues related to language.
Projects
None yet
Development

No branches or pull requests

7 participants