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

Nested try with return ignores outer then #2843

Closed
Shorttail opened this issue Jul 27, 2018 · 3 comments · Fixed by #2853
Closed

Nested try with return ignores outer then #2843

Shorttail opened this issue Jul 27, 2018 · 3 comments · Fixed by #2853

Comments

@Shorttail
Copy link

Shorttail commented Jul 27, 2018

Minimal example:

actor Main
  new create(e: Env) =>
    try
      try
        if I32(1) > I32(2) then error end
        e.out.print("try")
        return
      end
      if I32(1) > I32(2) then error end
    else
      e.out.print("else")
    then
      e.out.print("then")
    end

When a try is nested in an outer try, the outer try's then block is ignored if the inner try returns.

@SeanTAllen SeanTAllen changed the title Nested try with return ignores outer then Nested try with return ignores outer then Jul 27, 2018
@mfelsche
Copy link
Contributor

Thanks for this one.
I was hunting this one through all places of the compiler already, phew! I learned a lot during the last couple of days doing that, so thanks for the opportunity.

It turns out the bug is in the codegen pass in the gen_return function where we only actually generate (actually output this part of the code into the binary) the then clause of the innermost try clause as seen from the return statement. That means in the example above, we immediately return without a value, but only execute the implicit then clause (that is not explicitly written above) of the innermost try.

What we need to do is to recurse upward and execute every then clause one after the other of every try clause we hit. I am thinking about ensuring a similar logic with different code-constructs that escape the normal flow, like error.

@mfelsche
Copy link
Contributor

mfelsche commented Aug 2, 2018

Btw, while investigating this i also found that it is possible to skip a then clause using break and continue within a loop. Example:

actor Main
  new create(env: Env) =>
    var i: USize = 0
    while i < 10 do
      try
        i = i + 1
        continue // replace with break, same result, then is never executed
      then
        env.out.print("then")
      end
    end
    env.out.print("done")

@SeanTAllen
Copy link
Member

@Shorttail this is fixed on master. thanks for reporting.

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 a pull request may close this issue.

3 participants