Skip to content

Unreachability check for pattern is not precise enough when the scrutinee is a union #8711

Closed
@smarter

Description

@smarter
class A
class B
class C

object Test {
  def foo(x: A) = x match {
    case x: B => x // as expected, error: this case is unreachable since class A is not a subclass of class B
    case _ =>
  }

  def bar(x: A | B) = x match {
    case x: C => x // no error!
    case _ =>
  }
}

The logic handling this is in TypeTestCast#interceptTypeApply#interceptWith#transformIsInstanceOf which computes a class symbol for the scrutinee:

      def foundCls = effectiveClass(expr.tpe.widen)

So when the pattern has type A | B, testCls will be Any. This logic needs to be adapted to handle union types where there's more than one possible class symbol.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions