Skip to content

Strange behaviour related to type test of path-dependent types and divergence from Scala 2 #20354

Open
@liufengyun

Description

@liufengyun

Compiler version

3.5.0-RC1-bin-20240506-1cdf99f-NIGHTLY

Minimized code

trait CmdLineParser { outer =>

  trait Opt[+T] {
    val default: T
    val names: Set[String]
    val help: String
  }

  trait IntOpt extends Opt[Int] {
    val parser = outer                          //       <=== comment out this line, we get "true true"
  }
}

object FirstParser extends CmdLineParser {
  object OptMinSuccess extends IntOpt {
    val default = 100
    val names = Set("bla")
    val help = "bla"
  }

  val opts = List(OptMinSuccess)
}

object SecondParser extends CmdLineParser {
  object OptMinSuccess extends IntOpt {
    val default = 50
    val names = Set("bla")
    val help = "help"
  }
}


val a = SecondParser.OptMinSuccess.isInstanceOf[FirstParser.IntOpt]

println(a)

(SecondParser.OptMinSuccess: SecondParser.IntOpt)  match {
  case _: FirstParser.IntOpt => println("true")
  case _ => println("false")
}

Output

true
false

If we comment out the following line in trait IntOpt:

  trait IntOpt extends Opt[Int] {
    // val parser = outer                          //       <=== comment out this line, we get "true true"
  }

We get

true
true

In contrast, Scala 2 reports a compilation error for the pattern match in both versions:

pattern type is incompatible with expected type;
 found   : Playground.FirstParser.IntOpt
 required: Playground.SecondParser.IntOpt

Expectation

  • We would expect the output to be always the same, ideally false false
  • Change code in a trait should not change type test result

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions