From 324bd3bf400cb1c7efb3647d321e14a6f5995ed6 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Wed, 7 Feb 2024 21:22:11 +0530 Subject: [PATCH 1/2] Fix const step in loop --- src/libasr/pass/pass_utils.cpp | 3 ++- src/lpython/semantics/python_ast_to_asr.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libasr/pass/pass_utils.cpp b/src/libasr/pass/pass_utils.cpp index 4a581ac144..da07eea17d 100644 --- a/src/libasr/pass/pass_utils.cpp +++ b/src/libasr/pass/pass_utils.cpp @@ -893,7 +893,8 @@ namespace LCompilers { if( comp == -1 ) { int increment; bool not_constant_inc = false; - if (!ASRUtils::is_integer(*ASRUtils::expr_type(c))) { + if (!ASRUtils::is_integer(*ASRUtils::type_get_past_const( + ASRUtils::expr_type(c)))) { throw LCompilersException("Do loop increment type should be an integer"); } if (c->type == ASR::exprType::IntegerConstant) { diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 5eb253f8f0..44edc909b1 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5301,7 +5301,8 @@ class BodyVisitor : public CommonVisitor { head.m_start = loop_start; head.m_increment = inc; - if( !ASR::is_a(*ASRUtils::expr_type(inc)) ) { + if( !ASR::is_a(*ASRUtils::type_get_past_const( + ASRUtils::expr_type(inc))) ) { throw SemanticError("For loop increment type should be Integer.", loc); } ASR::expr_t *inc_value = ASRUtils::expr_value(inc); From b26d942d5cc7b877289d1f2bbb5a1a114000aaa7 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Wed, 7 Feb 2024 21:22:27 +0530 Subject: [PATCH 2/2] TEST: Add for const step in loop --- integration_tests/CMakeLists.txt | 1 + integration_tests/loop_08.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 integration_tests/loop_08.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 54584dfc0f..fbe7e0a3b8 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -509,6 +509,7 @@ RUN(NAME loop_04 LABELS cpython llvm c) RUN(NAME loop_05 LABELS cpython llvm c) RUN(NAME loop_06 LABELS cpython llvm c NOFAST) RUN(NAME loop_07 LABELS cpython llvm c) +RUN(NAME loop_08 LABELS cpython llvm c) RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64) RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64) RUN(NAME if_03 FAIL LABELS cpython llvm c NOFAST) diff --git a/integration_tests/loop_08.py b/integration_tests/loop_08.py new file mode 100644 index 0000000000..98ecbe2f7d --- /dev/null +++ b/integration_tests/loop_08.py @@ -0,0 +1,13 @@ +from lpython import i32, Const + +def main0(): + i: i32 + n: Const[i32] = 10 + M2: Const[i32] = 2 + y: i32 = 0 + for i in range(0, n, M2): # each M2 block in A cols and B rows # !!!!!!!!!!!!!! + y = y + 2 + print(y) + assert(y == 10) + +main0()