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

lazy val reset and not re-evaluated in tail recursion #9730

Closed
scabug opened this issue Mar 31, 2016 · 2 comments
Closed

lazy val reset and not re-evaluated in tail recursion #9730

scabug opened this issue Mar 31, 2016 · 2 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Mar 31, 2016

import scala.annotation.tailrec

object TestLazyTail {

  def main(args: Array[String]): Unit = (() => f(true))()

  @tailrec
  private def f(next: Boolean): Unit = {
    // Should always output 5!
    lazy val x = 5
    println(x)

    if (next) f(false) else ()
  }
}

Output is:

5
0

Expected:

5
5

The necessary conditions I've found for this to show the issue:

  1. The call to the function must be in a closure
  2. The function must be tail optimized
  3. The function must be private
  4. The variable must be a lazy val

Other aspects of this snippet don't seem to impact the result.

@scabug
Copy link
Author

scabug commented Mar 31, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9730?orig=1
Reporter: Branden Visser (mrvisser)
Affected Versions: 2.11.8

@scabug
Copy link
Author

scabug commented Aug 16, 2016

@SethTisue said:
got fixed along the way, somewhere during 2.12 development

@scabug scabug closed this as completed Aug 16, 2016
@scabug scabug added the backend label Apr 7, 2017
@scabug scabug added this to the 2.12.0-M5 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant