From cdd0248633c881c7126e948c51eea279dc1ee88a Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sun, 30 Oct 2022 10:08:01 +0530 Subject: [PATCH 1/4] Fix variable access from module use dot --- src/lpython/semantics/python_ast_to_asr.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index a93962430e..55d99e31ec 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4391,6 +4391,13 @@ class BodyVisitor : public CommonVisitor { tmp = ASR::make_EnumValue_t(al, x.base.base.loc, enum_member_var, enum_t, enum_member_variable->m_type, ASRUtils::expr_value(enum_member_variable->m_symbolic_value)); + } else if (ASR::is_a(*t)) { + ASR::Module_t *m = ASR::down_cast(t); + ASR::symbol_t *sym = import_from_module(al, m, current_scope, value, + x.m_attr, x.m_attr, x.base.base.loc); + LFORTRAN_ASSERT(ASR::is_a(*sym)); + current_scope->add_symbol(x.m_attr, sym); + tmp = ASR::make_Var_t(al, x.base.base.loc, sym); } else { throw SemanticError("Only Variable type is supported for now in Attribute", x.base.base.loc); From c375d4fd40f6e25d948331171a21a1d2d6fffbe1 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Sun, 30 Oct 2022 10:10:57 +0530 Subject: [PATCH 2/4] Add tests --- integration_tests/test_math.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index c656872f22..65da427de7 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,6 +1,7 @@ from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow, ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc, modf, fsum, prod) +import math from ltypes import i32, i64, f32, f64 eps: f64 @@ -234,6 +235,11 @@ def test_modf(): assert abs(res[1] + 442.0) <= 1e-6 +def test_issue_1242(): + assert abs(math.pi - 3.14159265358979323846) < 1e-10 + assert abs(math.e - 2.7182818284590452353) < 1e-10 + + def check(): test_factorial_1() test_comb() @@ -257,6 +263,7 @@ def check(): test_fsum() test_prod() test_modf() + test_issue_1242() check() From 99e8ee55c99612debd6183174258d82db98af026 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 31 Oct 2022 10:09:36 +0530 Subject: [PATCH 3/4] Fix to allow symbol with same name present in other module and used by dot --- src/lpython/semantics/python_ast_to_asr.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 55d99e31ec..17bd8d65a0 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -395,14 +395,15 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab, ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope, std::string mname, std::string cur_sym_name, std::string new_sym_name, - const Location &loc) { + const Location &loc, bool skip_current_scope_check=false) { ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name); if (!t) { throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'", loc); } - if (current_scope->get_scope().find(cur_sym_name) != current_scope->get_scope().end()) { - throw SemanticError(cur_sym_name + " already defined", loc); + if (!skip_current_scope_check && + current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) { + throw SemanticError(new_sym_name + " already defined", loc); } if (ASR::is_a(*t)) { ASR::Function_t *mfn = ASR::down_cast(t); @@ -4393,10 +4394,14 @@ class BodyVisitor : public CommonVisitor { ASRUtils::expr_value(enum_member_variable->m_symbolic_value)); } else if (ASR::is_a(*t)) { ASR::Module_t *m = ASR::down_cast(t); - ASR::symbol_t *sym = import_from_module(al, m, current_scope, value, - x.m_attr, x.m_attr, x.base.base.loc); - LFORTRAN_ASSERT(ASR::is_a(*sym)); - current_scope->add_symbol(x.m_attr, sym); + std::string sym_name = value + "@" + x.m_attr; + ASR::symbol_t *sym = current_scope->resolve_symbol(sym_name); + if (!sym) { + sym = import_from_module(al, m, current_scope, value, + x.m_attr, sym_name, x.base.base.loc, true); + LFORTRAN_ASSERT(ASR::is_a(*sym)); + current_scope->add_symbol(sym_name, sym); + } tmp = ASR::make_Var_t(al, x.base.base.loc, sym); } else { throw SemanticError("Only Variable type is supported for now in Attribute", From e4bd9b5716a8a374a4e1ab0b55802010d797b7a1 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Mon, 31 Oct 2022 10:09:42 +0530 Subject: [PATCH 4/4] Add a test of fixed issue with same variable name --- integration_tests/test_math.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 65da427de7..40b68b1011 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -239,6 +239,12 @@ def test_issue_1242(): assert abs(math.pi - 3.14159265358979323846) < 1e-10 assert abs(math.e - 2.7182818284590452353) < 1e-10 + # https://github.com/lcompilers/lpython/pull/1243#discussion_r1008810444 + pi: f64 = 8.4603959020429502 + assert abs(pi - 8.4603959020429502) < 1e-10 + assert abs(math.pi - 3.14159265358979323846) < 1e-10 + assert abs(math.pi - 3.14159265358979323846) < 1e-10 + def check(): test_factorial_1()