Detect unhashable object types at the ASR level#2664
Merged
ubaidsk merged 8 commits intolcompilers:mainfrom Apr 27, 2024
Merged
Detect unhashable object types at the ASR level#2664ubaidsk merged 8 commits intolcompilers:mainfrom
ubaidsk merged 8 commits intolcompilers:mainfrom
Conversation
dict key at ASR level
ubaidsk
reviewed
Apr 25, 2024
Collaborator
ubaidsk
left a comment
There was a problem hiding this comment.
The current approach in this PR seems fine to me. We should also check for mutable/unhashable types in the type declaration for a dict. We can do that later. Since this PR does not check on declaration type, but checks on the dict constant, let's update the error tests added in this PR to not use a type declaration. For example, we can have tests like:
% cat examples/expr2.py
from lpython import i32
def f():
print({{1, 2}: "first", {3, 4}: "second"})
f()
% lpython examples/expr2.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 1999
err = compile_python_using_llvm(arg_file, tmp_o, runtime_library_dir,
File "/Users/ubaid/Desktop/OpenSource/lpython/src/bin/lpython.cpp", line 878
res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/lpython/python_evaluator.cpp", line 71
run_fn, infile);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 9779
v.visit_asr((ASR::asr_t&)asr);
File "../libasr/asr.h", line 5177
File "../libasr/asr.h", line 5153
File "../libasr/asr.h", line 5178
File "../libasr/asr.h", line 4882
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 932
ASR::symbol_t *mod = x.m_symtab->get_symbol(item);
File "../libasr/asr.h", line 5180
File "../libasr/asr.h", line 4890
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3007
finish_module_init_function_prototype(x);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3994
ASR::Function_t *s = ASR::down_cast<ASR::Function_t>(item.second);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3750
visit_procedures(x);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 3947
this->visit_stmt(*x.m_body[i]);
File "../libasr/asr.h", line 5197
File "../libasr/asr.h", line 4929
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 7698
handle_print(x);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 7993
compute_fmt_specifier_and_arg(fmt, args, x.m_values[i], x.base.base.loc);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 7812
this->visit_expr_wrapper(v, true);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 5240
this->visit_expr(*x);
File "../libasr/asr.h", line 5246
File "../libasr/asr.h", line 5026
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 1284
x_dict->m_key_type, x_dict->m_value_type, name2memidx);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/llvm_utils.cpp", line 3972
rehash_all_at_once_if_needed(dict, module, key_asr_type, value_asr_type, name2memidx);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/llvm_utils.cpp", line 3934
capacity_times_3), [&]() {
File "../libasr/codegen/llvm_utils.h", line 349
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/llvm_utils.cpp", line 3935
rehash(dict, module, key_asr_type, value_asr_type, name2memidx);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/llvm_utils.cpp", line 3751
llvm::Value* key_hash = get_key_hash(current_capacity, key, key_asr_type, *module);
File "/Users/ubaid/Desktop/OpenSource/lpython/src/libasr/codegen/llvm_utils.cpp", line 3671
throw LCompilersException("Hashing " + ASRUtils::type_to_str_python(key_asr_type) +
LCompilersException: Hashing set[i32] isn't implemented yet.and this should print a semantic error instead of a codegen exception.
Contributor
Author
Let's handle this in this PR only. |
a84486a to
53341f3
Compare
ubaidsk
approved these changes
Apr 27, 2024
Collaborator
ubaidsk
left a comment
There was a problem hiding this comment.
Shared two minor comments. Rest looks good. Thanks for this! Amazing work!
assem2002
pushed a commit
to assem2002/lpython
that referenced
this pull request
Apr 28, 2024
* Detect unhashable types for `dict` key * Tests: Add error tests and update references * Create a function to check for hashable objects * Tests: Add error tests for `set` and update references * Tests: Update error tests and references * Check for unhashable types in type-annotations * Tests: Update tests and references * Fix indentation
assem2002
pushed a commit
to assem2002/lpython
that referenced
this pull request
Apr 28, 2024
* Detect unhashable types for `dict` key * Tests: Add error tests and update references * Create a function to check for hashable objects * Tests: Add error tests for `set` and update references * Tests: Update error tests and references * Check for unhashable types in type-annotations * Tests: Update tests and references * Fix indentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2663
Working for
dictis shown below. It is the same forset.List
Dictionary
Set