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

Exhaustivity warnings on nested case classes #13030

Merged
merged 1 commit into from
Jul 12, 2021
Merged
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
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ class SpaceEngine(using Context) extends SpaceLogic {
}

private def exhaustivityCheckable(sel: Tree): Boolean = {
val seen = collection.mutable.Set.empty[Type]

// Possible to check everything, but be compatible with scalac by default
def isCheckable(tp: Type): Boolean =
val tpw = tp.widen.dealias
Expand All @@ -815,16 +817,15 @@ class SpaceEngine(using Context) extends SpaceLogic {
isCheckable(and.tp1) || isCheckable(and.tp2)
}) ||
tpw.isRef(defn.BooleanClass) ||
classSym.isAllOf(JavaEnumTrait)
classSym.isAllOf(JavaEnumTrait) ||
classSym.is(Case) && {
if seen.add(tpw) then productSelectorTypes(tpw, sel.srcPos).exists(isCheckable(_))
else true // recursive case class: return true and other members can still fail the check
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems false aligns better with the protocol that if a component is sealed, then perform the check.

Copy link
Member Author

Choose a reason for hiding this comment

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

Given something like case class Foo(parent: Option[Foo], other: Bar) this else branch is hit when we've running isCheckable(Foo) on the Foo in the Option. My thinking is we can optimistically return true here, and then whether or not Bar is checkable will determine if the top Foo is checkable. So I think true is right here, no?

Copy link
Contributor

Choose a reason for hiding this comment

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

Given the following type:

case class A(a: A)

IIRC, the implementation above will have isCheckable(A) == true. If that's as expected, then true is fine. Otherwise, it seems false should be used here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'm happy with that result.

}

val res = !sel.tpe.hasAnnotation(defn.UncheckedAnnot) && {
ctx.settings.YcheckAllPatmat.value
|| isCheckable(sel.tpe)
|| {
val tpw = sel.tpe.widen.dealias
val classSym = tpw.classSymbol
classSym.is(Case) && productSelectorTypes(tpw, sel.srcPos).exists(isCheckable(_))
}
}

debug.println(s"exhaustivity checkable: ${sel.show} = $res")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@ import java.nio.file.{Path => JPath}
import scala.io.Source._
import org.junit.Test

class PatmatDefaultExhaustivityTest extends PatmatExhaustivityTest {
override val testsDir = "tests/patmat-default"
override val options = super.options.filter(_ != "-Ycheck-all-patmat")
}

class PatmatExhaustivityTest {
val testsDir = "tests/patmat"
// stop-after: patmatexhaust-huge.scala crash compiler
val options = List("-pagewidth", "80", "-color:never", "-Ystop-after:explicitSelf", "-Ycheck-all-patmat", "-classpath", TestConfiguration.basicClasspath)
def options = List("-pagewidth", "80", "-color:never", "-Ystop-after:explicitSelf", "-Ycheck-all-patmat", "-classpath", TestConfiguration.basicClasspath)

private def compile(files: Seq[String]): Seq[String] = {
val stringBuffer = new StringWriter()
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
val printWriter = new PrintWriter(stringBuffer)
val reporter = TestReporter.simplifiedReporter(printWriter)

try {
Main.process((options ++ files).toArray, reporter, null)
} catch {
case e: Throwable =>
println(s"Compile $files exception:")
e.printStackTrace()
e.printStackTrace(printWriter)
}

stringBuffer.toString.trim.replaceAll("\\s+\n", "\n") match {
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/patmat-default/i13003.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
4: Pattern Match Exhaustivity: One(Two(None))
7: Pattern Match Exhaustivity: Two(None)
10: Pattern Match Exhaustivity: None, Some(None)
13: Pattern Match Exhaustivity: None, Some(None), Some(Some(None))
14 changes: 14 additions & 0 deletions tests/patmat-default/i13003.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
case class One(two: Two)
case class Two(o: Option[Int])

def matchOneTwo(one: One) = one match
case One(Two(Some(i))) => "match!"

def matchTwo(two: Two) = two match
case Two(Some(i)) => "match!"

def matchOO(oo: Option[Option[Int]]) = oo match
case Some(Some(i)) => "match!"

def matchOOO(ooo: Option[Option[Option[Int]]]) = ooo match
case Some(Some(Some(i))) => "match!"