Skip to content

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

Closed
@kmr-srbh

Description

@kmr-srbh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions