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

Add support for dict methods with Const #2567

Merged
merged 40 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4d833da
Implement attributes for `Const dict`
kmr-srbh Mar 2, 2024
3d65739
Remove duplicate changes
kmr-srbh Mar 3, 2024
7cd71db
Improve checking for `Const` types
kmr-srbh Mar 7, 2024
005fb72
Simplify type checking for `Const dict`.
kmr-srbh Mar 7, 2024
d7ea3bb
Merge branch 'main' into const-dict-methods
kmr-srbh Mar 8, 2024
161bd8e
Add tests
kmr-srbh Mar 10, 2024
e75755b
Update test
kmr-srbh Mar 10, 2024
1a068c8
Update fetching attribute name logic
kmr-srbh Mar 10, 2024
68524a4
Update test references
kmr-srbh Mar 10, 2024
6337279
Update fetching `dict_type`
kmr-srbh Mar 10, 2024
c422524
Formatting changes
kmr-srbh Mar 10, 2024
a047e71
Update test references
kmr-srbh Mar 10, 2024
9917227
Update error test references
kmr-srbh Mar 10, 2024
03d5998
Tests: Update test references
kmr-srbh Mar 10, 2024
01748f7
Merge branch 'main' into const-dict-methods
kmr-srbh Mar 10, 2024
22009cd
Tests: Add runtime tests and update test references
kmr-srbh Mar 10, 2024
3b3a272
Merge branch 'main' into const-dict-methods
kmr-srbh Mar 11, 2024
5b1dcd4
Merge branch 'main' into const-dict-methods
kmr-srbh Mar 18, 2024
848f3e0
Merge branch 'main' into const-dict-methods
kmr-srbh Mar 19, 2024
e53a8bc
Merge branch 'main' into const-dict-methods
kmr-srbh Apr 7, 2024
3b61a29
Merge branch 'main' into const-dict-methods
kmr-srbh Apr 21, 2024
74dd5ce
Merge branch 'main' into const-dict-methods
kmr-srbh Apr 30, 2024
737acb4
Remove checks on the absent `Const` node
kmr-srbh May 1, 2024
4a2942f
Remove call to `get_contained_type()`
kmr-srbh May 1, 2024
6f19980
Tests: Add tests and update references
kmr-srbh May 1, 2024
5988ca2
Merge branch 'main' into const-dict-methods
kmr-srbh May 1, 2024
634011c
Style changes
kmr-srbh May 1, 2024
8579851
Tests: Update tests and add to CMakeLists
kmr-srbh May 1, 2024
809fe8d
Delete tests/reference/asr-test_const_dict-151acad.json
kmr-srbh May 1, 2024
f0de6cf
Delete tests/reference/asr-test_const_dict-151acad.stdout
kmr-srbh May 1, 2024
6c086e3
Delete tests/reference/asr-test_const_dict-59445d7.json
kmr-srbh May 1, 2024
7e0a8c2
Delete tests/reference/asr-test_dict_const-69479e2.json
kmr-srbh May 1, 2024
e0c4d62
Delete tests/reference/asr-test_dict_const-69479e2.stderr
kmr-srbh May 1, 2024
3307acf
Delete tests/reference/asr-test_dict_const-69479e2.stdout
kmr-srbh May 1, 2024
aa6c245
Delete tests/reference/runtime-test_dict_const-62054df.json
kmr-srbh May 1, 2024
cab3eea
Delete tests/reference/runtime-test_dict_const-62054df.stderr
kmr-srbh May 1, 2024
c740e70
Delete tests/reference/asr-test_const_dict-59445d7.stderr
kmr-srbh May 1, 2024
9581a31
Tests: Update error references
kmr-srbh May 1, 2024
78e4205
Undo formatting changes
kmr-srbh May 1, 2024
891c001
Remove extra newline
kmr-srbh May 1, 2024
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
18 changes: 18 additions & 0 deletions integration_tests/test_dict_const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from lpython import i32, f64, str, dict, Const


def test_dict_const():
d_int: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}

assert d_int.get("a") == 1
assert d_int.keys() == ["c", "a", "b"]
assert d_int.values() == [3, 1, 2]

d_float: Const[dict[str, f64]] = {"a": 1.0, "b": 2.0, "c": 3.0}

assert d_float.get("a") == 1.0
assert d_float.keys() == ["c", "a", "b"]
assert d_float.values() == [3.0, 1.0, 2.0]


test_dict_const()
10 changes: 4 additions & 6 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}

void visit_ListItem(const ASR::ListItem_t& x) {
ASR::ttype_t* el_type = ASRUtils::get_contained_type(
ASRUtils::expr_type(x.m_a));
ASR::ttype_t *el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a));
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_a);
Expand All @@ -1540,8 +1539,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}

void visit_DictItem(const ASR::DictItem_t& x) {
ASR::Dict_t* dict_type = ASR::down_cast<ASR::Dict_t>(
ASRUtils::expr_type(x.m_a));
ASR::Dict_t *dict_type = ASR::down_cast<ASR::Dict_t>(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a)));
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved

kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 0;
this->visit_expr(*x.m_a);
Expand Down Expand Up @@ -1845,8 +1844,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}

