scala / bug

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

A specialized trait `T` extending a specialized class `C` triggers unnecessary warning 'C must be a trait' #12827

Open noresttherein opened 11 months ago

noresttherein commented 11 months ago

Reproduction steps

Scala version: 2.13.11

    class Specialized[@specialized A] {
        def stackTrace(token :A) = new Exception().getStackTrace.toSeq
    }
    trait Mixin[@specialized A] extends Specialized[A] {
        def trace(token :A) = stackTrace(token)
    }

    object I extends Specialized[Int] with Mixin[Int]
    println(I.trace(0).mkString("", "\n", ""))

Problem

The compiler produces the following warnings:

/home/turin/porn/sugar/src/test/scala/Playground.scala:9:4
class Specialized must be a trait. Specialized version of trait Mixin will inherit generic Playground.Specialized[Int]
    trait Mixin[@specialized(Int) A] extends Specialized[A] {

However, executing the code shows the following stack trace:

Playground$Specialized$mcI$sp.stackTrace$mcI$sp(Playground.scala:7)
Playground$Mixin$mcI$sp.trace$mcI$sp(Playground.scala:10)
Playground$Mixin$mcI$sp.trace$mcI$sp$(Playground.scala:10)
Playground$I$.trace$mcI$sp(Playground.scala:13)
Playground$.delayedEndpoint$Playground$1(Playground.scala:14)
Playground$delayedInit$body.apply(Playground.scala:5)
scala.Function0.apply$mcV$sp(Function0.scala:42)
scala.Function0.apply$mcV$sp$(Function0.scala:42)
scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
scala.App.$anonfun$main$1(App.scala:98)
scala.App.$anonfun$main$1$adapted(App.scala:98)
scala.collection.IterableOnceOps.foreach(IterableOnce.scala:576)
scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:574)
scala.collection.AbstractIterable.foreach(Iterable.scala:933)
scala.App.main(App.scala:98)
scala.App.main$(App.scala:96)
Playground$.main(Playground.scala:5)
Playground.main(Playground.scala)

We can see that Mixin[Int].trace, when invoked, calls Specialized[Int].stackTrace directly, and of course I extends the specialized variant of Specialized, so this composition should be completely legal, as no functionality is lost.