From dae7f50d886e7f21646013f600a10652e6658bf1 Mon Sep 17 00:00:00 2001 From: advik Date: Wed, 15 May 2024 22:50:19 +0530 Subject: [PATCH 1/3] Initialize empty value to dictionaries without type given --- src/lpython/semantics/python_ast_to_asr.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1538901fe0..e368f3b921 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2568,6 +2568,15 @@ class CommonVisitor : public AST::BaseVisitor { ASR::Variable_t* v_variable = ASR::down_cast(v_sym); std::string var_name = v_variable->m_name; ASR::ttype_t* type = v_variable->m_type; + if (!init_expr && ASR::is_a(*type)) { + ASR::ttype_t *key_type = ASR::down_cast(type)->m_key_type; + ASR::ttype_t *value_type = ASR::down_cast(type)->m_value_type; + ASR::ttype_t *dict_type = ASRUtils::TYPE(ASR::make_Dict_t(al, loc, + key_type, value_type)); + init_expr = ASRUtils::EXPR(ASR::make_DictConstant_t(al, loc, + nullptr, 0, nullptr, 0, type)); + } + if( init_expr ) { value = ASRUtils::expr_value(init_expr); SetChar variable_dependencies_vec; From ceb89dd7ef08b0a608b4a5eea03fa9383f8511e9 Mon Sep 17 00:00:00 2001 From: advik Date: Wed, 15 May 2024 22:56:16 +0530 Subject: [PATCH 2/3] Update tests --- tests/reference/asr-dictionary1-a105a36.json | 2 +- .../reference/asr-dictionary1-a105a36.stdout | 74 ++++++++++++++++++- .../asr-print_list_tuple_03-9de3736.json | 2 +- .../asr-print_list_tuple_03-9de3736.stdout | 29 ++++++++ ...ist_tuple-print_list_tuple_03-195fa9c.json | 2 +- ...t_tuple-print_list_tuple_03-195fa9c.stdout | 29 ++++++++ 6 files changed, 134 insertions(+), 4 deletions(-) diff --git a/tests/reference/asr-dictionary1-a105a36.json b/tests/reference/asr-dictionary1-a105a36.json index 991461787d..7191ef6371 100644 --- a/tests/reference/asr-dictionary1-a105a36.json +++ b/tests/reference/asr-dictionary1-a105a36.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-dictionary1-a105a36.stdout", - "stdout_hash": "3ea42309cc8f2201f43bb2fdeb28a85feea890fe49db4063af5c46f8", + "stdout_hash": "ac58817e3dc84de980d646cffeb63540c55bde9ca4229b8a7c58b77a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-dictionary1-a105a36.stdout b/tests/reference/asr-dictionary1-a105a36.stdout index 8ae305005f..192d0350b7 100644 --- a/tests/reference/asr-dictionary1-a105a36.stdout +++ b/tests/reference/asr-dictionary1-a105a36.stdout @@ -141,6 +141,18 @@ [] [] [(Assignment + (Var 3 x) + (DictConstant + [] + [] + (Dict + (Integer 4) + (Integer 4) + ) + ) + () + ) + (Assignment (Var 3 x) (DictConstant [(IntegerConstant 1 (Integer 4)) @@ -154,6 +166,18 @@ ) () ) + (Assignment + (Var 3 y) + (DictConstant + [] + [] + (Dict + (Character 1 -2 ()) + (Integer 4) + ) + ) + () + ) (Assignment (Var 3 y) (DictConstant @@ -286,6 +310,18 @@ [] [] [(Assignment + (Var 5 y) + (DictConstant + [] + [] + (Dict + (Character 1 -2 ()) + (Integer 4) + ) + ) + () + ) + (Assignment (Var 5 y) (DictConstant [(StringConstant @@ -390,6 +426,18 @@ [] [] [(Assignment + (Var 4 y) + (DictConstant + [] + [] + (Dict + (Character 1 -2 ()) + (Integer 4) + ) + ) + () + ) + (Assignment (Var 4 y) (DictConstant [(StringConstant @@ -494,6 +542,18 @@ [] [] [(Assignment + (Var 6 y) + (DictConstant + [] + [] + (Dict + (Character 1 -2 ()) + (Integer 4) + ) + ) + () + ) + (Assignment (Var 6 y) (DictConstant [(StringConstant @@ -574,7 +634,19 @@ ) [f] [] - [(SubroutineCall + [(Assignment + (Var 8 x) + (DictConstant + [] + [] + (Dict + (Integer 4) + (Integer 4) + ) + ) + () + ) + (SubroutineCall 2 f () [((Var 8 x))] diff --git a/tests/reference/asr-print_list_tuple_03-9de3736.json b/tests/reference/asr-print_list_tuple_03-9de3736.json index 857cf48d38..5a107ca056 100644 --- a/tests/reference/asr-print_list_tuple_03-9de3736.json +++ b/tests/reference/asr-print_list_tuple_03-9de3736.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-print_list_tuple_03-9de3736.stdout", - "stdout_hash": "8962f3d49727ceb8f899acc2382f5fb6d24b16506a154ccf907400f5", + "stdout_hash": "9bc9712a40c386ddbd519614bb9ed900ebde24b5db7d0876f7e88e95", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-print_list_tuple_03-9de3736.stdout b/tests/reference/asr-print_list_tuple_03-9de3736.stdout index 9e0bc45dec..dfd164741e 100644 --- a/tests/reference/asr-print_list_tuple_03-9de3736.stdout +++ b/tests/reference/asr-print_list_tuple_03-9de3736.stdout @@ -110,6 +110,21 @@ [] [] [(Assignment + (Var 3 x) + (DictConstant + [] + [] + (Dict + (Integer 4) + (Tuple + [(Integer 4) + (Integer 4)] + ) + ) + ) + () + ) + (Assignment (Var 3 x) (DictConstant [(IntegerConstant 1 (Integer 4)) @@ -140,6 +155,20 @@ ) () ) + (Assignment + (Var 3 y) + (DictConstant + [] + [] + (Dict + (Integer 4) + (List + (Integer 4) + ) + ) + ) + () + ) (Assignment (Var 3 y) (DictConstant diff --git a/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.json b/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.json index c2fa5807d7..7734699e6f 100644 --- a/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.json +++ b/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout", - "stdout_hash": "f63197ac9c1a649cfb2d3a3ef6f6672964ad753593afc68ce6d567e9", + "stdout_hash": "edb9d31c77ac27a72de4454275693936ef43c07263a2da687e16da5c", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout b/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout index aa210f8619..98feee42a5 100644 --- a/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout +++ b/tests/reference/pass_print_list_tuple-print_list_tuple_03-195fa9c.stdout @@ -144,6 +144,21 @@ [] [] [(Assignment + (Var 3 x) + (DictConstant + [] + [] + (Dict + (Integer 4) + (Tuple + [(Integer 4) + (Integer 4)] + ) + ) + ) + () + ) + (Assignment (Var 3 x) (DictConstant [(IntegerConstant 1 (Integer 4)) @@ -174,6 +189,20 @@ ) () ) + (Assignment + (Var 3 y) + (DictConstant + [] + [] + (Dict + (Integer 4) + (List + (Integer 4) + ) + ) + ) + () + ) (Assignment (Var 3 y) (DictConstant From 811673276a7402c8ba8a7ee9a02befdf91dd5fff Mon Sep 17 00:00:00 2001 From: advik Date: Wed, 15 May 2024 23:00:47 +0530 Subject: [PATCH 3/3] Remove dead code --- src/lpython/semantics/python_ast_to_asr.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index e368f3b921..221ac09ce4 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2569,10 +2569,6 @@ class CommonVisitor : public AST::BaseVisitor { std::string var_name = v_variable->m_name; ASR::ttype_t* type = v_variable->m_type; if (!init_expr && ASR::is_a(*type)) { - ASR::ttype_t *key_type = ASR::down_cast(type)->m_key_type; - ASR::ttype_t *value_type = ASR::down_cast(type)->m_value_type; - ASR::ttype_t *dict_type = ASRUtils::TYPE(ASR::make_Dict_t(al, loc, - key_type, value_type)); init_expr = ASRUtils::EXPR(ASR::make_DictConstant_t(al, loc, nullptr, 0, nullptr, 0, type)); }