scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Typer gets types wrong, even though they are explicit

Open rcano opened this issue 6 months ago • 0 comments

Compiler version

3.7.1

Minimized code

//> using scala 3.7.1
object CompilerBug {

  trait Signal[T] {
    class Projection[U] extends Signal[U]
  }

  case class Bounds(x: Double, y: Double, width: Double, height: Double)

  object BoundsSignal extends Signal[Bounds] {
    object loc extends Projection[(x: Double, y: Double)] {
      val x = Projection[Double]()
    }
  }

  val f: Signal[Bounds] = BoundsSignal // ✅
  val f2: Signal[(x: Double, y: Double)] = BoundsSignal.loc  // ✅
  val f3: Signal[Double] = BoundsSignal.loc.x  // ❌
}

Output

[error] ./compilerbug.sc:18:28
[error] Found:    (compilerbug$_.CompilerBug.BoundsSignal.loc.x :
[error]   compilerbug$_.CompilerBug.BoundsSignal.loc.Projection[Double])
[error] Required: compilerbug$_.CompilerBug.Signal[Double]
[error] 
[error] Explanation
[error] ===========
[error] 
[error] Tree: this.BoundsSignal.loc.x
[error] I tried to show that
[error]   (compilerbug$_.CompilerBug.BoundsSignal.loc.x :
[error]   compilerbug$_.CompilerBug.BoundsSignal.loc.Projection[Double])
[error] conforms to
[error]   compilerbug$_.CompilerBug.Signal[Double]
[error] but none of the attempts shown below succeeded:
[error] 
[error]   ==> (compilerbug$_.CompilerBug.BoundsSignal.loc.x :   compilerbug$_.CompilerBug.BoundsSignal.loc.Projection[Double])  <:  compilerbug$_.CompilerBug.Signal[Double]
[error]     ==> compilerbug$_.CompilerBug.Signal[(Double, Double)]  <:  compilerbug$_.CompilerBug.Signal[Double]
[error]       ==> Double  <:  (Double, Double)  = false
[error] 
[error] The tests were made under the empty constraint
[error]   val f3: Signal[Double] = BoundsSignal.loc.x
[error]                            ^^^^^^^^^^^^^^^^^^

Expectation

this should typecheck correctly

rcano avatar Jun 10 '25 14:06 rcano