Skip to content

Commit 768e809

Browse files
authored
Merge pull request #2049 from Shaikh-Ubaid/fix_imports
Fix imports when same symbol is imported in different files
2 parents 934e794 + 41210e2 commit 768e809

File tree

76 files changed

+1687
-1677
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1687
-1677
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ RUN(NAME test_import_03 LABELS cpython llvm c)
484484
RUN(NAME test_import_04 IMPORT_PATH ..
485485
LABELS cpython llvm c)
486486
RUN(NAME test_import_05 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
487+
RUN(NAME test_import_06 LABELS cpython llvm)
487488
RUN(NAME test_math LABELS cpython llvm NOFAST)
488489
RUN(NAME test_numpy_01 LABELS cpython llvm c)
489490
RUN(NAME test_numpy_02 LABELS cpython llvm c)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from test_import_06_mod1 import StringIO
2+
from test_import_06_mod2 import stringio_test
3+
4+
if __name__ == '__main__':
5+
integer_asr : str = '(Integer 4 [])'
6+
fd : StringIO = StringIO(integer_asr)
7+
stringio_test(fd, integer_asr)
8+
print("Ok")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from lpython import dataclass
2+
3+
@dataclass
4+
class StringIO:
5+
a: str
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from test_import_06_mod1 import StringIO
2+
3+
def stringio_test(fd: StringIO, integer_asr: str):
4+
assert fd.a == integer_asr

src/bin/lpython.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ int emit_asr(const std::string &infile,
226226

227227
diagnostics.diagnostics.clear();
228228
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
229-
r = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics,
230-
compiler_options, true, infile);
229+
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics,
230+
compiler_options, true, "", infile);
231231
std::cerr << diagnostics.render(lm, compiler_options);
232232
if (!r.ok) {
233233
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -281,7 +281,7 @@ int emit_cpp(const std::string &infile,
281281

282282
diagnostics.diagnostics.clear();
283283
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
284-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
284+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
285285
std::cerr << diagnostics.render(lm, compiler_options);
286286
if (!r1.ok) {
287287
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -326,7 +326,7 @@ int emit_c(const std::string &infile,
326326

327327
diagnostics.diagnostics.clear();
328328
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
329-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
329+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
330330
std::cerr << diagnostics.render(lm, compiler_options);
331331
if (!r1.ok) {
332332
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -379,7 +379,7 @@ int emit_c_to_file(const std::string &infile, const std::string &outfile,
379379

380380
diagnostics.diagnostics.clear();
381381
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
382-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
382+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
383383
std::cerr << diagnostics.render(lm, compiler_options);
384384
if (!r1.ok) {
385385
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -435,7 +435,7 @@ int emit_wat(const std::string &infile,
435435

436436
diagnostics.diagnostics.clear();
437437
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
438-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
438+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
439439
std::cerr << diagnostics.render(lm, compiler_options);
440440
if (!r1.ok) {
441441
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -484,7 +484,7 @@ int get_symbols (const std::string &infile,
484484
if (r1.ok) {
485485
LCompilers::LPython::AST::ast_t* ast = r1.result;
486486
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
487-
x = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
487+
x = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
488488
if (!x.ok) {
489489
std::cout << "{}\n";
490490
return 0;
@@ -585,7 +585,7 @@ int get_errors (const std::string &infile,
585585
if (r1.ok) {
586586
LCompilers::LPython::AST::ast_t* ast = r1.result;
587587
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
588-
r = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
588+
r = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
589589
}
590590
std::vector<LCompilers::error_highlight> diag_lists;
591591
LCompilers::error_highlight h;
@@ -705,7 +705,7 @@ int emit_llvm(const std::string &infile,
705705
LCompilers::LPython::AST::ast_t* ast = r.result;
706706
diagnostics.diagnostics.clear();
707707
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
708-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
708+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
709709
std::cerr << diagnostics.render(lm, compiler_options);
710710
if (!r1.ok) {
711711
LCOMPILERS_ASSERT(diagnostics.has_error())
@@ -780,8 +780,8 @@ int compile_python_to_object_file(
780780
diagnostics.diagnostics.clear();
781781
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
782782
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
783-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options,
784-
!(arg_c && compiler_options.disable_main), infile);
783+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options,
784+
!(arg_c && compiler_options.disable_main), "", infile);
785785

786786
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
787787
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
@@ -898,7 +898,7 @@ int compile_to_binary_wasm(
898898
diagnostics.diagnostics.clear();
899899
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
900900
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
901-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
901+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
902902
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
903903
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
904904
std::cerr << diagnostics.render(lm, compiler_options);
@@ -971,7 +971,7 @@ int compile_to_binary_x86(
971971
diagnostics.diagnostics.clear();
972972
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
973973
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
974-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
974+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
975975
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
976976
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
977977
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1045,7 +1045,7 @@ int compile_to_binary_wasm_to_x86(
10451045
diagnostics.diagnostics.clear();
10461046
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
10471047
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1048-
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
1048+
r1 = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *ast, diagnostics, compiler_options, true, "", infile);
10491049
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
10501050
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
10511051
std::cerr << diagnostics.render(lm, compiler_options);
@@ -1341,7 +1341,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
13411341
if (ast.ok) {
13421342
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13431343
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1344-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, *casted_ast, diagnostics, compiler_options, true, "input");
1344+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
13451345
out = diagnostics.render(lm, compiler_options);
13461346
if (asr.ok) {
13471347
out += LCompilers::LPython::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
@@ -1359,7 +1359,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
13591359
if (ast.ok) {
13601360
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13611361
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1362-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, *casted_ast, diagnostics, compiler_options, true, "input");
1362+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
13631363
out = diagnostics.render(lm, compiler_options);
13641364
if (asr.ok) {
13651365
LCompilers::Result<LCompilers::Vec<uint8_t>>
@@ -1386,7 +1386,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
13861386
if (ast.ok) {
13871387
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
13881388
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1389-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, *casted_ast, diagnostics, compiler_options, true, "input");
1389+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
13901390
out = diagnostics.render(lm, compiler_options);
13911391
if (asr.ok) {
13921392
auto res = LCompilers::asr_to_cpp(al, *asr.result, diagnostics,
@@ -1424,7 +1424,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
14241424
if (ast.ok) {
14251425
auto casted_ast = (LCompilers::LPython::AST::ast_t*)ast.result;
14261426
LCompilers::Result<LCompilers::ASR::TranslationUnit_t*>
1427-
asr = LCompilers::LPython::python_ast_to_asr(al, lm, *casted_ast, diagnostics, compiler_options, true, "input");
1427+
asr = LCompilers::LPython::python_ast_to_asr(al, lm, nullptr, *casted_ast, diagnostics, compiler_options, true, "", "input");
14281428
out = diagnostics.render(lm, compiler_options);
14291429
if (asr.ok) {
14301430
LCompilers::Result<LCompilers::Vec<uint8_t>>

src/libasr/asr_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ static inline bool all_args_evaluated(const Vec<ASR::expr_t*> &args) {
912912
static inline std::string get_mangled_name(ASR::Module_t* module, std::string symbol_name) {
913913
std::string module_name = module->m_name;
914914
if( module_name == symbol_name ) {
915-
return "__" + std::string(module->m_name) + "_" + symbol_name;
915+
return "__" + module_name + "_" + symbol_name;
916916
} else {
917917
return symbol_name;
918918
}

0 commit comments

Comments
 (0)