Skip to content

Superfluous asInstanceOf breaking singleton types #11220

Closed
@Ang9876

Description

@Ang9876

Compiler version

Scala compiler version 3.0.0-M3

Minimized code

This code is inspired by tests/pos/i4345.scala.

import scala.annotation.tailrec
class Context {
  type Tree
}

final def loop3[C <: Context](): Unit =
  @tailrec
  def loop4[A <: C](c: A): c.Tree = loop4(c)

Output

-- [E007] Type Mismatch Error: test.scala:19:41 --------------------------------
19 |  def loop4[A <: C](c: A): c.Tree = loop4(c)
   |                                    ^^^^^^^^
   |                              Found:    ?1.Tree
   |                              Required: c.Tree
   |
   |                              where:    ?1 is an unknown value of type A
1 error found

Result after typer

result of test.scala after typer:
package <empty> {
  import scala.annotation.tailrec
  class Context() extends Object() {
    type Tree >: Nothing <: Any
  }
  final lazy module val test$package: test$package$ = new test$package$()
  final module class test$package$() extends Object() {
    this: test$package.type =>
    final def loop3[C >: Nothing <: Context](): Unit =
      {
        @annotation.tailrec() def loop4[A >: Nothing <: C](c: A): c.Tree =
          loop4[A](c.$asInstanceOf$[A])
        ()
      }
  }
}

Expectation

This code should be compiled but not. c is converted to c.$asInstanceOf$[A] in method adapt when the compiler testSubType (c:A) and A. testSubType returns OKwithGADTused, but GADT usage in thirdTryNamed is unnecessary.

https://github.com/lampepfl/dotty/blob/a3b7ed601061e3a309fa23f09a740b7759c61bf6/compiler/src/dotty/tools/dotc/typer/Typer.scala#L3418

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions