From 1a2e739b0fdcd237268c48099f7c3f53ecfd67a5 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 19:31:48 +0200 Subject: [PATCH 01/40] Fixed Issue #2042 --- src/lpython/semantics/python_ast_to_asr.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 59640c6fd8..b247e4c461 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5167,6 +5167,38 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); + //Construction-While-Assigning Problem + AST::exprType value_type_ast = (x.m_value)->type; + AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. + AST::exprType::Tuple,AST::exprType::List}; + for(AST::exprType &exp : objects_to_check){ + if (exp == value_type_ast){ + ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); + try{ + this->visit_expr(*x.m_value); + } + catch (SemanticError &error){ + ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + diag.add(diag::Diagnostic( + "Type mismatch in assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) + }) + ); + throw SemanticAbort(); + } + } + } + + } + + + this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6629,6 +6661,9 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //set the tmp to use it in the error message. + ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From 1adbddc7d90eb894c7d7be1a8745422b67040d5e Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 02/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b247e4c461..0c9b6410c2 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5169,8 +5169,9 @@ class BodyVisitor : public CommonVisitor { assign_asr_target = ASRUtils::EXPR(tmp); //Construction-While-Assigning Problem AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. - AST::exprType::Tuple,AST::exprType::List}; + AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. + AST::exprType::Dict, + AST::exprType::List }; for(AST::exprType &exp : objects_to_check){ if (exp == value_type_ast){ ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); @@ -5335,6 +5336,10 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); + tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, + list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6181,6 +6186,16 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { + //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) + Vec values; + ASR::ttype_t* value_type = nullptr; + visit_expr(*x.m_values[0]); + ASR::expr_t *value = ASRUtils::EXPR(tmp); + value_type = ASRUtils::expr_type(value); + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } @@ -6197,6 +6212,11 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6661,7 +6681,7 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message. + //set the tmp to use it in the error message.(copied from the end of this function) ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", From 9fbd33e3ce5007488994fe5d9fcef165bac912bf Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 21:12:23 +0200 Subject: [PATCH 03/40] Fix Warning :UnInitialized Vec --- src/lpython/semantics/python_ast_to_asr.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 0c9b6410c2..c675a3124b 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6188,6 +6188,7 @@ class BodyVisitor : public CommonVisitor { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) Vec values; + values.reserve(al, x.n_values); ASR::ttype_t* value_type = nullptr; visit_expr(*x.m_values[0]); ASR::expr_t *value = ASRUtils::EXPR(tmp); From 0e14c0aabeb8f1c73e8188c24ac87b5603d43a36 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 19:31:48 +0200 Subject: [PATCH 04/40] Fixed Issue #2042 --- src/lpython/semantics/python_ast_to_asr.cpp | 36 --------------------- 1 file changed, 36 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index c675a3124b..00af060bcc 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5167,39 +5167,6 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); - //Construction-While-Assigning Problem - AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. - AST::exprType::Dict, - AST::exprType::List }; - for(AST::exprType &exp : objects_to_check){ - if (exp == value_type_ast){ - ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); - try{ - this->visit_expr(*x.m_value); - } - catch (SemanticError &error){ - ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); - ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); - if (!ASRUtils::check_equal_type(target_type, value_type)){ - std::string ltype = ASRUtils::type_to_str_python(target_type); - std::string rtype = ASRUtils::type_to_str_python(value_type); - diag.add(diag::Diagnostic( - "Type mismatch in assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) - }) - ); - throw SemanticAbort(); - } - } - } - - } - - - this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6682,9 +6649,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); - tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From abcd34a432f3a9ba82695657aaf1ea8275d6cb93 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 05/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 46 ++++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 00af060bcc..6d2befa6cf 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5167,6 +5167,38 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); + //Construction-While-Assigning Problem + AST::exprType value_type_ast = (x.m_value)->type; + AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. + AST::exprType::Tuple,AST::exprType::List}; + for(AST::exprType &exp : objects_to_check){ + if (exp == value_type_ast){ + ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); + try{ + this->visit_expr(*x.m_value); + } + catch (SemanticError &error){ + ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + diag.add(diag::Diagnostic( + "Type mismatch in assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) + }) + ); + throw SemanticAbort(); + } + } + } + + } + + + this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6153,17 +6185,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { - //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) - Vec values; - values.reserve(al, x.n_values); - ASR::ttype_t* value_type = nullptr; - visit_expr(*x.m_values[0]); - ASR::expr_t *value = ASRUtils::EXPR(tmp); - value_type = ASRUtils::expr_type(value); - ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, - key_type, value_type)); - tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), - values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } @@ -6649,6 +6670,9 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //set the tmp to use it in the error message. + ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From d7869b1782d6c8ea498799de57110150f90c0261 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 19:31:48 +0200 Subject: [PATCH 06/40] Fixed Issue #2042 --- src/lpython/semantics/python_ast_to_asr.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 6d2befa6cf..32869447a5 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5169,8 +5169,9 @@ class BodyVisitor : public CommonVisitor { assign_asr_target = ASRUtils::EXPR(tmp); //Construction-While-Assigning Problem AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. - AST::exprType::Tuple,AST::exprType::List}; + AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. + AST::exprType::Dict, + AST::exprType::List }; for(AST::exprType &exp : objects_to_check){ if (exp == value_type_ast){ ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); @@ -6670,7 +6671,7 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message. + //set the tmp to use it in the error message.(copied from the end of this function) ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", From fa18fa77e8403f8e89316508af4b5f000c1eb7c6 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 07/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 36 --------------------- 1 file changed, 36 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 32869447a5..1c052c4018 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5167,39 +5167,6 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); - //Construction-While-Assigning Problem - AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. - AST::exprType::Dict, - AST::exprType::List }; - for(AST::exprType &exp : objects_to_check){ - if (exp == value_type_ast){ - ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); - try{ - this->visit_expr(*x.m_value); - } - catch (SemanticError &error){ - ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); - ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); - if (!ASRUtils::check_equal_type(target_type, value_type)){ - std::string ltype = ASRUtils::type_to_str_python(target_type); - std::string rtype = ASRUtils::type_to_str_python(value_type); - diag.add(diag::Diagnostic( - "Type mismatch in assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) - }) - ); - throw SemanticAbort(); - } - } - } - - } - - - this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6671,9 +6638,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); - tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From 7f85dc2f5e642c9864648862e4c6c6b4147375d8 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 02:11:48 +0200 Subject: [PATCH 08/40] Default Arguments Implemented --- src/lpython/semantics/python_ast_to_asr.cpp | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1c052c4018..c0ab7f1aa5 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1143,6 +1143,19 @@ class CommonVisitor : public AST::BaseVisitor { visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); } + if((n_pos_args+ n_kwargs) < func->n_args){ + for(size_t def_arg = (n_pos_args+ n_kwargs) ; def_arg <(func->n_args) ;def_arg++){ + if(! ((func->m_args)[def_arg]) ) { + break; + } + ASR::call_arg_t call_arg; + args.push_back(al,call_arg); + ASR::symbol_t* sym = (ASR::down_cast((func->m_args)[def_arg]))->m_v; + ASR::Variable_t* var = ASR::down_cast(sym); + args.p[def_arg].loc = ((var->m_value)->base).loc; + args.p[def_arg].m_value = (var->m_value); + } + } if (ASRUtils::get_FunctionType(func)->m_is_restriction) { rt_vec.push_back(s); } else if (ASRUtils::is_generic_function(s)) { @@ -4269,6 +4282,8 @@ class SymbolTableVisitor : public CommonVisitor { throw SemanticError("Function " + std::string(x.m_name) + " is already defined", x.base.base.loc); } bool is_allocatable = false, is_const = false; + size_t default_arg_index_start = x.m_args.n_args - x.m_args.n_defaults; + for (size_t i=0; i { std::string arg_s = arg; ASR::expr_t *value = nullptr; ASR::expr_t *init_expr = nullptr; + if (i >= default_arg_index_start) + { + size_t default_arg_index = i - default_arg_index_start; + this->visit_expr(*(x.m_args.m_defaults[default_arg_index])); + value = ASRUtils::EXPR(tmp); + init_expr = value; + } if (s_intent == ASRUtils::intent_unspecified) { s_intent = ASRUtils::intent_in; if (ASRUtils::is_array(arg_type)) { @@ -4308,6 +4330,9 @@ class SymbolTableVisitor : public CommonVisitor { } ASR::accessType s_access = ASR::accessType::Public; ASR::presenceType s_presence = ASR::presenceType::Required; + if (i >= default_arg_index_start){ + s_presence = ASR::presenceType::Optional; + } bool value_attr = false; if (current_procedure_abi_type == ASR::abiType::BindC) { value_attr = true; From 649b0796512c6cad0c6fd5ec7220c255e2f7ab61 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 02:43:35 +0200 Subject: [PATCH 09/40] Default_argumets_implemented -removed other branch edit --- src/lpython/semantics/python_ast_to_asr.cpp | 53 +++++++++++++++------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index c0ab7f1aa5..a5370e7ea6 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1143,7 +1143,7 @@ class CommonVisitor : public AST::BaseVisitor { visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); } - if((n_pos_args+ n_kwargs) < func->n_args){ + if((n_pos_args+ n_kwargs) < func->n_args){ for(size_t def_arg = (n_pos_args+ n_kwargs) ; def_arg <(func->n_args) ;def_arg++){ if(! ((func->m_args)[def_arg]) ) { break; @@ -4305,8 +4305,7 @@ class SymbolTableVisitor : public CommonVisitor { std::string arg_s = arg; ASR::expr_t *value = nullptr; ASR::expr_t *init_expr = nullptr; - if (i >= default_arg_index_start) - { + if (i >= default_arg_index_start){ size_t default_arg_index = i - default_arg_index_start; this->visit_expr(*(x.m_args.m_defaults[default_arg_index])); value = ASRUtils::EXPR(tmp); @@ -4330,8 +4329,8 @@ class SymbolTableVisitor : public CommonVisitor { } ASR::accessType s_access = ASR::accessType::Public; ASR::presenceType s_presence = ASR::presenceType::Required; - if (i >= default_arg_index_start){ - s_presence = ASR::presenceType::Optional; + if (i >= default_arg_index_start){ + s_presence = ASR::presenceType::Optional; } bool value_attr = false; if (current_procedure_abi_type == ASR::abiType::BindC) { @@ -5192,6 +5191,38 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); + //Construction-While-Assigning Problem + AST::exprType value_type_ast = (x.m_value)->type; + AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. + AST::exprType::Tuple,AST::exprType::List}; + for(AST::exprType &exp : objects_to_check){ + if (exp == value_type_ast){ + ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); + try{ + this->visit_expr(*x.m_value); + } + catch (SemanticError &error){ + ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + diag.add(diag::Diagnostic( + "Type mismatch in assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) + }) + ); + throw SemanticAbort(); + } + } + } + + } + + + this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -5328,10 +5359,6 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); - tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, - list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6194,11 +6221,6 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, - key_type, value_type)); - tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), - values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6663,6 +6685,9 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //set the tmp to use it in the error message. + ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From 2902eea30c81791bff1fac6e18e64a9a54c8c8e0 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 03:17:48 +0200 Subject: [PATCH 10/40] setting loc makes a serious problem with complex() --- src/lpython/semantics/python_ast_to_asr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index a5370e7ea6..d0318a8adf 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1152,7 +1152,7 @@ class CommonVisitor : public AST::BaseVisitor { args.push_back(al,call_arg); ASR::symbol_t* sym = (ASR::down_cast((func->m_args)[def_arg]))->m_v; ASR::Variable_t* var = ASR::down_cast(sym); - args.p[def_arg].loc = ((var->m_value)->base).loc; + // args.p[def_arg].loc = ((var->m_value)->base).loc; args.p[def_arg].m_value = (var->m_value); } } From d3734db9b6d49774114efdce6907fdcfdcc18dc9 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 04:42:30 +0200 Subject: [PATCH 11/40] handled function call without apearent argument passed --- src/lpython/semantics/python_ast_to_asr.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d0318a8adf..2ee0dfb5b2 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1143,7 +1143,7 @@ class CommonVisitor : public AST::BaseVisitor { visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); } - if((n_pos_args+ n_kwargs) < func->n_args){ + if((n_pos_args+ n_kwargs) < func->n_args && (args.size() < func->n_args) ){ for(size_t def_arg = (n_pos_args+ n_kwargs) ; def_arg <(func->n_args) ;def_arg++){ if(! ((func->m_args)[def_arg]) ) { break; @@ -1152,7 +1152,7 @@ class CommonVisitor : public AST::BaseVisitor { args.push_back(al,call_arg); ASR::symbol_t* sym = (ASR::down_cast((func->m_args)[def_arg]))->m_v; ASR::Variable_t* var = ASR::down_cast(sym); - // args.p[def_arg].loc = ((var->m_value)->base).loc; + args.p[def_arg].loc = ((var->m_value)->base).loc; args.p[def_arg].m_value = (var->m_value); } } @@ -1200,6 +1200,7 @@ class CommonVisitor : public AST::BaseVisitor { return make_call_helper(al, t, current_scope, new_args, new_call_name, loc); } if (args.size() != func->n_args) { + std::cout<<"\nthis happened"; std::string fnd = std::to_string(args.size()); std::string org = std::to_string(func->n_args); diag.add(diag::Diagnostic( From f8b7c97f7a414e82095b09fd7ab715b2fa9a031c Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 05:43:11 +0200 Subject: [PATCH 12/40] removed print statement usedin debugging --- src/lpython/semantics/python_ast_to_asr.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 2ee0dfb5b2..989a70d5bf 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1200,7 +1200,6 @@ class CommonVisitor : public AST::BaseVisitor { return make_call_helper(al, t, current_scope, new_args, new_call_name, loc); } if (args.size() != func->n_args) { - std::cout<<"\nthis happened"; std::string fnd = std::to_string(args.size()); std::string org = std::to_string(func->n_args); diag.add(diag::Diagnostic( From c4ac4f3863db749a50cb32b8e902ad9794fee8e5 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 05:54:05 +0200 Subject: [PATCH 13/40] added proper handling for nullptr values (they won't break the code anyway) --- src/lpython/semantics/python_ast_to_asr.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 989a70d5bf..6b3aa0b00f 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1145,13 +1145,13 @@ class CommonVisitor : public AST::BaseVisitor { } if((n_pos_args+ n_kwargs) < func->n_args && (args.size() < func->n_args) ){ for(size_t def_arg = (n_pos_args+ n_kwargs) ; def_arg <(func->n_args) ;def_arg++){ - if(! ((func->m_args)[def_arg]) ) { - break; - } ASR::call_arg_t call_arg; args.push_back(al,call_arg); ASR::symbol_t* sym = (ASR::down_cast((func->m_args)[def_arg]))->m_v; ASR::Variable_t* var = ASR::down_cast(sym); + if(!(var->m_value)) { + break; + } args.p[def_arg].loc = ((var->m_value)->base).loc; args.p[def_arg].m_value = (var->m_value); } From 1f3d26fba12bdc1042726183e95a2a38c760e6f1 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 20 Mar 2024 06:35:16 +0200 Subject: [PATCH 14/40] minor edit --- src/lpython/semantics/python_ast_to_asr.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 6b3aa0b00f..06f180ec43 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1145,13 +1145,13 @@ class CommonVisitor : public AST::BaseVisitor { } if((n_pos_args+ n_kwargs) < func->n_args && (args.size() < func->n_args) ){ for(size_t def_arg = (n_pos_args+ n_kwargs) ; def_arg <(func->n_args) ;def_arg++){ - ASR::call_arg_t call_arg; - args.push_back(al,call_arg); ASR::symbol_t* sym = (ASR::down_cast((func->m_args)[def_arg]))->m_v; ASR::Variable_t* var = ASR::down_cast(sym); if(!(var->m_value)) { break; } + ASR::call_arg_t call_arg; + args.push_back(al,call_arg); args.p[def_arg].loc = ((var->m_value)->base).loc; args.p[def_arg].m_value = (var->m_value); } From 2cf7f4f1f23a2e9d42d234d78dce0c5d4f0ce01e Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 21 Mar 2024 06:25:15 +0200 Subject: [PATCH 15/40] handled problem in creating c code (c dosen't support default arguments) --- src/libasr/codegen/asr_to_c_cpp.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index fbbaa35fc4..9b980767ad 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -565,6 +565,7 @@ R"(#include if( is_c ) { CDeclarationOptions c_decl_options; c_decl_options.pre_initialise_derived_type = false; + c_decl_options.do_not_initialize = true; func += self().convert_variable_decl(*arg, &c_decl_options); } else { CPPDeclarationOptions cpp_decl_options; From 35b5faa59e790bb14e385f76208b8a948f65c12f Mon Sep 17 00:00:00 2001 From: Assem Medhat <75497299+assem2002@users.noreply.github.com> Date: Sun, 24 Mar 2024 06:05:14 +0200 Subject: [PATCH 16/40] Apply suggestions from code review Co-authored-by: Shaikh Ubaid --- src/lpython/semantics/python_ast_to_asr.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 06f180ec43..7de2b1fe28 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1144,10 +1144,10 @@ class CommonVisitor : public AST::BaseVisitor { args, rt_subs, func, loc); } if((n_pos_args+ n_kwargs) < func->n_args && (args.size() < func->n_args) ){ - for(size_t def_arg = (n_pos_args+ n_kwargs) ; def_arg <(func->n_args) ;def_arg++){ - ASR::symbol_t* sym = (ASR::down_cast((func->m_args)[def_arg]))->m_v; + for (size_t def_arg = (n_pos_args+ n_kwargs); def_arg < func->n_args; def_arg++){ + ASR::symbol_t* sym = ASR::down_cast(func->m_args[def_arg])->m_v; ASR::Variable_t* var = ASR::down_cast(sym); - if(!(var->m_value)) { + if (var->m_value == nullptr) { break; } ASR::call_arg_t call_arg; @@ -4329,7 +4329,7 @@ class SymbolTableVisitor : public CommonVisitor { } ASR::accessType s_access = ASR::accessType::Public; ASR::presenceType s_presence = ASR::presenceType::Required; - if (i >= default_arg_index_start){ + if (i >= default_arg_index_start){ s_presence = ASR::presenceType::Optional; } bool value_attr = false; From a4d4d9c4f89d0cfd4df4ed9afa24c8b9c8808590 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 27 Mar 2024 07:13:37 +0200 Subject: [PATCH 17/40] applied code review changes (by Shaikh Ubaid) --- src/lpython/semantics/python_ast_to_asr.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 7de2b1fe28..402f3391a6 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1143,8 +1143,8 @@ class CommonVisitor : public AST::BaseVisitor { visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); } - if((n_pos_args+ n_kwargs) < func->n_args && (args.size() < func->n_args) ){ - for (size_t def_arg = (n_pos_args+ n_kwargs); def_arg < func->n_args; def_arg++){ + if(args.size() < func->n_args){ + for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ ASR::symbol_t* sym = ASR::down_cast(func->m_args[def_arg])->m_v; ASR::Variable_t* var = ASR::down_cast(sym); if (var->m_value == nullptr) { From 8ec5b38157613fddb035bf69e37f8c3bcd41d6c0 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 27 Mar 2024 08:54:59 +0200 Subject: [PATCH 18/40] added integration test for function default arguments --- integration_tests/def_func_01.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 integration_tests/def_func_01.py diff --git a/integration_tests/def_func_01.py b/integration_tests/def_func_01.py new file mode 100644 index 0000000000..a3ac163f99 --- /dev/null +++ b/integration_tests/def_func_01.py @@ -0,0 +1,68 @@ +from lpython import i32,i64 + +def factorial_1(x: i32, y:i32 =1) ->i32 : + if x <= 1: + return y + return x * factorial_1(x-1) + +def factorial_2(x: i32, y:i32=3 ,z:i32 =2) ->i32: + if x ==4: + return x * y * z + return x * factorial_2(x-1) + +def default_func(x : str ="Hello" ,y : str = " ", z : str = "World") ->str: + return x + y + z + + +def even_positons(iterator : i32 , to_add : str = "?")-> str: + if (iterator == 10): return "" + + if iterator%2 == 0 : + return to_add + even_positons(iterator+1,"X") + + return to_add +even_positons(iterator+1) + + + +def test_all(): + + test_01 : i32 = factorial_1(5,0) + print("test_01 is =>",test_01) + assert test_01 == 120 + + test_02 : i32 = factorial_1(1,5555) + print("test_02 is =>",test_02) + assert test_02 == 5555 + + test_03 : i32 =factorial_2(5,99999,99999) + print("test_03 is =>",test_03) + assert test_03 == 120 + + test_04 : i32 = factorial_2(4,-1,100) + print("test_04 is =>",test_04) + assert test_04 == -400 + + test_05 :str = default_func() + print("test_05 is =>",test_05) + assert test_05 == "Hello World" + + test_06 :str = default_func(y = "|||",x="Hi") + print("test_06 is =>",test_06) + assert test_06 == "Hi|||World" + + test_07 : str = even_positons(0) + print("test_07 is =>",test_07) + assert test_07 == "?X?X?X?X?X" + + test_08 : str = even_positons(0,"W") + print("test_08 is =>",test_08) + assert test_08 == "WX?X?X?X?X" + +test_all() + + + + + + + \ No newline at end of file From 3bb336c996912b4b62132800c966a79d6dd4d7b5 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 27 Mar 2024 09:09:26 +0200 Subject: [PATCH 19/40] added the 'def_func_01.py' to cmake testing list --- integration_tests/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index d19918b843..ac0fbabb9d 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -790,6 +790,8 @@ RUN(NAME test_statistics_01 LABELS cpython llvm llvm_jit NOFAST) RUN(NAME test_statistics_02 LABELS cpython llvm llvm_jit NOFAST REQ_PY_VER 3.10) RUN(NAME test_str_attributes LABELS cpython llvm llvm_jit c) RUN(NAME kwargs_01 LABELS cpython llvm llvm_jit c NOFAST) +RUN(NAME def_func_01 LABELS cpython llvm c) + RUN(NAME func_inline_01 LABELS llvm llvm_jit c wasm) RUN(NAME func_inline_02 LABELS cpython llvm llvm_jit c) From c7ff531309df75c8469b9d3780a692860db875c4 Mon Sep 17 00:00:00 2001 From: Assem Date: Sun, 28 Apr 2024 06:54:57 +0300 Subject: [PATCH 20/40] searching in symbol table for default arguments --- integration_tests/def_func_01.py | 8 +++ src/lpython/semantics/python_ast_to_asr.cpp | 73 ++++++++------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/integration_tests/def_func_01.py b/integration_tests/def_func_01.py index a3ac163f99..db281b4c07 100644 --- a/integration_tests/def_func_01.py +++ b/integration_tests/def_func_01.py @@ -50,6 +50,7 @@ def test_all(): print("test_06 is =>",test_06) assert test_06 == "Hi|||World" + test_07 : str = even_positons(0) print("test_07 is =>",test_07) assert test_07 == "?X?X?X?X?X" @@ -58,6 +59,13 @@ def test_all(): print("test_08 is =>",test_08) assert test_08 == "WX?X?X?X?X" + test_09 :str = default_func(y = "++",z = "LPython") + print("test_09 is =>",test_09) + assert test_09 == "Hello++LPython" + + test_10 :str = default_func("Welcome",z = "LPython") + print("test_10 is =>",test_10) + assert test_10 == "Welcome LPython" test_all() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 402f3391a6..b6e0152f82 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -659,7 +659,7 @@ class CommonVisitor : public AST::BaseVisitor { // Fill the whole call_args_vec with nullptr // This is for error handling later on. - for( size_t i = 0; i < n_pos_args + n_kwargs; i++ ) { + for( size_t i = 0; i < call_args_vec.max; i++ ) { ASR::call_arg_t call_arg; Location loc; loc.first = loc.last = 1; @@ -704,6 +704,21 @@ class CommonVisitor : public AST::BaseVisitor { call_args_vec.p[arg_pos].loc = expr->base.loc; call_args_vec.p[arg_pos].m_value = expr; } + //Filling missing arguments with its default argument passed in function definition (if existed). + for(size_t i = 0; i < orig_func->n_args; i++ ){ + if(call_args_vec.p[i].m_value == nullptr){ + ASR::symbol_t* sym = ASR::down_cast(orig_func->m_args[i])->m_v; + std::string variable_name = ASR::down_cast(sym)->m_name; + ASR::Variable_t* var = ASR::down_cast(orig_func->m_symtab->get_symbol(variable_name)); + if (var->m_symbolic_value == nullptr){ + call_args_vec.reserve(al, n_pos_args + n_kwargs); //deprecate Vec container, so it raise error later. + break; + } + else{ + call_args_vec.p[i].m_value = var->m_symbolic_value; + } + } + } return true; } @@ -1139,21 +1154,22 @@ class CommonVisitor : public AST::BaseVisitor { if (ASR::is_a(*s)) { ASR::Function_t *func = ASR::down_cast(s); if( n_kwargs > 0 && !is_generic_procedure ) { - args.reserve(al, n_pos_args + n_kwargs); + args.reserve(al, func->n_args); visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); } - if(args.size() < func->n_args){ + else if(args.size() < func->n_args){ for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ ASR::symbol_t* sym = ASR::down_cast(func->m_args[def_arg])->m_v; - ASR::Variable_t* var = ASR::down_cast(sym); - if (var->m_value == nullptr) { + std::string variable_name = ASR::down_cast(sym)->m_name; + ASR::Variable_t* var = ASR::down_cast(func->m_symtab->get_symbol(variable_name)); + if(var->m_symbolic_value == nullptr) { break; } ASR::call_arg_t call_arg; - args.push_back(al,call_arg); - args.p[def_arg].loc = ((var->m_value)->base).loc; - args.p[def_arg].m_value = (var->m_value); + call_arg.m_value = var->m_symbolic_value; + call_arg.loc = (var->m_symbolic_value->base).loc; + args.push_back(al,call_arg); } } if (ASRUtils::get_FunctionType(func)->m_is_restriction) { @@ -4283,7 +4299,6 @@ class SymbolTableVisitor : public CommonVisitor { } bool is_allocatable = false, is_const = false; size_t default_arg_index_start = x.m_args.n_args - x.m_args.n_defaults; - for (size_t i=0; i { if (i >= default_arg_index_start){ size_t default_arg_index = i - default_arg_index_start; this->visit_expr(*(x.m_args.m_defaults[default_arg_index])); - value = ASRUtils::EXPR(tmp); - init_expr = value; + init_expr = ASRUtils::EXPR(tmp); } if (s_intent == ASRUtils::intent_unspecified) { s_intent = ASRUtils::intent_in; @@ -4329,7 +4343,7 @@ class SymbolTableVisitor : public CommonVisitor { } ASR::accessType s_access = ASR::accessType::Public; ASR::presenceType s_presence = ASR::presenceType::Required; - if (i >= default_arg_index_start){ + if (i >= default_arg_index_start){ s_presence = ASR::presenceType::Optional; } bool value_attr = false; @@ -5191,38 +5205,6 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); - //Construction-While-Assigning Problem - AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. - AST::exprType::Tuple,AST::exprType::List}; - for(AST::exprType &exp : objects_to_check){ - if (exp == value_type_ast){ - ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); - try{ - this->visit_expr(*x.m_value); - } - catch (SemanticError &error){ - ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); - ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); - if (!ASRUtils::check_equal_type(target_type, value_type)){ - std::string ltype = ASRUtils::type_to_str_python(target_type); - std::string rtype = ASRUtils::type_to_str_python(value_type); - diag.add(diag::Diagnostic( - "Type mismatch in assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) - }) - ); - throw SemanticAbort(); - } - } - } - - } - - - this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6685,9 +6667,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message. - ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); - tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From 3423d9bf280b77e0f100e4125d014aeccad1214c Mon Sep 17 00:00:00 2001 From: Assem Medhat Date: Mon, 29 Apr 2024 23:07:23 +0300 Subject: [PATCH 21/40] Update src/lpython/semantics/python_ast_to_asr.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Čertík --- src/lpython/semantics/python_ast_to_asr.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b6e0152f82..125463db37 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1157,8 +1157,7 @@ class CommonVisitor : public AST::BaseVisitor { args.reserve(al, func->n_args); visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); - } - else if(args.size() < func->n_args){ + } else if (args.size() < func->n_args) { for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ ASR::symbol_t* sym = ASR::down_cast(func->m_args[def_arg])->m_v; std::string variable_name = ASR::down_cast(sym)->m_name; From 8748b4214e67d707f9eec73825376da820ce53ae Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 19:31:48 +0200 Subject: [PATCH 22/40] Fixed Issue #2042 --- src/lpython/semantics/python_ast_to_asr.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 125463db37..6426f30ecc 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5204,6 +5204,38 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); + //Construction-While-Assigning Problem + AST::exprType value_type_ast = (x.m_value)->type; + AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. + AST::exprType::Tuple,AST::exprType::List}; + for(AST::exprType &exp : objects_to_check){ + if (exp == value_type_ast){ + ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); + try{ + this->visit_expr(*x.m_value); + } + catch (SemanticError &error){ + ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + diag.add(diag::Diagnostic( + "Type mismatch in assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) + }) + ); + throw SemanticAbort(); + } + } + } + + } + + + this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6666,6 +6698,9 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //set the tmp to use it in the error message. + ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From be236f85b1834967fbebfb4f20b10b7b196676a8 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 23/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 6426f30ecc..f658a0bcce 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5206,8 +5206,9 @@ class BodyVisitor : public CommonVisitor { assign_asr_target = ASRUtils::EXPR(tmp); //Construction-While-Assigning Problem AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. - AST::exprType::Tuple,AST::exprType::List}; + AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. + AST::exprType::Dict, + AST::exprType::List }; for(AST::exprType &exp : objects_to_check){ if (exp == value_type_ast){ ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); @@ -5372,6 +5373,10 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); + tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, + list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6218,6 +6223,16 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { + //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) + Vec values; + ASR::ttype_t* value_type = nullptr; + visit_expr(*x.m_values[0]); + ASR::expr_t *value = ASRUtils::EXPR(tmp); + value_type = ASRUtils::expr_type(value); + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } @@ -6234,6 +6249,11 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6698,7 +6718,7 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message. + //set the tmp to use it in the error message.(copied from the end of this function) ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", From c286d39f8ec4f0a6cc1948e8f16e0042c696c6c8 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 24/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 43 --------------------- 1 file changed, 43 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index f658a0bcce..f3ab857b4f 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5204,39 +5204,6 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); - //Construction-While-Assigning Problem - AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. - AST::exprType::Dict, - AST::exprType::List }; - for(AST::exprType &exp : objects_to_check){ - if (exp == value_type_ast){ - ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); - try{ - this->visit_expr(*x.m_value); - } - catch (SemanticError &error){ - ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); - ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); - if (!ASRUtils::check_equal_type(target_type, value_type)){ - std::string ltype = ASRUtils::type_to_str_python(target_type); - std::string rtype = ASRUtils::type_to_str_python(value_type); - diag.add(diag::Diagnostic( - "Type mismatch in assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) - }) - ); - throw SemanticAbort(); - } - } - } - - } - - - this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6223,16 +6190,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { - //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) - Vec values; - ASR::ttype_t* value_type = nullptr; - visit_expr(*x.m_values[0]); - ASR::expr_t *value = ASRUtils::EXPR(tmp); - value_type = ASRUtils::expr_type(value); - ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, - key_type, value_type)); - tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), - values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } From b34a32b3b98792967297cef833dbc2387b1ca9fc Mon Sep 17 00:00:00 2001 From: Assem Date: Mon, 29 Apr 2024 23:25:44 +0300 Subject: [PATCH 25/40] added test_00 --- integration_tests/def_func_01.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/integration_tests/def_func_01.py b/integration_tests/def_func_01.py index db281b4c07..5bc16bdeaf 100644 --- a/integration_tests/def_func_01.py +++ b/integration_tests/def_func_01.py @@ -14,18 +14,22 @@ def default_func(x : str ="Hello" ,y : str = " ", z : str = "World") ->str: return x + y + z -def even_positons(iterator : i32 , to_add : str = "?")-> str: +def even_positions(iterator : i32 , to_add : str = "?")-> str: if (iterator == 10): return "" if iterator%2 == 0 : - return to_add + even_positons(iterator+1,"X") + return to_add + even_positions(iterator+1,"X") - return to_add +even_positons(iterator+1) + return to_add +even_positions(iterator+1) def test_all(): + test_00 : i32 = factorial_1(1) + print("test_00 is =>",test_00) + assert test_00 == 1 + test_01 : i32 = factorial_1(5,0) print("test_01 is =>",test_01) assert test_01 == 120 @@ -51,11 +55,11 @@ def test_all(): assert test_06 == "Hi|||World" - test_07 : str = even_positons(0) + test_07 : str = even_positions(0) print("test_07 is =>",test_07) assert test_07 == "?X?X?X?X?X" - test_08 : str = even_positons(0,"W") + test_08 : str = even_positions(0,"W") print("test_08 is =>",test_08) assert test_08 == "WX?X?X?X?X" From 11d1015d865310f3747baebecb672363304b9b2d Mon Sep 17 00:00:00 2001 From: Assem Date: Mon, 29 Apr 2024 23:38:54 +0300 Subject: [PATCH 26/40] solved minor problem with previous commits --- src/lpython/semantics/python_ast_to_asr.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index f3ab857b4f..125463db37 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5340,10 +5340,6 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); - tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, - list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6206,11 +6202,6 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, - key_type, value_type)); - tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), - values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6675,9 +6666,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); - tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From ee07640ab03c7accc45f14a4619125f4a008a83b Mon Sep 17 00:00:00 2001 From: Assem Date: Tue, 30 Apr 2024 02:07:52 +0300 Subject: [PATCH 27/40] fixed some indentations in def_func_01.py test file --- integration_tests/def_func_01.py | 39 ++++++++++++-------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/integration_tests/def_func_01.py b/integration_tests/def_func_01.py index 5bc16bdeaf..5f6e25b5f0 100644 --- a/integration_tests/def_func_01.py +++ b/integration_tests/def_func_01.py @@ -10,71 +10,60 @@ def factorial_2(x: i32, y:i32=3 ,z:i32 =2) ->i32: return x * y * z return x * factorial_2(x-1) -def default_func(x : str ="Hello" ,y : str = " ", z : str = "World") ->str: +def default_func(x : str ="Hello", y : str = " ", z : str = "World") ->str: return x + y + z -def even_positions(iterator : i32 , to_add : str = "?")-> str: +def even_positions(iterator : i32, to_add : str = "?")-> str: if (iterator == 10): return "" - if iterator%2 == 0 : return to_add + even_positions(iterator+1,"X") - return to_add +even_positions(iterator+1) def test_all(): - test_00 : i32 = factorial_1(1) - print("test_00 is =>",test_00) + print("test_00 is =>", test_00) assert test_00 == 1 test_01 : i32 = factorial_1(5,0) - print("test_01 is =>",test_01) + print("test_01 is =>", test_01) assert test_01 == 120 test_02 : i32 = factorial_1(1,5555) - print("test_02 is =>",test_02) + print("test_02 is =>", test_02) assert test_02 == 5555 test_03 : i32 =factorial_2(5,99999,99999) - print("test_03 is =>",test_03) + print("test_03 is =>", test_03) assert test_03 == 120 test_04 : i32 = factorial_2(4,-1,100) - print("test_04 is =>",test_04) + print("test_04 is =>", test_04) assert test_04 == -400 test_05 :str = default_func() - print("test_05 is =>",test_05) + print("test_05 is =>", test_05) assert test_05 == "Hello World" test_06 :str = default_func(y = "|||",x="Hi") - print("test_06 is =>",test_06) + print("test_06 is =>", test_06) assert test_06 == "Hi|||World" - test_07 : str = even_positions(0) - print("test_07 is =>",test_07) + print("test_07 is =>", test_07) assert test_07 == "?X?X?X?X?X" test_08 : str = even_positions(0,"W") - print("test_08 is =>",test_08) + print("test_08 is =>", test_08) assert test_08 == "WX?X?X?X?X" test_09 :str = default_func(y = "++",z = "LPython") - print("test_09 is =>",test_09) + print("test_09 is =>", test_09) assert test_09 == "Hello++LPython" test_10 :str = default_func("Welcome",z = "LPython") - print("test_10 is =>",test_10) + print("test_10 is =>", test_10) assert test_10 == "Welcome LPython" -test_all() - - - - - - - \ No newline at end of file +test_all() \ No newline at end of file From fe4f1625d103e26018f61a79e5111bab786f2018 Mon Sep 17 00:00:00 2001 From: Assem Medhat Date: Wed, 1 May 2024 00:47:28 +0300 Subject: [PATCH 28/40] Update src/lpython/semantics/python_ast_to_asr.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Čertík --- src/lpython/semantics/python_ast_to_asr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 125463db37..628b5f9421 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -704,7 +704,7 @@ class CommonVisitor : public AST::BaseVisitor { call_args_vec.p[arg_pos].loc = expr->base.loc; call_args_vec.p[arg_pos].m_value = expr; } - //Filling missing arguments with its default argument passed in function definition (if existed). + // Filling missing arguments with their defaults passed in function definition (if present). for(size_t i = 0; i < orig_func->n_args; i++ ){ if(call_args_vec.p[i].m_value == nullptr){ ASR::symbol_t* sym = ASR::down_cast(orig_func->m_args[i])->m_v; From ae0f7a0c1bb89d93404f93303c4283016bfd596c Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 19:31:48 +0200 Subject: [PATCH 29/40] Fixed Issue #2042 --- src/lpython/semantics/python_ast_to_asr.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 628b5f9421..54cfbc8676 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5204,6 +5204,38 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); + //Construction-While-Assigning Problem + AST::exprType value_type_ast = (x.m_value)->type; + AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. + AST::exprType::Tuple,AST::exprType::List}; + for(AST::exprType &exp : objects_to_check){ + if (exp == value_type_ast){ + ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); + try{ + this->visit_expr(*x.m_value); + } + catch (SemanticError &error){ + ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + diag.add(diag::Diagnostic( + "Type mismatch in assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) + }) + ); + throw SemanticAbort(); + } + } + } + + } + + + this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6666,6 +6698,9 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //set the tmp to use it in the error message. + ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From 233edddb499c218e481d7eabef5aa721a8585214 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 30/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 54cfbc8676..b944e23da0 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5206,8 +5206,9 @@ class BodyVisitor : public CommonVisitor { assign_asr_target = ASRUtils::EXPR(tmp); //Construction-While-Assigning Problem AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. - AST::exprType::Tuple,AST::exprType::List}; + AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. + AST::exprType::Dict, + AST::exprType::List }; for(AST::exprType &exp : objects_to_check){ if (exp == value_type_ast){ ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); @@ -5372,6 +5373,10 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); + tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, + list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6218,6 +6223,16 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { + //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) + Vec values; + ASR::ttype_t* value_type = nullptr; + visit_expr(*x.m_values[0]); + ASR::expr_t *value = ASRUtils::EXPR(tmp); + value_type = ASRUtils::expr_type(value); + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } @@ -6234,6 +6249,11 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6698,7 +6718,7 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message. + //set the tmp to use it in the error message.(copied from the end of this function) ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", From b86819a6b96ddfbaef27c62b57a50820ceec49de Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 31/40] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b944e23da0..eda2b6be0f 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6223,16 +6223,6 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { - //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) - Vec values; - ASR::ttype_t* value_type = nullptr; - visit_expr(*x.m_values[0]); - ASR::expr_t *value = ASRUtils::EXPR(tmp); - value_type = ASRUtils::expr_type(value); - ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, - key_type, value_type)); - tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), - values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } From d912b71ecbe6c8b8c6381bd2086995c342347a1b Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 2 May 2024 10:25:59 +0300 Subject: [PATCH 32/40] added semantic error for insufficient arguments + some edits --- integration_tests/def_func_01.py | 29 +++++++----- src/lpython/semantics/python_ast_to_asr.cpp | 52 +++++++++++++++------ 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/integration_tests/def_func_01.py b/integration_tests/def_func_01.py index 5f6e25b5f0..2564cff13e 100644 --- a/integration_tests/def_func_01.py +++ b/integration_tests/def_func_01.py @@ -22,7 +22,7 @@ def even_positions(iterator : i32, to_add : str = "?")-> str: -def test_all(): +def test_factorial_1(): test_00 : i32 = factorial_1(1) print("test_00 is =>", test_00) assert test_00 == 1 @@ -35,6 +35,7 @@ def test_all(): print("test_02 is =>", test_02) assert test_02 == 5555 +def test_factorial_2(): test_03 : i32 =factorial_2(5,99999,99999) print("test_03 is =>", test_03) assert test_03 == 120 @@ -43,6 +44,7 @@ def test_all(): print("test_04 is =>", test_04) assert test_04 == -400 +def test_default_func(): test_05 :str = default_func() print("test_05 is =>", test_05) assert test_05 == "Hello World" @@ -51,19 +53,24 @@ def test_all(): print("test_06 is =>", test_06) assert test_06 == "Hi|||World" - test_07 : str = even_positions(0) + test_07 :str = default_func(y = "++",z = "LPython") print("test_07 is =>", test_07) - assert test_07 == "?X?X?X?X?X" + assert test_07 == "Hello++LPython" - test_08 : str = even_positions(0,"W") - print("test_08 is =>", test_08) - assert test_08 == "WX?X?X?X?X" + test_8 :str = default_func("Welcome",z = "LPython") + print("test_8 is =>", test_8) + assert test_8 == "Welcome LPython" - test_09 :str = default_func(y = "++",z = "LPython") +def test_even_positions(): + test_09 : str = even_positions(0) print("test_09 is =>", test_09) - assert test_09 == "Hello++LPython" + assert test_09 == "?X?X?X?X?X" - test_10 :str = default_func("Welcome",z = "LPython") + test_10 : str = even_positions(0,"W") print("test_10 is =>", test_10) - assert test_10 == "Welcome LPython" -test_all() \ No newline at end of file + assert test_10 == "WX?X?X?X?X" + +test_factorial_1() +test_factorial_2() +test_default_func() +test_even_positions() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index eda2b6be0f..4ea36cc183 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -659,7 +659,7 @@ class CommonVisitor : public AST::BaseVisitor { // Fill the whole call_args_vec with nullptr // This is for error handling later on. - for( size_t i = 0; i < call_args_vec.max; i++ ) { + for( size_t i = 0; i < orig_func->n_args; i++ ) { ASR::call_arg_t call_arg; Location loc; loc.first = loc.last = 1; @@ -707,18 +707,27 @@ class CommonVisitor : public AST::BaseVisitor { // Filling missing arguments with their defaults passed in function definition (if present). for(size_t i = 0; i < orig_func->n_args; i++ ){ if(call_args_vec.p[i].m_value == nullptr){ - ASR::symbol_t* sym = ASR::down_cast(orig_func->m_args[i])->m_v; - std::string variable_name = ASR::down_cast(sym)->m_name; - ASR::Variable_t* var = ASR::down_cast(orig_func->m_symtab->get_symbol(variable_name)); + ASR::Variable_t* var = ASRUtils::EXPR2VAR(orig_func->m_args[i]); if (var->m_symbolic_value == nullptr){ - call_args_vec.reserve(al, n_pos_args + n_kwargs); //deprecate Vec container, so it raise error later. - break; + missed_args_names+="'" + (std::string) var->m_name + "' and "; + missed_args_count++; } else{ call_args_vec.p[i].m_value = var->m_symbolic_value; } } } + if(missed_args_count > 0){ + missed_args_names = missed_args_names.substr(0,missed_args_names.length() - 5); + diag.add(diag::Diagnostic( + "Number of arguments does not match in the function call", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("missing " + std::to_string(missed_args_count) + " required arguments :" + missed_args_names, + {call_loc}) + }) + ); + throw SemanticAbort(); + } return true; } @@ -1158,17 +1167,32 @@ class CommonVisitor : public AST::BaseVisitor { visit_expr_list(pos_args, n_pos_args, kwargs, n_kwargs, args, rt_subs, func, loc); } else if (args.size() < func->n_args) { + std::string missed_args_names =" "; + size_t missed_args_count =0; for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ - ASR::symbol_t* sym = ASR::down_cast(func->m_args[def_arg])->m_v; - std::string variable_name = ASR::down_cast(sym)->m_name; - ASR::Variable_t* var = ASR::down_cast(func->m_symtab->get_symbol(variable_name)); + ASR::Variable_t* var = ASRUtils::EXPR2VAR(func->m_args[def_arg]); if(var->m_symbolic_value == nullptr) { - break; + missed_args_names+= "'" + (std::string)var->m_name + "' and "; + missed_args_count++; + } + else{ + + ASR::call_arg_t call_arg; + call_arg.m_value = var->m_symbolic_value; + call_arg.loc = (var->m_symbolic_value->base).loc; + args.push_back(al,call_arg); } - ASR::call_arg_t call_arg; - call_arg.m_value = var->m_symbolic_value; - call_arg.loc = (var->m_symbolic_value->base).loc; - args.push_back(al,call_arg); + } + if(missed_args_count > 0){ + missed_args_names = missed_args_names.substr(0,missed_args_names.length() - 5); + diag.add(diag::Diagnostic( + "Number of arguments does not match in the function call", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("missing " + std::to_string(missed_args_count) + " required arguments :" + missed_args_names, + {loc}) + }) + ); + throw SemanticAbort(); } } if (ASRUtils::get_FunctionType(func)->m_is_restriction) { From 48602306f5cf9e8d3b92d140d73c506d5522a867 Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 2 May 2024 10:33:43 +0300 Subject: [PATCH 33/40] resolved minor problems --- src/lpython/semantics/python_ast_to_asr.cpp | 47 +-------------------- 1 file changed, 1 insertion(+), 46 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4ea36cc183..5f46886e97 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5228,39 +5228,6 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); - //Construction-While-Assigning Problem - AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. - AST::exprType::Dict, - AST::exprType::List }; - for(AST::exprType &exp : objects_to_check){ - if (exp == value_type_ast){ - ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); - try{ - this->visit_expr(*x.m_value); - } - catch (SemanticError &error){ - ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); - ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); - if (!ASRUtils::check_equal_type(target_type, value_type)){ - std::string ltype = ASRUtils::type_to_str_python(target_type); - std::string rtype = ASRUtils::type_to_str_python(value_type); - diag.add(diag::Diagnostic( - "Type mismatch in assignment, the types must be compatible", - diag::Level::Error, diag::Stage::Semantic, { - diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", - {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) - }) - ); - throw SemanticAbort(); - } - } - } - - } - - - this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -5397,10 +5364,6 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); - tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, - list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6263,11 +6226,6 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, - key_type, value_type)); - tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), - values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6732,10 +6690,7 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message.(copied from the end of this function) - ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); - tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); - throw SemanticError("All Set values must be of the same type for now", + throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } } From 7ca1987cb00c05adb3fa8066e577bd91f7658b3c Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 2 May 2024 10:46:19 +0300 Subject: [PATCH 34/40] minor edit --- src/lpython/semantics/python_ast_to_asr.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 5f46886e97..b6b1c80ac9 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -705,6 +705,8 @@ class CommonVisitor : public AST::BaseVisitor { call_args_vec.p[arg_pos].m_value = expr; } // Filling missing arguments with their defaults passed in function definition (if present). + std::string missed_args_names; + size_t missed_args_count =0; for(size_t i = 0; i < orig_func->n_args; i++ ){ if(call_args_vec.p[i].m_value == nullptr){ ASR::Variable_t* var = ASRUtils::EXPR2VAR(orig_func->m_args[i]); @@ -6690,7 +6692,7 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - throw SemanticError("All Set values must be of the same type for now", + throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } } From 0fee90557ea666a26cb2c30a3618550432bf1db2 Mon Sep 17 00:00:00 2001 From: Assem Medhat Date: Thu, 2 May 2024 22:26:39 +0300 Subject: [PATCH 35/40] Update src/lpython/semantics/python_ast_to_asr.cpp Co-authored-by: Shaikh Ubaid --- src/lpython/semantics/python_ast_to_asr.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b6b1c80ac9..e5744ec7a5 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1174,11 +1174,9 @@ class CommonVisitor : public AST::BaseVisitor { for (size_t def_arg = args.size(); def_arg < func->n_args; def_arg++){ ASR::Variable_t* var = ASRUtils::EXPR2VAR(func->m_args[def_arg]); if(var->m_symbolic_value == nullptr) { - missed_args_names+= "'" + (std::string)var->m_name + "' and "; + missed_args_names+= "'" + std::string(var->m_name) + "' and "; missed_args_count++; - } - else{ - + } else{ ASR::call_arg_t call_arg; call_arg.m_value = var->m_symbolic_value; call_arg.loc = (var->m_symbolic_value->base).loc; From fc1d61cc40aae3a16d60d99e5e32bba78c42d8e5 Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 2 May 2024 23:14:41 +0300 Subject: [PATCH 36/40] added reference tests --- tests/errors/def_func_01.py | 4 ++++ tests/errors/def_func_02.py | 5 ++++ tests/errors/def_func_03.py | 5 ++++ tests/errors/def_func_04.py | 5 ++++ tests/errors/def_func_05.py | 5 ++++ tests/errors/def_func_06.py | 5 ++++ tests/reference/asr-def_func_01-1c7f4cd.json | 13 ++++++++++ .../reference/asr-def_func_01-1c7f4cd.stderr | 5 ++++ tests/reference/asr-def_func_02-8bf7092.json | 13 ++++++++++ .../reference/asr-def_func_02-8bf7092.stderr | 5 ++++ tests/reference/asr-def_func_03-58ad7c5.json | 13 ++++++++++ .../reference/asr-def_func_03-58ad7c5.stderr | 5 ++++ tests/reference/asr-def_func_04-4af8c0d.json | 13 ++++++++++ .../reference/asr-def_func_04-4af8c0d.stderr | 5 ++++ tests/reference/asr-def_func_05-6c33b29.json | 13 ++++++++++ .../reference/asr-def_func_05-6c33b29.stderr | 5 ++++ tests/reference/asr-def_func_06-9a3ebfc.json | 13 ++++++++++ .../reference/asr-def_func_06-9a3ebfc.stderr | 5 ++++ tests/tests.toml | 24 +++++++++++++++++++ 19 files changed, 161 insertions(+) create mode 100644 tests/errors/def_func_01.py create mode 100644 tests/errors/def_func_02.py create mode 100644 tests/errors/def_func_03.py create mode 100644 tests/errors/def_func_04.py create mode 100644 tests/errors/def_func_05.py create mode 100644 tests/errors/def_func_06.py create mode 100644 tests/reference/asr-def_func_01-1c7f4cd.json create mode 100644 tests/reference/asr-def_func_01-1c7f4cd.stderr create mode 100644 tests/reference/asr-def_func_02-8bf7092.json create mode 100644 tests/reference/asr-def_func_02-8bf7092.stderr create mode 100644 tests/reference/asr-def_func_03-58ad7c5.json create mode 100644 tests/reference/asr-def_func_03-58ad7c5.stderr create mode 100644 tests/reference/asr-def_func_04-4af8c0d.json create mode 100644 tests/reference/asr-def_func_04-4af8c0d.stderr create mode 100644 tests/reference/asr-def_func_05-6c33b29.json create mode 100644 tests/reference/asr-def_func_05-6c33b29.stderr create mode 100644 tests/reference/asr-def_func_06-9a3ebfc.json create mode 100644 tests/reference/asr-def_func_06-9a3ebfc.stderr diff --git a/tests/errors/def_func_01.py b/tests/errors/def_func_01.py new file mode 100644 index 0000000000..5b1f2894c1 --- /dev/null +++ b/tests/errors/def_func_01.py @@ -0,0 +1,4 @@ +def func_01(x : str) -> str: + print(x) + +func_01() \ No newline at end of file diff --git a/tests/errors/def_func_02.py b/tests/errors/def_func_02.py new file mode 100644 index 0000000000..c94e00a5a5 --- /dev/null +++ b/tests/errors/def_func_02.py @@ -0,0 +1,5 @@ +from lpython import i32 +def func_02(x : i32 ,y : i32) -> i32 : + print(x,y) + +func_02() \ No newline at end of file diff --git a/tests/errors/def_func_03.py b/tests/errors/def_func_03.py new file mode 100644 index 0000000000..da885e3c45 --- /dev/null +++ b/tests/errors/def_func_03.py @@ -0,0 +1,5 @@ +from lpython import i32 +def func_03(x : i32 ,y : i32) -> i32 : + print(x,y) + +func_03(1) \ No newline at end of file diff --git a/tests/errors/def_func_04.py b/tests/errors/def_func_04.py new file mode 100644 index 0000000000..f7f9f81d47 --- /dev/null +++ b/tests/errors/def_func_04.py @@ -0,0 +1,5 @@ +from lpython import i32 +def func_04(x : i32 ,y : i32) -> i32 : + print(x,y) + +func_04(y=3) \ No newline at end of file diff --git a/tests/errors/def_func_05.py b/tests/errors/def_func_05.py new file mode 100644 index 0000000000..3fb86d7d7d --- /dev/null +++ b/tests/errors/def_func_05.py @@ -0,0 +1,5 @@ +from lpython import i32 +def func_05(x : i32 ,y : i32,z : i32) -> i32 : + print(x,y,z) + +func_05(1,2) \ No newline at end of file diff --git a/tests/errors/def_func_06.py b/tests/errors/def_func_06.py new file mode 100644 index 0000000000..babf3eb065 --- /dev/null +++ b/tests/errors/def_func_06.py @@ -0,0 +1,5 @@ +from lpython import i32 +def func_05(x : i32 ,y : i32,z : i32) -> i32 : + print(x,y,z) + +func_05(z=3) \ No newline at end of file diff --git a/tests/reference/asr-def_func_01-1c7f4cd.json b/tests/reference/asr-def_func_01-1c7f4cd.json new file mode 100644 index 0000000000..e3743a1c95 --- /dev/null +++ b/tests/reference/asr-def_func_01-1c7f4cd.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-def_func_01-1c7f4cd", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/def_func_01.py", + "infile_hash": "61daf4e2823865c57c501d99dceee1553856e2e142e8d6276fcf5a35", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-def_func_01-1c7f4cd.stderr", + "stderr_hash": "0914547db8dc99e767e8817786e1c94c1bc72c695bd5ec6d810a9cdb", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-def_func_01-1c7f4cd.stderr b/tests/reference/asr-def_func_01-1c7f4cd.stderr new file mode 100644 index 0000000000..8a375fba52 --- /dev/null +++ b/tests/reference/asr-def_func_01-1c7f4cd.stderr @@ -0,0 +1,5 @@ +semantic error: Number of arguments does not match in the function call + --> tests/errors/def_func_01.py:4:1 + | +4 | func_01() + | ^^^^^^^^^ missing 1 required arguments : 'x' diff --git a/tests/reference/asr-def_func_02-8bf7092.json b/tests/reference/asr-def_func_02-8bf7092.json new file mode 100644 index 0000000000..dd639549f4 --- /dev/null +++ b/tests/reference/asr-def_func_02-8bf7092.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-def_func_02-8bf7092", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/def_func_02.py", + "infile_hash": "fe3a3789ece86f790691ead17887dfebb8d60b882f58e06d333c9bb2", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-def_func_02-8bf7092.stderr", + "stderr_hash": "61aea2e70bfee634a40291abc98afa838c6ca173201d9d16f9dfb428", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-def_func_02-8bf7092.stderr b/tests/reference/asr-def_func_02-8bf7092.stderr new file mode 100644 index 0000000000..7c4bcd5d23 --- /dev/null +++ b/tests/reference/asr-def_func_02-8bf7092.stderr @@ -0,0 +1,5 @@ +semantic error: Number of arguments does not match in the function call + --> tests/errors/def_func_02.py:5:1 + | +5 | func_02() + | ^^^^^^^^^ missing 2 required arguments : 'x' and 'y' diff --git a/tests/reference/asr-def_func_03-58ad7c5.json b/tests/reference/asr-def_func_03-58ad7c5.json new file mode 100644 index 0000000000..d702aeffdf --- /dev/null +++ b/tests/reference/asr-def_func_03-58ad7c5.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-def_func_03-58ad7c5", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/def_func_03.py", + "infile_hash": "e69f130e474a8757368e7ad3e9afcd3553eaff1e819173febb66fd06", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-def_func_03-58ad7c5.stderr", + "stderr_hash": "5ac45e87ffbe795b9ca06dc4a82d3743c762f4f0a1f6bbfdc3e14ca2", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-def_func_03-58ad7c5.stderr b/tests/reference/asr-def_func_03-58ad7c5.stderr new file mode 100644 index 0000000000..3c357d9a50 --- /dev/null +++ b/tests/reference/asr-def_func_03-58ad7c5.stderr @@ -0,0 +1,5 @@ +semantic error: Number of arguments does not match in the function call + --> tests/errors/def_func_03.py:5:1 + | +5 | func_03(1) + | ^^^^^^^^^^ missing 1 required arguments : 'y' diff --git a/tests/reference/asr-def_func_04-4af8c0d.json b/tests/reference/asr-def_func_04-4af8c0d.json new file mode 100644 index 0000000000..51c9bf2948 --- /dev/null +++ b/tests/reference/asr-def_func_04-4af8c0d.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-def_func_04-4af8c0d", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/def_func_04.py", + "infile_hash": "522166d0c6c1a0cb273d67ce577ec42330f02822c75b1b317c97608c", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-def_func_04-4af8c0d.stderr", + "stderr_hash": "11bd3ae2f41227fd383927fa2f9fc4feff50c19784df51b56f50d3e9", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-def_func_04-4af8c0d.stderr b/tests/reference/asr-def_func_04-4af8c0d.stderr new file mode 100644 index 0000000000..88195b0527 --- /dev/null +++ b/tests/reference/asr-def_func_04-4af8c0d.stderr @@ -0,0 +1,5 @@ +semantic error: Number of arguments does not match in the function call + --> tests/errors/def_func_04.py:5:1 + | +5 | func_04(y=3) + | ^^^^^^^^^^^^ missing 1 required arguments :'x' diff --git a/tests/reference/asr-def_func_05-6c33b29.json b/tests/reference/asr-def_func_05-6c33b29.json new file mode 100644 index 0000000000..68c9f7192a --- /dev/null +++ b/tests/reference/asr-def_func_05-6c33b29.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-def_func_05-6c33b29", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/def_func_05.py", + "infile_hash": "bc8d5377ec564a4d5758653dea39d5c6237992a54ec33fdef88f63f2", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-def_func_05-6c33b29.stderr", + "stderr_hash": "9dad35128e5da8dcc73f9c96bdb43ce15e3309d590bb794b14e3133c", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-def_func_05-6c33b29.stderr b/tests/reference/asr-def_func_05-6c33b29.stderr new file mode 100644 index 0000000000..4af8d9f66c --- /dev/null +++ b/tests/reference/asr-def_func_05-6c33b29.stderr @@ -0,0 +1,5 @@ +semantic error: Number of arguments does not match in the function call + --> tests/errors/def_func_05.py:5:1 + | +5 | func_05(1,2) + | ^^^^^^^^^^^^ missing 1 required arguments : 'z' diff --git a/tests/reference/asr-def_func_06-9a3ebfc.json b/tests/reference/asr-def_func_06-9a3ebfc.json new file mode 100644 index 0000000000..77f6bfe11b --- /dev/null +++ b/tests/reference/asr-def_func_06-9a3ebfc.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-def_func_06-9a3ebfc", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/def_func_06.py", + "infile_hash": "5ad73c3f18ab4d9fe82108e65e0013687a70acc3eff495a402d4297e", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-def_func_06-9a3ebfc.stderr", + "stderr_hash": "f9c79e62d7ef7f411870195bfeb99615cb7da9216af328fda2fb0cd2", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-def_func_06-9a3ebfc.stderr b/tests/reference/asr-def_func_06-9a3ebfc.stderr new file mode 100644 index 0000000000..65527b826a --- /dev/null +++ b/tests/reference/asr-def_func_06-9a3ebfc.stderr @@ -0,0 +1,5 @@ +semantic error: Number of arguments does not match in the function call + --> tests/errors/def_func_06.py:5:1 + | +5 | func_05(z=3) + | ^^^^^^^^^^^^ missing 2 required arguments :'x' and 'y' diff --git a/tests/tests.toml b/tests/tests.toml index b106996999..7e9b1d43d0 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -1442,4 +1442,28 @@ run_with_dbg = true [[test]] filename = "errors/test_optional.py" +asr = true + +[[test]] +filename = "errors/def_func_01.py" +asr = true + +[[test]] +filename = "errors/def_func_02.py" +asr = true + +[[test]] +filename = "errors/def_func_03.py" +asr = true + +[[test]] +filename = "errors/def_func_04.py" +asr = true + +[[test]] +filename = "errors/def_func_05.py" +asr = true + +[[test]] +filename = "errors/def_func_06.py" asr = true \ No newline at end of file From 262041abf4c0ebeb7299b86668432ae7a572d2c1 Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 2 May 2024 23:20:13 +0300 Subject: [PATCH 37/40] minor edit --- tests/errors/def_func_01.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/errors/def_func_01.py b/tests/errors/def_func_01.py index 5b1f2894c1..4611f33a96 100644 --- a/tests/errors/def_func_01.py +++ b/tests/errors/def_func_01.py @@ -1,3 +1,4 @@ +from lpython import i32 def func_01(x : str) -> str: print(x) From b8a14b11eb3c31ad24da1c42911e989b3f6dcc4f Mon Sep 17 00:00:00 2001 From: Assem Date: Thu, 2 May 2024 23:26:36 +0300 Subject: [PATCH 38/40] minor edit --- tests/reference/asr-def_func_01-1c7f4cd.json | 4 ++-- tests/reference/asr-def_func_01-1c7f4cd.stderr | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/reference/asr-def_func_01-1c7f4cd.json b/tests/reference/asr-def_func_01-1c7f4cd.json index e3743a1c95..4eff9fff31 100644 --- a/tests/reference/asr-def_func_01-1c7f4cd.json +++ b/tests/reference/asr-def_func_01-1c7f4cd.json @@ -2,12 +2,12 @@ "basename": "asr-def_func_01-1c7f4cd", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/errors/def_func_01.py", - "infile_hash": "61daf4e2823865c57c501d99dceee1553856e2e142e8d6276fcf5a35", + "infile_hash": "fda645ec7920824250cc2b5c28663061fe629b1dc341fc70ba3a691c", "outfile": null, "outfile_hash": null, "stdout": null, "stdout_hash": null, "stderr": "asr-def_func_01-1c7f4cd.stderr", - "stderr_hash": "0914547db8dc99e767e8817786e1c94c1bc72c695bd5ec6d810a9cdb", + "stderr_hash": "e96fc67b26c68ef0954595fb87bf261a1bfe6e9f55d83baf28e73032", "returncode": 2 } \ No newline at end of file diff --git a/tests/reference/asr-def_func_01-1c7f4cd.stderr b/tests/reference/asr-def_func_01-1c7f4cd.stderr index 8a375fba52..ac8d574cb7 100644 --- a/tests/reference/asr-def_func_01-1c7f4cd.stderr +++ b/tests/reference/asr-def_func_01-1c7f4cd.stderr @@ -1,5 +1,5 @@ semantic error: Number of arguments does not match in the function call - --> tests/errors/def_func_01.py:4:1 + --> tests/errors/def_func_01.py:5:1 | -4 | func_01() +5 | func_01() | ^^^^^^^^^ missing 1 required arguments : 'x' From a4747341928ae292fc5eaaec9f17ce18777a5c93 Mon Sep 17 00:00:00 2001 From: Assem Medhat Date: Fri, 3 May 2024 22:28:45 +0300 Subject: [PATCH 39/40] Update integration_tests/CMakeLists.txt Co-authored-by: Saurabh Kumar --- integration_tests/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index ac0fbabb9d..85e47e5997 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -792,7 +792,6 @@ RUN(NAME test_str_attributes LABELS cpython llvm llvm_jit c) RUN(NAME kwargs_01 LABELS cpython llvm llvm_jit c NOFAST) RUN(NAME def_func_01 LABELS cpython llvm c) - RUN(NAME func_inline_01 LABELS llvm llvm_jit c wasm) RUN(NAME func_inline_02 LABELS cpython llvm llvm_jit c) RUN(NAME func_static_01 LABELS cpython llvm llvm_jit c wasm) From 2549f0bd1fa54623107154fc444bd8657f58ec4e Mon Sep 17 00:00:00 2001 From: Assem Date: Fri, 3 May 2024 22:31:30 +0300 Subject: [PATCH 40/40] typo edits --- integration_tests/CMakeLists.txt | 2 +- src/lpython/semantics/python_ast_to_asr.cpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 85e47e5997..517b40dd9a 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -790,7 +790,7 @@ RUN(NAME test_statistics_01 LABELS cpython llvm llvm_jit NOFAST) RUN(NAME test_statistics_02 LABELS cpython llvm llvm_jit NOFAST REQ_PY_VER 3.10) RUN(NAME test_str_attributes LABELS cpython llvm llvm_jit c) RUN(NAME kwargs_01 LABELS cpython llvm llvm_jit c NOFAST) -RUN(NAME def_func_01 LABELS cpython llvm c) +RUN(NAME def_func_01 LABELS cpython llvm llvm_jit c) RUN(NAME func_inline_01 LABELS llvm llvm_jit c wasm) RUN(NAME func_inline_02 LABELS cpython llvm llvm_jit c) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index e5744ec7a5..9a06a8453b 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -713,8 +713,7 @@ class CommonVisitor : public AST::BaseVisitor { if (var->m_symbolic_value == nullptr){ missed_args_names+="'" + (std::string) var->m_name + "' and "; missed_args_count++; - } - else{ + } else { call_args_vec.p[i].m_value = var->m_symbolic_value; } } @@ -1176,7 +1175,7 @@ class CommonVisitor : public AST::BaseVisitor { if(var->m_symbolic_value == nullptr) { missed_args_names+= "'" + std::string(var->m_name) + "' and "; missed_args_count++; - } else{ + } else { ASR::call_arg_t call_arg; call_arg.m_value = var->m_symbolic_value; call_arg.loc = (var->m_symbolic_value->base).loc;