Skip to content

Commit

Permalink
Merge pull request lcompilers#1755 from Smit-create/i-1681
Browse files Browse the repository at this point in the history
Fix empty list assignment
  • Loading branch information
Smit-create committed May 3, 2023
2 parents 7b7336a + 39bcc1d commit 5f99a2b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
12 changes: 12 additions & 0 deletions integration_tests/test_list_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,21 @@ def test_list_03():
for i in range(size):
assert x[i] == x[((i-len(x)) + size) % size]


def test_issue_1681():
a: list[i32] = [2, 3, 4]
a = [1, 2, 3]
assert len(a) == 3 and a[0] == 1 and a[1] == 2 and a[2] == 3
a = []
assert len(a) == 0
a = [1]
assert len(a) == 1 and a[0] == 1


def tests():
test_list_01()
test_list_02()
test_list_03()
test_issue_1681()

tests()
34 changes: 24 additions & 10 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4413,6 +4413,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
);
throw SemanticAbort();
}
if (tmp_value == nullptr && AST::is_a<AST::List_t>(*x.m_value)) {
LCOMPILERS_ASSERT(AST::down_cast<AST::List_t>(x.m_value)->n_elts == 0);
Vec<ASR::expr_t*> list_ele;
list_ele.reserve(al, 1);
tmp_value = ASRUtils::EXPR(ASR::make_ListConstant_t(al, x.base.base.loc, list_ele.p,
list_ele.size(), value_type));
}
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(tmp_value), value_type)) {
std::string vtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(tmp_value));
std::string totype = ASRUtils::type_to_str_python(value_type);
Expand Down Expand Up @@ -4451,11 +4458,18 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}
}
}
if (!tmp_value) continue;
this->visit_expr(*x.m_targets[i]);
target = ASRUtils::EXPR(tmp);
ASR::ttype_t *target_type = ASRUtils::expr_type(target);
ASR::ttype_t *value_type = ASRUtils::expr_type(assign_value);
if (tmp_value == nullptr && AST::is_a<AST::List_t>(*x.m_value)) {
LCOMPILERS_ASSERT(AST::down_cast<AST::List_t>(x.m_value)->n_elts == 0);
Vec<ASR::expr_t*> list_ele;
list_ele.reserve(al, 1);
tmp_value = ASRUtils::EXPR(ASR::make_ListConstant_t(al, x.base.base.loc, list_ele.p,
list_ele.size(), target_type));
}
if (!tmp_value) continue;
ASR::ttype_t *value_type = ASRUtils::expr_type(tmp_value);
if( ASR::is_a<ASR::Pointer_t>(*target_type) &&
ASR::is_a<ASR::Var_t>(*target) ) {
if( !ASRUtils::check_equal_type(target_type, value_type) ) {
Expand Down Expand Up @@ -6082,7 +6096,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {

} else if(attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
/*
String Validation Methods i.e all "is" based functions are handled here
String Validation Methods i.e all "is" based functions are handled here
*/
std::vector<std::string> validation_methods{"lower", "upper", "decimal", "ascii"}; // Database of validation methods supported
std::string method_name = attr_name.substr(2);
Expand Down Expand Up @@ -6454,9 +6468,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
return;
} else if (attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
/*
* Specification -
* Specification -
Return True if all cased characters [lowercase, uppercase, titlecase] in the string
Return True if all cased characters [lowercase, uppercase, titlecase] in the string
are lowercase and there is at least one cased character, False otherwise.
* islower() method is limited to English Alphabets currently
Expand All @@ -6473,7 +6487,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {

if(attr_name == "islower") {
/*
* Specification:
* Specification:
Return True if all cased characters in the string are lowercase and there is at least one cased character, False otherwise.
*/
bool is_cased_present = false;
Expand All @@ -6493,7 +6507,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
return;
} else if(attr_name == "isupper") {
/*
* Specification:
* Specification:
Return True if all cased characters in the string are uppercase and there is at least one cased character, False otherwise.
*/
bool is_cased_present = false;
Expand All @@ -6513,7 +6527,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
return;
} else if(attr_name == "isdecimal") {
/*
* Specification:
* Specification:
Return True if all characters in the string are decimal characters and there is at least one character, False otherwise.
*/
bool is_decimal = (s_var.size() != 0);
Expand All @@ -6528,8 +6542,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
return;
} else if(attr_name == "isascii") {
/*
* Specification -
Return True if the string is empty or all characters in the string are ASCII, False otherwise.
* Specification -
Return True if the string is empty or all characters in the string are ASCII, False otherwise.
ASCII characters have code points in the range U+0000-U+007F.
*/
bool is_ascii = true;
Expand Down

0 comments on commit 5f99a2b

Please sign in to comment.