scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Spurious deprecation warning with partial function #13000

Open lrytz opened 4 months ago

lrytz commented 4 months ago

Scala 2.13.14

@deprecated abstract class K {
  def prop: Boolean
}

class Test {
  @annotation.nowarn("cat=deprecation")
  def l: List[K] = Nil

  def t = l collect {
    case k if k.prop => k.toString
  }
}
➜ sandbox sc T.scala -deprecation
> scala compile --server=false -S 2.13 -d . -release 8 T.scala -deprecation
/Users/luc/code/scala/scala13/sandbox/T.scala:9: warning: class K is deprecated
  def t = l collect {
                    ^
1 warning

The AST is

Test.this.l.collect[String](({
  @SerialVersionUID(value = 0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[K,String] with java.io.Serializable {
    final override def applyOrElse[A1 <: K, B1 >: String](x1: A1, default: A1 => B1): B1 = ((x1.asInstanceOf[K]: K): K @unchecked) match {
      case (k @ _) if k.prop => k.toString()
      case (defaultCase$ @ _) => default.apply(x1)
    };
    final def isDefinedAt(x1: K): Boolean = ((x1.asInstanceOf[K]: K): K @unchecked) match {
      case (k @ _) if k.prop => true
      case (defaultCase$ @ _) => false
    }
  };
  new $anonfun()
}: PartialFunction[K,String]))

In A1 <: K, the TypeTree for <: K has a non-null orig, so it passes here: https://github.com/scala/scala/blob/v2.13.14/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala#L1541-L1542.

Same for the K @unchecked type trees.

synthesizePartialFunction generates untyped untyped trees and calls the typer on them, so the originals are there.

Maybe we can catch isSynthetic of some symbol along the way before we get there. The $anonfun symbol is syntehtic. The applyOrElse / isDefinedAt are not.