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

Failed to optimize a recursive call in tail position #9647

Closed
scabug opened this issue Feb 5, 2016 · 3 comments
Closed

Failed to optimize a recursive call in tail position #9647

scabug opened this issue Feb 5, 2016 · 3 comments

Comments

@scabug
Copy link

scabug commented Feb 5, 2016

The following tail-recursive funciton foo fails to be optimized.

import scala.annotation.tailrec

sealed trait HList
case class HCons[H, T <: HList](head: H, tail: T) extends HList
case object HNil extends HList

@tailrec
def foo[L <: HList](l: L): Unit = l match {
  case HNil => ()
  case HCons(h, t) => foo(t)
}

Here is the REPL output:

Welcome to Scala 2.12.0-333187a-nightly (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.annotation.tailrec

sealed trait HList
case class HCons[H, T <: HList](head: H, tail: T) extends HList
case object HNil extends HList

@tailrec
def foo[L <: HList](l: L): Unit = l match {
  case HNil => ()
  case HCons(h, t) => foo(t)
}

// Exiting paste mode, now interpreting.

<console>:18: error: could not optimize @tailrec annotated method foo: it contains a recursive call not in tail position
       def foo[L <: HList](l: L): Unit = l match {
                                         ^

scala> 
@scabug
Copy link
Author

scabug commented Feb 5, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9647?orig=1
Reporter: Tomas Mikula (tomas.mikula-at-gmail.com)
Affected Versions: 2.11.7, 2.12.0-M3, 2.12.0-RC1
See #8796

@scabug
Copy link
Author

scabug commented Sep 29, 2016

@SethTisue said:
I would have expected a "could not optimize @tailrec annotated method foo: it is called recursively with different type arguments" error.

@scabug
Copy link
Author

scabug commented Sep 29, 2016

@SethTisue said:
with -Xprint:all we see that after patmat runs we have:

                  val t: HList = x2.tail;
                  matchEnd5($iw.this.foo[HList](t))

so this looks like a pattern matcher issue — the transform the matcher does moves the recursive call to non-tail-position

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

No branches or pull requests

2 participants