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

Bug: Trying to access items from data-structure contained within Const fails #2578

Closed
kmr-srbh opened this issue Mar 7, 2024 · 0 comments · Fixed by #2579
Closed

Bug: Trying to access items from data-structure contained within Const fails #2578

kmr-srbh opened this issue Mar 7, 2024 · 0 comments · Fixed by #2579

Comments

@kmr-srbh
Copy link
Contributor

kmr-srbh commented Mar 7, 2024

Overview

Accessing values from a data-structure contained within Const fails with different errors for different data-structures.

Dictionary

from lpython import i32, dict, Const

d: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
print(d["a"])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
semantic error: Type mismatch in index, expected a single integer or slice
 --> ./examples/example.py:4:9
  |
4 | print(d["a"])
  |         ^^^ type mismatch (found: 'str', expected: 'i32' or slice)


Note: Please report unclear or confusing messages as bugs at
https://github.com/lcompilers/lpython/issues.

List

from lpython import i32, list, Const

l: Const[list[i32]] = [1, 2, 3, 4, 5]
print(l[0])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
ASR verify pass error: ASR verify: The variable in ArrayItem must be an array, not a scalar
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython", in _start()
  File "./csu/../csu/libc-start.c", line 360, in __libc_start_main_impl()
  File "./csu/../sysdeps/x86/libc-start.c", line 58, in __libc_start_call_main()
  File "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython.cpp", line 1873, in main()
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython.cpp", line 787, in compile_python_to_object_file()
    !(arg_c && compiler_options.po.disable_main), "__main__", infile);
LCompilersException: Verify failed

Tuple

from lpython import i32, tuple, Const

t: Const[tuple[i32, i32, i32, i32, i32]] = (1, 2, 3, 4, 5)
print(t[0])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
Internal Compiler Error: Unhandled exception
.
.
.
LCompilersException: Not implemented 8

String

from lpython import i32, str, Const

s: Const[str] = "Hello, LPython!"
print(s[0])
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
ASR verify pass error: ASR verify: The variable in ArrayItem must be an array, not a scalar
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython", in _start()
  File "./csu/../csu/libc-start.c", line 360, in __libc_start_main_impl()
  File "./csu/../sysdeps/x86/libc-start.c", line 58, in __libc_start_call_main()
  File "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython.cpp", line 1873, in main()
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
  File "/home/saurabh-kumar/Projects/System/lpython/src/bin/lpython.cpp", line 787, in compile_python_to_object_file()
    !(arg_c && compiler_options.po.disable_main), "__main__", infile);
LCompilersException: Verify failed

Fix

Check for Const type when handling subscript indices and do required processing for mutable types like dict and list. For types which are already immutable, like str and tuple, throw an error stating that Const is not required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant