scala / bug

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

Any wrongly inferred and fails to warn #12852

Open som-snytt opened 10 months ago

som-snytt commented 10 months ago

Reproduction steps

Scala version: 2.13.11

//positioned.getAndRemoveAttachment[NamePos].foreach(att => ts.head.updateAttachment[NamePos](att))
//positioned.getAndRemoveAttachment[NamePos].foreach(ts.head.updateAttachment) // infers Any

import reflect.ClassTag

class C {
  def f[A: ClassTag]: Option[A] = None
  def g[A: ClassTag](a: A): this.type = this
}

class D {
  val c = new C
  def d(): Unit = {
    c.f[String].foreach(c.g)
  }
}

Problem

Per title.

➜  scalac -Vprint -Xlint eta-infer.scala
[[syntax trees at end of                     typer]] // eta-infer.scala
package <empty> {
  import scala.reflect.ClassTag;
  class C extends scala.AnyRef {
    def <init>(): C = {
      C.super.<init>();
      ()
    };
    def f[A](implicit evidence$1: scala.reflect.ClassTag[A]): Option[A] = scala.None;
    def g[A](a: A)(implicit evidence$2: scala.reflect.ClassTag[A]): C.this.type = this
  };
  class D extends scala.AnyRef {
    def <init>(): D = {
      D.super.<init>();
      ()
    };
    private[this] val c: C = new C();
    <stable> <accessor> def c: C = D.this.c;
    def d(): Unit = D.this.c.f[String]((ClassTag.apply[String](classOf[java.lang.String]): scala.reflect.ClassTag[String])).foreach[C](((a: Any) => D.this.c.g[Any](a)((ClassTag.Any: scala.reflect.ClassTag[Any]))))
  }
}

Scala 3 does not infer Any:

➜  scalac -Vprint:refchecks eta-infer.scala
[[syntax trees at end of MegaPhase{firstTransform, checkReentrant, elimPackagePrefixes, cookComments, checkStatic, checkLoopingImplicits, betaReduce, inlineVals, expandSAMs, elimRepeated, refchecks}]] // eta-infer.scala
package <empty> {
  import reflect.ClassTag
  @SourceFile("eta-infer.scala") class C() extends Object() {
    def f[A](implicit evidence$1: reflect.ClassTag[A]): Option[A] = None
    def g[A](a: A)(implicit evidence$2: reflect.ClassTag[A]): (C.this : C) = this
  }
  @SourceFile("eta-infer.scala") class D() extends Object() {
    val c: C = new C()
    def d(): Unit =
      {
        this.c.f[String](scala.reflect.ClassTag.apply[String](classOf[String])).foreach[C](
          {
            def $anonfun(a: String): C = this.c.g[String](a)(scala.reflect.ClassTag.apply[String](classOf[String]))
            closure($anonfun)
          }
        )
      }
  }
}