lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 158 forks source link

Bug: Catch unhashable objects in `dict` and `set` at the ASR level #2663

Closed kmr-srbh closed 5 months ago

kmr-srbh commented 5 months ago

In the current state, we throw an error stating that hashing a specific type is not implemented. This happens totally in the back-end when we try to hash the object.

from lpython import i32

my_dict: dict[list[i32], str] = {[1, 2]: "first", [3, 4]: "second"}
(lp) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
.
.
.
    llvm::Value* key_hash = get_key_hash(current_capacity, key, key_asr_type, *module);
LCompilersException: Hashing list[i32] isn't implemented yet.

However, we do not catch types which cannot be hashed. All mutable types are incompatible to become a key in a dict and element in a set. We should detect them early at the ASR level and throw an error.