Skip to content

Detect unhashable object types at the ASR level #2664

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

Merged
merged 8 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,15 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}
}

bool is_hashable(ASR::ttype_t* object_type) {
if (ASR::is_a<ASR::List_t>(*object_type)
|| ASR::is_a<ASR::Dict_t>(*object_type)
|| ASR::is_a<ASR::Set_t>(*object_type)) {
return false;
}
return true;
}

AST::expr_t* get_var_intent_and_annotation(AST::expr_t *annotation, ASR::intentType &intent) {
if (AST::is_a<AST::Subscript_t>(*annotation)) {
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(annotation);
Expand Down Expand Up @@ -1701,6 +1710,17 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
if (AST::is_a<AST::Name_t>(*s->m_slice) || AST::is_a<AST::Subscript_t>(*s->m_slice)) {
ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice,
is_allocatable, is_const, raise_error, abi, is_argument);
if (!is_hashable(type)) {
diag.add(diag::Diagnostic(
"Unhashable type: '" + ASRUtils::type_to_str(type) + "'",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("Mutable type '" + ASRUtils::type_to_str(type)
+ "' cannot be stored in a set.",
{s->m_slice->base.loc})
})
);
throw SemanticAbort();
}
return ASRUtils::TYPE(ASR::make_Set_t(al, loc, type));
} else {
throw SemanticError("Only Name in Subscript supported for now in `set`"
Expand Down Expand Up @@ -1736,6 +1756,17 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}
ASR::ttype_t *key_type = ast_expr_to_asr_type(loc, *t->m_elts[0],
is_allocatable, is_const, raise_error, abi, is_argument);
if (!is_hashable(key_type)) {
diag.add(diag::Diagnostic(
"Unhashable type: '" + ASRUtils::type_to_str(key_type) + "'",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("Mutable type '" + ASRUtils::type_to_str(key_type)
+ "' cannot become a key in dict. Hint: Use an immutable type for key.",
{t->m_elts[0]->base.loc})
})
);
throw SemanticAbort();
}
ASR::ttype_t *value_type = ast_expr_to_asr_type(loc, *t->m_elts[1],
is_allocatable, is_const, raise_error, abi, is_argument);
raise_error_when_dict_key_is_float_or_complex(key_type, loc);
Expand Down Expand Up @@ -3779,7 +3810,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
ai.m_step, type, nullptr);
return false;
} else if (ASR::is_a<ASR::Dict_t>(*type)) {
throw SemanticError("unhashable type in dict: 'slice'", loc);
throw SemanticError("Unhashable type in dict: 'slice'", loc);
}
} else if(AST::is_a<AST::Tuple_t>(*m_slice) &&
ASRUtils::is_array(type)) {
Expand Down Expand Up @@ -6096,6 +6127,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::expr_t *key = ASRUtils::EXPR(tmp);
if (key_type == nullptr) {
key_type = ASRUtils::expr_type(key);
if (!is_hashable(key_type)) {
diag.add(diag::Diagnostic(
"Unhashable type: '" + ASRUtils::type_to_str(key_type) + "'",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("Mutable type '" + ASRUtils::type_to_str(key_type)
+ "' cannot become a key in dict. Hint: Use an immutable type for key.",
{key->base.loc})
})
);
throw SemanticAbort();
}
} else {
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) {
throw SemanticError("All dictionary keys must be of the same type",
Expand Down Expand Up @@ -6565,6 +6607,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::expr_t *value = ASRUtils::EXPR(tmp);
if (type == nullptr) {
type = ASRUtils::expr_type(value);
if (!is_hashable(type)) {
diag.add(diag::Diagnostic(
"Unhashable type: '" + ASRUtils::type_to_str(type) + "'",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("Mutable type '" + ASRUtils::type_to_str(type)
+ "' cannot be stored in a set.",
{value->base.loc})
})
);
throw SemanticAbort();
}
} else {
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) {
throw SemanticError("All Set values must be of the same type for now",
Expand Down
6 changes: 6 additions & 0 deletions tests/errors/test_dict_key1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32

def test_dict_key1():
my_dict: dict[list[i32], str] = {[1, 2]: "first", [3, 4]: "second"}

test_dict_key1()
6 changes: 6 additions & 0 deletions tests/errors/test_dict_key2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32

def test_dict_key2():
my_dict: dict[dict[i32, str], str] = {{1: "a", 2: "b"}: "first", {3: "c", 4: "d"}: "second"}

test_dict_key2()
6 changes: 6 additions & 0 deletions tests/errors/test_dict_key3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32

def test_dict_key3():
my_dict: dict[set[str], str] = {{1, 2}: "first", {3, 4}: "second"}

test_dict_key3()
4 changes: 4 additions & 0 deletions tests/errors/test_dict_key4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_dict_key4():
print({[1, 2]: "first", [3, 4]: "second"})

test_dict_key4()
4 changes: 4 additions & 0 deletions tests/errors/test_dict_key5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_dict_key5():
print({{1: "a", 2: "b"}: "first", {3: "c", 4: "d"}: "second"})

test_dict_key5()
4 changes: 4 additions & 0 deletions tests/errors/test_dict_key6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_dict_key6():
print({{1, 2}: "first", {3, 4}: "second"})

test_dict_key6()
6 changes: 6 additions & 0 deletions tests/errors/test_set_object1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32

def test_set_object1():
my_set: set[list[i32]] = {[1, 2], [3, 4]}

test_set_object1()
6 changes: 6 additions & 0 deletions tests/errors/test_set_object2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32

def test_set_object2():
my_set: set[dict[i32, str]] = {{1: "a", 2: "b"}, {3: "c", 4: "d"}}

test_set_object2()
6 changes: 6 additions & 0 deletions tests/errors/test_set_object3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from lpython import i32

def test_set_object3():
my_set: set[set[i32]] = {{1, 2}, {3, 4}}

test_set_object3()
4 changes: 4 additions & 0 deletions tests/errors/test_set_object4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_set_object4():
print({[1, 2], [3, 4]})

test_set_object4()
4 changes: 4 additions & 0 deletions tests/errors/test_set_object5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_set_object5():
print({{1: "a", 2: "b"}, {3: "c", 4: "d"}})

test_set_object5()
4 changes: 4 additions & 0 deletions tests/errors/test_set_object6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def test_set_object6():
print({{1, 2}, {3, 4}})

test_set_object6()
2 changes: 1 addition & 1 deletion tests/reference/asr-test_dict7-1415e14.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict7-1415e14.stderr",
"stderr_hash": "a51d1d4a46839e1f4258410e979ba83a14abe8c011482e30be2336cd",
"stderr_hash": "843409ee199a2581d9cd1abab45bb59e5e0372d56ef94f1b15aea584",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-test_dict7-1415e14.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: unhashable type in dict: 'slice'
semantic error: Unhashable type in dict: 'slice'
--> tests/errors/test_dict7.py:4:11
|
4 | print(d[1:2])
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-test_dict_key1-6e57a28.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_dict_key1-6e57a28",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_dict_key1.py",
"infile_hash": "0ee4ab5e47edab5de323d7cf97cf3e726e54882e4a5fadb82ee9aedc",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict_key1-6e57a28.stderr",
"stderr_hash": "4ee828a6b9a93bfb8285c2006843243b5327f915f9548a2f1b3f1480",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_dict_key1-6e57a28.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'list'
--> tests/errors/test_dict_key1.py:4:19
|
4 | my_dict: dict[list[i32], str] = {[1, 2]: "first", [3, 4]: "second"}
| ^^^^^^^^^ Mutable type 'list' cannot become a key in dict. Hint: Use an immutable type for key.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_dict_key2-18ea6fb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_dict_key2-18ea6fb",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_dict_key2.py",
"infile_hash": "25b325264991082018c989f990a6b71409e7af0df4a27e5b5142a349",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict_key2-18ea6fb.stderr",
"stderr_hash": "5883683aaf0a4ae56b5fd86f56f6900e3e752a72bc675af9c607d998",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_dict_key2-18ea6fb.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'dict'
--> tests/errors/test_dict_key2.py:4:19
|
4 | my_dict: dict[dict[i32, str], str] = {{1: "a", 2: "b"}: "first", {3: "c", 4: "d"}: "second"}
| ^^^^^^^^^^^^^^ Mutable type 'dict' cannot become a key in dict. Hint: Use an immutable type for key.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_dict_key3-9fc7793.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_dict_key3-9fc7793",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_dict_key3.py",
"infile_hash": "9675711d37ed0e58ddd82a53ec580cc21c58a9b94ad598b706fb78f8",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict_key3-9fc7793.stderr",
"stderr_hash": "bd995f8512a83892aa1be985c6f7ff1761691829150549ba4ac84f17",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_dict_key3-9fc7793.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'set'
--> tests/errors/test_dict_key3.py:4:19
|
4 | my_dict: dict[set[str], str] = {{1, 2}: "first", {3, 4}: "second"}
| ^^^^^^^^ Mutable type 'set' cannot become a key in dict. Hint: Use an immutable type for key.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_dict_key4-dc7abfc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_dict_key4-dc7abfc",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_dict_key4.py",
"infile_hash": "197ac00a9a0a5763f939d8b5aec2e33a5b3ec769d93149a1c93999c1",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict_key4-dc7abfc.stderr",
"stderr_hash": "ff55c824acc6a3bc2c7f8845b345bcf5d66d13374526ab958a005dc7",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_dict_key4-dc7abfc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'list'
--> tests/errors/test_dict_key4.py:2:12
|
2 | print({[1, 2]: "first", [3, 4]: "second"})
| ^^^^^^ Mutable type 'list' cannot become a key in dict. Hint: Use an immutable type for key.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_dict_key5-87496d1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_dict_key5-87496d1",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_dict_key5.py",
"infile_hash": "08a7118a664a5ac63f470b5a47d19ed7c35a06e3c8ae40a7b44010ea",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict_key5-87496d1.stderr",
"stderr_hash": "c7ae39bf80d3a6d1817fbd7aba5455e96623b1225abeb9428af2c73a",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_dict_key5-87496d1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'dict'
--> tests/errors/test_dict_key5.py:2:12
|
2 | print({{1: "a", 2: "b"}: "first", {3: "c", 4: "d"}: "second"})
| ^^^^^^^^^^^^^^^^ Mutable type 'dict' cannot become a key in dict. Hint: Use an immutable type for key.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_dict_key6-1d334b2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_dict_key6-1d334b2",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_dict_key6.py",
"infile_hash": "14ea00618e1414afe9f93d0aa0d4fd5b4332883465126cbba6faab76",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_dict_key6-1d334b2.stderr",
"stderr_hash": "74a8ee0549333b4659afc7deec824a14bbc672316b22e3c99a026846",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_dict_key6-1d334b2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'set'
--> tests/errors/test_dict_key6.py:2:12
|
2 | print({{1, 2}: "first", {3, 4}: "second"})
| ^^^^^^ Mutable type 'set' cannot become a key in dict. Hint: Use an immutable type for key.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_set_object1-d9bd2e1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_set_object1-d9bd2e1",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_set_object1.py",
"infile_hash": "9450d7ca46f30271944800137d28413648bafdbeb7f0a7ac0906c832",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_set_object1-d9bd2e1.stderr",
"stderr_hash": "b528f86f591ab403348d8dd5037d2385fdb7ce29501215a69d10702f",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_set_object1-d9bd2e1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'list'
--> tests/errors/test_set_object1.py:4:17
|
4 | my_set: set[list[i32]] = {[1, 2], [3, 4]}
| ^^^^^^^^^ Mutable type 'list' cannot be stored in a set.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_set_object2-41401ff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_set_object2-41401ff",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_set_object2.py",
"infile_hash": "e7360eff7caf0991c5bd4c505a947d23e2bc01277e9a2966362400df",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_set_object2-41401ff.stderr",
"stderr_hash": "4fe845a8f949fce5b955b86d5a5ad60f0e1ae84e3c17b01572d37e2a",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_set_object2-41401ff.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'dict'
--> tests/errors/test_set_object2.py:4:17
|
4 | my_set: set[dict[i32, str]] = {{1: "a", 2: "b"}, {3: "c", 4: "d"}}
| ^^^^^^^^^^^^^^ Mutable type 'dict' cannot be stored in a set.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_set_object3-680b593.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_set_object3-680b593",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_set_object3.py",
"infile_hash": "f1dea0a951aa880721aa38a0dcf254983e7d50ab408c64c87b9a078e",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_set_object3-680b593.stderr",
"stderr_hash": "05d3a6338fd929fef485c7403500a1f2111dc8e638a3369ff942bea2",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_set_object3-680b593.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Unhashable type: 'set'
--> tests/errors/test_set_object3.py:4:17
|
4 | my_set: set[set[i32]] = {{1, 2}, {3, 4}}
| ^^^^^^^^ Mutable type 'set' cannot be stored in a set.
13 changes: 13 additions & 0 deletions tests/reference/asr-test_set_object4-243eb04.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_set_object4-243eb04",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_set_object4.py",
"infile_hash": "0b339aaa798fca7bd12920c583b0d60d70fe2f8afeb68a1811992f59",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_set_object4-243eb04.stderr",
"stderr_hash": "dff44d0e30f3fed351e8df2bc1875c3a9972db927a58400df456ec12",
"returncode": 2
}
Loading