From fc999f3350faeab64ea77e23287208cbc8d4e8c1 Mon Sep 17 00:00:00 2001 From: swamishiju Date: Wed, 19 Mar 2025 08:47:08 +0530 Subject: [PATCH 1/3] Fix: Unused imports no longer give unhandled exception --- src/lpython/semantics/python_ast_to_asr.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index fac917eaf4..058266ea97 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5114,6 +5114,14 @@ class BodyVisitor : public CommonVisitor { tmp = nullptr; tmp_vec.clear(); visit_stmt(*x.m_body[i]); + + if (x.m_body[i]->type == LCompilers::LPython::AST::stmtType::Import){ + AST::Import_t import = (const AST::Import_t &) *x.m_body[i]; + for (size_t j=0;j Date: Wed, 19 Mar 2025 09:12:12 +0530 Subject: [PATCH 2/3] Test: Added tests for unused imports --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_import_08.py | 5 +++++ integration_tests/test_import_08_module.py | 6 ++++++ 3 files changed, 12 insertions(+) create mode 100644 integration_tests/test_import_08.py create mode 100644 integration_tests/test_import_08_module.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 2c360fd51d..d320517100 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -606,6 +606,7 @@ RUN(NAME test_import_04 LABELS cpython llvm llvm_jit c) RUN(NAME test_import_05 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64) RUN(NAME test_import_06 LABELS cpython llvm llvm_jit) RUN(NAME test_import_07 LABELS cpython llvm llvm_jit c) +RUN(NAME test_import_08 LABELS cpython llvm) RUN(NAME test_math LABELS cpython llvm llvm_jit NOFAST) RUN(NAME test_membership_01 LABELS cpython llvm) RUN(NAME test_numpy_01 LABELS cpython llvm llvm_jit c) diff --git a/integration_tests/test_import_08.py b/integration_tests/test_import_08.py new file mode 100644 index 0000000000..5f2fadca16 --- /dev/null +++ b/integration_tests/test_import_08.py @@ -0,0 +1,5 @@ +import string + +import test_import_08_module + +# Supporting unused imports that "have expr own variables" diff --git a/integration_tests/test_import_08_module.py b/integration_tests/test_import_08_module.py new file mode 100644 index 0000000000..f92430e846 --- /dev/null +++ b/integration_tests/test_import_08_module.py @@ -0,0 +1,6 @@ +from lpython import i32 + +a: i32 = 10 +b: i32 = a + 10 + +print("Inside import") From 9ef42086f88fd52aae96fb58639cea53e4b5fdee Mon Sep 17 00:00:00 2001 From: swamishiju Date: Wed, 19 Mar 2025 11:13:10 +0530 Subject: [PATCH 3/3] Previous commit bug fix moved dependency addition to body visitor --- integration_tests/test_import_08.py | 2 -- src/lpython/semantics/python_ast_to_asr.cpp | 17 ++++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/integration_tests/test_import_08.py b/integration_tests/test_import_08.py index 5f2fadca16..80b1bffa0f 100644 --- a/integration_tests/test_import_08.py +++ b/integration_tests/test_import_08.py @@ -1,5 +1,3 @@ import string import test_import_08_module - -# Supporting unused imports that "have expr own variables" diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 058266ea97..a44694d95d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4910,6 +4910,16 @@ class SymbolTableVisitor : public CommonVisitor { throw SemanticError("The module '" + mod_sym + "' cannot be loaded", x.base.base.loc); } + if( mod_sym == "__init__" ) { + for( auto item: ASRUtils::symbol_symtab(t)->get_scope() ) { + if( ASR::is_a(*item.second) ) { + current_module_dependencies.push_back(al, + ASR::down_cast(item.second)->m_module_name); + } + } + } else { + current_module_dependencies.push_back(al, s2c(al, mod_sym)); + } } } @@ -5115,13 +5125,6 @@ class BodyVisitor : public CommonVisitor { tmp_vec.clear(); visit_stmt(*x.m_body[i]); - if (x.m_body[i]->type == LCompilers::LPython::AST::stmtType::Import){ - AST::Import_t import = (const AST::Import_t &) *x.m_body[i]; - for (size_t j=0;j