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

Match on inner type generates no compiler warning but fails at runtime #9484

Open
scabug opened this issue Sep 23, 2015 · 5 comments
Open

Match on inner type generates no compiler warning but fails at runtime #9484

scabug opened this issue Sep 23, 2015 · 5 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Sep 23, 2015

The attached sample code compiles fine in Scala 2.11.7 and Scala 2.10.3 (Scala 2.9.3 warns that "TestMain.scala:9: error: scrutinee is incompatible with pattern type") - even with -unchecked -deprecation -feature -Xfuture -Xfatal-warnings -Xlint compiler options - but prints "ChildA#InnerOne: b.InnerOne" when run, i.e. the match at TestMain:8 matches ChildA#InnerOne although x is ChildB#InnerOne (and thus I'd expect it to print "ChildB#InnerOne: b.InnerOne".
While I think the match should be possible (by type-checking the reference to the outer object) the compiler should at least generate a warning (like it does for matching things like case e:Seq[String]=> ???.

@scabug
Copy link
Author

scabug commented Sep 23, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9484?orig=1
Reporter: Bernhard Messerer (messi)
Affected Versions: 2.11.7
See #2, #6583
Attachments:

@scabug
Copy link
Author

scabug commented Sep 24, 2015

@som-snytt said:
Similar search terms might lead one to #2.

@som-snytt
Copy link

At 2.13.8, still no outer checking, but compiler overcompensates by warning:

t9484.scala:9: warning: match may not be exhaustive.
It would fail on the following input: (x: Parent#InnerOne forSome x not in (ChildA#InnerOne, ChildB#InnerOne))
    x match {
    ^

The attached test

object TestMain {
  def main(args:Array[String]): Unit = {
    val b=new ChildB
    val x:Parent#InnerOne=new b.InnerOne("b.InnerOne")
    matchInnerOne(x)
  }
  def matchInnerOne(x:Parent#InnerOne):Unit={
    x match {
      case ao:ChildA#InnerOne⇒ println("ChildA#InnerOne: "+ao.name)
      case bo:ChildB#InnerOne⇒ println("ChildB#InnerOne: "+bo.name)
    }
  }
}

sealed trait Parent {
  sealed trait Inner {
    def name:String
  }
  final class InnerOne(val name:String) extends Inner
  final class InnerTwo(val name:String) extends Inner
}
class ChildA extends Parent
class ChildB extends Parent

Oh wait, ChildX are not final. OK, same result if final.

@SethTisue SethTisue added this to the Backlog milestone Aug 25, 2022
@SethTisue
Copy link
Member

anyone digging into this will probably also want to read #4440

@SethTisue
Copy link
Member

Scala 3 (3.3.1-RC1-bin-20230323-ee2bfdb-NIGHTLY) doesn't even warn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants