Skip to content

@tailrec false positive #21642

Open
Open
@sir-wabbit

Description

@sir-wabbit

Compiler version

3.5.0

Minimized code

EDIT: found a way to minimize the example:

@tailrec def minimal9_2[A](value: Option[Int]): A = {
  value match {
    case Some(n) => minimal9_2(None)
    case _ => ???
  }
}

Original snippet:

import scala.annotation.tailrec

final class Need[+A] private (private var thunk: Any) {
  def value: A = ???
}

object Need {
  type ThunkD[+A] = AnyRef
  final case class BindL[A](node: Need[A], run: Any => Need[A]) extends ThunkD[A]
  type BindR[A] = Need[A] // <: ThunkD[A]

  import java.util.{ArrayList => Q}
  final class BufRef(var buf: Q[ThunkD[Any]] = null)

  @tailrec def evalSlow[A, Z](ref: BufRef, count: Int, current: Need[A]): Z = {
    val r = current.thunk
    val value: ThunkD[Any] = ???
    value match {
      case BindL(c, f) =>
        val next = f(r)
        c.thunk = next.thunk
        ref.buf.add(c: BindR[Any])
        evalSlow(ref, count, next)
      case c =>
        val c1 = c.asInstanceOf[BindR[_]]
        c1.thunk = r
        evalSlow(ref, count - 1, c1)
    }
  }
}

Output

Cannot rewrite recursive call: it is not in tail position

on both evalSlow calls.

Expectation

Should work since they are in tail position, works in Scala 2.12.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions