Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern Matching Bug When matching against case classes defined in trait #9672

Open
scabug opened this issue Feb 24, 2016 · 3 comments
Open
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) mixin patmat
Milestone

Comments

@scabug
Copy link

scabug commented Feb 24, 2016

When pattern matching, compiler :

  • doesn't emit the "match is not exhaustive" warning (it should)
  • emits "unreachable code" (it should not)
trait Hierarchy {
  sealed trait Expr
}
trait If {
  this: Hierarchy =>
  case class If(cond: Expr, yes: Expr, no: Expr) extends Expr
}
trait Word {
  this: Hierarchy =>
  case class Word(name: String) extends Expr
}
trait IntExpr {
  this: Hierarchy =>
  case class IntExpr(value : Int) extends Expr
}

object SimpleExpr extends Hierarchy with If with Word with IntExpr
//object OtherExpr extends Hierarchy with If with IntExpr

object Demo extends App {
  import SimpleExpr._
  def func(expr: Expr) = expr match {
    case If(cond, yes, no) => cond
    case Word(name) => name
    // compiler should emit warning "missing case statement"
    // emits the wrong warning "unreachable code"
  }
}

see also here : http://stackoverflow.com/questions/35259337/scala-compiler-emits-no-warning-when-pattern-matching-against-a-sealed-trait

@scabug
Copy link
Author

scabug commented Feb 24, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9672?orig=1
Reporter: Julien Harbulot (JulienH)
Affected Versions: 2.11.7

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@hrhino
Copy link
Member

hrhino commented Aug 7, 2018

potentially related, and simpler if so:

  trait M
  sealed trait A
  class B extends A
  class C extends A

  val k: A with M = new B with M {}

  k match {
    case _: B => ???
    case _: C => ??? // unreachable code warning here
  }

(reported on gitter by @eugengarkusha)

@adriaanm adriaanm removed their assignment Sep 28, 2018
@martijnhoekstra
Copy link

martijnhoekstra commented Nov 4, 2019

Another variation reported on gitter by @disblader

sealed trait Test

class TestOne(val id: Int) extends Test
class TestTwo(val id: String) extends Test

trait Testorino { this: Test =>
  val reachable = this match {
    case _: TestOne => false
    case _: TestTwo => true
  }
}

object Leaf extends TestTwo("two") with Testorino

Leaf.reachable //true

@dwijnand dwijnand added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Jan 29, 2021
@dwijnand dwijnand self-assigned this Feb 4, 2021
@dwijnand dwijnand removed their assignment Nov 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) mixin patmat
Projects
None yet
Development

No branches or pull requests

5 participants