void generate_DictElems(ASR::expr_t* m_arg, bool key_or_value) {
ASR::Dict_t* dict_type = ASR::down_cast<ASR::Dict_t>(
ASRUtils::expr_type(m_arg));
ASR::Dict_t *dict_type = ASR::down_cast<ASR::Dict_t>(ASRUtils::type_get_past_const(ASRUtils::expr_type(m_arg)));
ASR::ttype_t* el_type = key_or_value == 0 ?
dict_type->m_key_type : dict_type->m_value_type;

Expand Down
33 changes: 18 additions & 15 deletions src/libasr/pass/intrinsic_function_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2744,14 +2744,15 @@ namespace DictKeys {
static inline void verify_args(const ASR::IntrinsicScalarFunction_t& x, diag::Diagnostics& diagnostics) {
ASRUtils::require_impl(x.n_args == 1, "Call to dict.keys must have no argument",
x.base.base.loc, diagnostics);
ASRUtils::require_impl(ASR::is_a<ASR::Dict_t>(*ASRUtils::expr_type(x.m_args[0])),
"Argument to dict.keys must be of dict type",
x.base.base.loc, diagnostics);
ASR::ttype_t *dict_type = ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_args[0]));
ASRUtils::require_impl(ASR::is_a<ASR::Dict_t>(*dict_type),
"Argument to dict.keys must be of dict type",
x.base.base.loc, diagnostics);
ASRUtils::require_impl(ASR::is_a<ASR::List_t>(*x.m_type) &&
ASRUtils::check_equal_type(ASRUtils::get_contained_type(x.m_type),
ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_args[0]), 0)),
"Return type of dict.keys must be of list of dict key element type",
x.base.base.loc, diagnostics);
ASRUtils::check_equal_type(ASRUtils::get_contained_type(x.m_type),
ASR::down_cast<ASR::Dict_t>(dict_type)->m_key_type),
"Return type of dict.keys must be of list of dict key element type",
x.base.base.loc, diagnostics);
}

static inline ASR::expr_t *eval_dict_keys(Allocator &/*al*/,
Expand All @@ -2766,9 +2767,8 @@ static inline ASR::asr_t* create_DictKeys(Allocator& al, const Location& loc,
if (args.size() != 1) {
err("Call to dict.keys must have no argument", loc);
}

ASR::expr_t* dict_expr = args[0];
ASR::ttype_t *type = ASRUtils::expr_type(dict_expr);
ASR::ttype_t *type = ASRUtils::type_get_past_const(ASRUtils::expr_type(dict_expr));
ASR::ttype_t *dict_keys_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type;

Vec<ASR::expr_t*> arg_values;
Expand All @@ -2790,14 +2790,17 @@ namespace DictValues {
static inline void verify_args(const ASR::IntrinsicScalarFunction_t& x, diag::Diagnostics& diagnostics) {
ASRUtils::require_impl(x.n_args == 1, "Call to dict.values must have no argument",
x.base.base.loc, diagnostics);
ASRUtils::require_impl(ASR::is_a<ASR::Dict_t>(*ASRUtils::expr_type(x.m_args[0])),
ASR::ttype_t *dict_type = ASR::is_a<ASR::Const_t>(*ASRUtils::expr_type(x.m_args[0]))
? ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_args[0]))
: ASRUtils::expr_type(x.m_args[0]);
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
ASRUtils::require_impl(ASR::is_a<ASR::Dict_t>(*dict_type),
"Argument to dict.values must be of dict type",
x.base.base.loc, diagnostics);
ASRUtils::require_impl(ASR::is_a<ASR::List_t>(*x.m_type) &&
ASRUtils::check_equal_type(ASRUtils::get_contained_type(x.m_type),
ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_args[0]), 1)),
"Return type of dict.values must be of list of dict value element type",
x.base.base.loc, diagnostics);
ASRUtils::check_equal_type(ASRUtils::get_contained_type(x.m_type),
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
ASRUtils::get_contained_type(ASR::down_cast<ASR::Dict_t>(dict_type)->m_value_type, 1)),
"Return type of dict.values must be of list of dict value element type",
x.base.base.loc, diagnostics);
}

static inline ASR::expr_t *eval_dict_values(Allocator &/*al*/,
Expand All @@ -2814,7 +2817,7 @@ static inline ASR::asr_t* create_DictValues(Allocator& al, const Location& loc,
}

ASR::expr_t* dict_expr = args[0];
ASR::ttype_t *type = ASRUtils::expr_type(dict_expr);
ASR::ttype_t *type = ASRUtils::type_get_past_const(ASRUtils::expr_type(dict_expr));
ASR::ttype_t *dict_values_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type;

Vec<ASR::expr_t*> arg_values;
Expand Down
7 changes: 5 additions & 2 deletions src/lpython/semantics/python_attribute_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct AttributeHandler {
ASR::asr_t* get_attribute(ASR::expr_t *e, std::string attr_name,
Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
ASR::ttype_t *type = ASRUtils::expr_type(e);
std::string class_name = get_type_name(type);
std::string class_name = ASR::is_a<ASR::Const_t>(*type) ? "dict" : get_type_name(type);
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
if (class_name == "") {
throw SemanticError("Type name is not implemented yet.", loc);
}
Expand Down Expand Up @@ -331,7 +331,7 @@ struct AttributeHandler {
throw SemanticError("'get' takes atleast 1 and atmost 2 arguments",
loc);
}
ASR::ttype_t *type = ASRUtils::expr_type(s);
ASR::ttype_t *type = ASRUtils::type_get_past_const(ASRUtils::expr_type(s));
ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type;
ASR::ttype_t *value_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type;
if (args.size() == 2) {
Expand Down Expand Up @@ -366,6 +366,9 @@ struct AttributeHandler {

static ASR::asr_t* eval_dict_pop(ASR::expr_t *s, Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
if (ASR::is_a<ASR::Const_t>(*ASRUtils::expr_type(s))) {
throw SemanticError("cannot pop elements from a const dict", loc);
}
if (args.size() != 1) {
throw SemanticError("'pop' takes only one argument for now", loc);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/errors/test_dict_const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from lpython import i32, f64, str, dict, Const


def test_dict_const():
d: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3}
print(d.pop("a"))
i: i32 = d.pop("a")
kmr-srbh marked this conversation as resolved.
Show resolved Hide resolved
Loading