Skip to content

Reflection regression from 2.13.0-M2(or 2.12.4) to 2.13.0-M3, 2.13.0-M4 #10758

@soronpo

Description

@soronpo

Update:

Thanks to @retronym we know the first regression for 2.13.0-M3 occurred at the following commit scala/scala@dd31672 of this PR scala/scala#6140
The second regression added another error for 2.13.0-M4, but maybe it's a side-effect of the primary issue.


Minimized at https://github.com/soronpo/ScalaRegression

Macros.scala

import scala.reflect.macros.whitebox.Context

final class TwoFaceInt[T](val value : Int) {
  def + [R](that : TwoFaceInt[R]) = ???
}
object TwoFaceInt {
  def apply[T <: Int, Out <: T](value : T) : TwoFaceInt[Out] = macro Builder.Macro.fromNumValue[T]
}

object Builder {
  final class Macro(val c: Context) {
    def fromNumValue[T](value : c.Tree)(implicit t : c.WeakTypeTag[T]) : c.Tree = {
      import c.universe._
      val tTpe = weakTypeOf[T]
      val valueTpe = value match {
        case Literal(Constant(t : Int)) => c.internal.constantType(Constant(t))
        case _ => tTpe
      }
      q"new TwoFaceInt[$valueTpe]($value)"
    }
  }
}

Test.scala

object Test {
    TwoFaceInt(1) + TwoFaceInt(2)
}

✅ 2.12.4
✅ 2.12.6
✅ 2.13.0-M2
❌ 2.13.0-M3 with one error:

[error] /Test.scala:2: type mismatch;
[error]  found   : TwoFaceInt[Int(2)]
[error]  required: TwoFaceInt[Nothing]
[error] Note: Int(2) >: Nothing, but class TwoFaceInt is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error]   TwoFaceInt(1) + TwoFaceInt(2)
[error]                             ^
[error] one error found

❌ 2.13.0-M4 with two errors:

[error] /Test.scala:2: type mismatch;
[error]  found   : TwoFaceInt[Int(2)]
[error]  required: TwoFaceInt[Nothing]
[error] Note: Int(2) >: Nothing, but class TwoFaceInt is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error]   TwoFaceInt(1) + TwoFaceInt(2)
[error]                             ^
[error] /Test.scala:2: type mismatch;
[error]  found   : TwoFaceInt[Int(1)]
[error]  required: TwoFaceInt[Nothing]
[error] Note: Int(1) >: Nothing, but class TwoFaceInt is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error]   TwoFaceInt(1) + TwoFaceInt(2)
[error]             ^
[error] two errors found

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions