Skip to content

"case BarWrapper(foo)" should fail if the type of foo is unrelated to the type of BarWrapper argument #1942

Closed
@smarter

Description

@smarter

This fails to compile, as expected:

class Foo

class Bar
case class BarWrapper(bar: Bar)

object Test {
  def test(barWrapper: BarWrapper): Unit = {
    val foo = new Foo

    barWrapper match {
      case BarWrapper(_: Foo) =>
      case _ =>
    }
  }
}
-- Error: try/casecomp0.scala --------------------------------------------------
11 |      case BarWrapper(_: Foo) =>
   |                         ^
   |                 will never match since `Foo` is not a subclass of `Bar`

But if you replace case BarWrapper(_: Foo) => by case BarWrapper(`foo`) =>, the file will compile without any error or warning in both scalac and dotty. I find this very concerning because it means that you cannot safely refactor any case class by changing the type of one of its parameter, for example in #1941 I replaced case class PostfixOp(od: Tree, op: Name) by case class PostfixOp(od: Tree, op: Ident), fixed the compilation errors, and expected everything to be fine. But the compiler failed to warn me about the now impossible pattern matches like case PostfixOp(_, nme.raw.STAR), so tests failed.

@odersky WDYT?

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