modularml / mojo

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

[BUG] Compiler crash for access to sys.argv[0] #3005

Open oepir opened 2 weeks ago

oepir commented 2 weeks ago

Bug description

The mojo compiler crashed trying to parse var prog = sys.argv[0].

Steps to reproduce

Trying to compile the following program:

import sys

fn main() :
    var prog = sys.argv[0]
    print(prog)

using mojo build bug.mojo

leads to:

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 bug.mojo
1.  Crash resolving decl body at loc("/Users/sduvan/mojo/bug.mojo":3:4)
    >> fn main() :
          ^.......
    >>     var prog = sys.argv[0]
       ..........................<
2.  Crash parsing statement at loc("/Users/sduvan/mojo/bug.mojo":4:5)
    >>     var prog = sys.argv[0]
           ^.....................<
 #0 0x0000000104c28c24 llvm_strlcpy (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1000c0c24)
 #1 0x0000000104c26f10 llvm_strlcpy (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1000bef10)
 #2 0x0000000104c292c4 llvm_strlcpy (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1000c12c4)
 #3 0x0000000182262a24 (/usr/lib/system/libsystem_platform.dylib+0x18042ea24)
 #4 0x0000000104f392c4 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003d12c4)
 #5 0x0000000104ecfd84 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x100367d84)
 #6 0x0000000104ed15a4 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003695a4)
 #7 0x0000000104ed1ca0 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x100369ca0)
 #8 0x0000000104ed1eb0 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x100369eb0)
 #9 0x0000000104f01c40 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x100399c40)
#10 0x0000000104f036bc __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x10039b6bc)
#11 0x0000000104f18db8 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003b0db8)
#12 0x0000000104f0a944 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003a2944)
#13 0x0000000104f500d0 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003e80d0)
#14 0x0000000104f472f0 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003df2f0)
#15 0x0000000104f47124 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003df124)
#16 0x0000000104ee25c4 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x10037a5c4)
#17 0x0000000104ef2c68 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x10038ac68)
#18 0x0000000104ef319c __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x10038b19c)
#19 0x0000000104efc7a8 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003947a8)
#20 0x0000000104efc9c8 __jit_debug_register_code (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x1003949c8)
#21 0x0000000104b768e8 _mh_execute_header (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x10000e8e8)
#22 0x0000000104b7281c _mh_execute_header (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x10000a81c)
#23 0x0000000104b71738 _mh_execute_header (/Users/sduvan/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x100009738)
#24 0x0000000181edbf28 
mojo crashed!
Please file a bug report.

System information

MacOS 13.6.7

shell> mojo -v
mojo 24.4.0 (2cb57382)

shell> modular -v
modular 0.8.0 (39a426b5)
ematejska commented 2 weeks ago

You actually need parenthesis after argv, like "sys.argv()[0]" because argv is a function. However, an error message should be displayed instead of crashing here.

bgreni commented 2 weeks ago

You actually need parenthesis after argv, like "sys.argv()[0]" because argv is a function. However, an error message should be displayed instead of crashing here.

Yeah seems related to free functions imported from other modules somehow? If you define a local function and try this you get a reasonable error message.

oepir commented 2 weeks ago

Got it, thanks! However, it seems odd to me for a language that is aiming to be a superset of Python to deviate from the latter in such a subtle way -- https://docs.python.org/3/library/sys.html.

bgreni commented 2 weeks ago

Got it, thanks! However, it seems odd to me for a language that is aiming to be a superset of Python to deviate from the latter in such a subtle way -- https://docs.python.org/3/library/sys.html.

This is likely just due to a technical limitation at the moment relating to file scope level expressions. I imagine in the future it'll match.

ematejska commented 1 week ago

Yes, exactly!