Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rewrite initialization, increment and condition for replace_doloop #3911

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gxyd
Copy link
Contributor

@gxyd gxyd commented Apr 20, 2024

The transformation set i = a-c initially, was incorrect because it effectively changes the starting value of the loop variable i.

Fixes #3902

…oop`

The transformation set `i = a-c` initially, was incorrect because it effectively
changes the starting value of the loop variable `i`.

Fixes lfortran#3902
@gxyd
Copy link
Contributor Author

gxyd commented Apr 20, 2024

Seems like the below program with the PR's changes would never end:

program main
    implicit none
    integer(4) :: k
    k = 1
    do while (k <= 4)
        if (k == 3) then
            cycle
        end if
        print *, "variable i =", k
        k = k + 1
    end do
end program main

@Pranavchiku
Copy link
Contributor

I think we did this intentionally, I do not know the reason atm. I commented way to fix #3902 there, basically this is old convention of writing codes, we encountered in SciPy and hence introduced --use-loop-variable-after-loop.

@@ -8,6 +8,7 @@ program doloop_04
j = j + i
end do
if (j /= 25) error stop
if (i /= 11) error stop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these changes will work with --use-loop-variable-after-loop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I did initially wonder what the purpose of use_loop_variable_after_loop in replace_doloop is.

@Pranavchiku Pranavchiku marked this pull request as draft April 20, 2024 09:00
@gxyd
Copy link
Contributor Author

gxyd commented Apr 20, 2024

I do not know the reason atm

Maybe the possibility of abrupt ending (via exit) or continuation of loop without going to the end (via cycle) might have been the reason.

@certik can you please confirm? If that's the case, I think there is nothing to do here, as we probably need to use --use-loop-variable-after-loop flag

@gxyd
Copy link
Contributor Author

gxyd commented Apr 20, 2024

I'm curious for if above is the reason, isn't it possible to increment the loop variable before appending the cycle & exit statements?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loop variable scope anomaly
2 participants