Skip to content

Fix #16899 #17068

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ object TypeTestsCasts {
// always false test warnings are emitted elsewhere
X.classSymbol.exists && P.classSymbol.exists &&
!X.classSymbol.asClass.mayHaveCommonChild(P.classSymbol.asClass)
|| X.classSymbol.isOneOf(FinalOrSealed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option being sealed doesn't mean that isInstanceOf[Option[Int]] isn't unchecked. Except of course that Unset | Option[Int] without sealed Unset means the scrutinee after case 1 is Option[Int], but I don't think we calculate that in type tests cast, and in a way we can't, because the match tree has been transformed. There's three places (that I'm aware of) where we look at the match trees: typing, pattern match translation, and space engine analysis. Perhaps in one of those we can find a way to annotate that the Option[Int] test is fully checked. Even in the case that isn't not the type of the scrutinee, like:

(v: Unset | Option[Int] | List[Long]) match
  case v: Unset =>
  case v: Option[Int] =>
  case v: List[Long] =>

|| typeArgsTrivial(X, tpe)
||| i"its type arguments can't be determined from $X"
}
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/16899.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sealed trait Unset

def foo(v: Unset|Option[Int]): Unit = v match
case v: Unset => ()
case v: Option[Int] => ()