diff options
author | Gerard Basler <gerard.basler@gmail.com> | 2015-09-12 19:38:22 +0200 |
---|---|---|
committer | Gerard Basler <gerard.basler@gmail.com> | 2015-09-12 19:58:08 +0200 |
commit | bdba16f5de91c4a8ac345c345a674f4ba56b542b (patch) | |
tree | e84a64f26689407ca93c126401a21b9579bed660 | |
parent | 7ec4207a2fa84e7a35f514a911893b7e3cb57672 (diff) | |
download | scala-bdba16f5de91c4a8ac345c345a674f4ba56b542b.tar.gz scala-bdba16f5de91c4a8ac345c345a674f4ba56b542b.tar.bz2 scala-bdba16f5de91c4a8ac345c345a674f4ba56b542b.zip |
SI-9369 Fix pattern matcher warnings for diamond shaped inheritance.
A previous optimization (d44a86f432a7f9ca250b014acdeab02ac9f2c304) for
pattern matcher exhaustivity checks used a smarter encoding to ensure
that the scrutinee can be equal to one child only.
However, in case of traits between the root and leave type, a child can
be of several types and these types should not be in a mutually exclusive
group. A simple solution (hat tip to retronym) is to just put traits
and classes into separate groups.
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala | 6 | ||||
-rw-r--r-- | test/files/pos/t9369.flags | 1 | ||||
-rw-r--r-- | test/files/pos/t9369.scala | 24 |
3 files changed, 30 insertions, 1 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala index a11906ace1..1331eb6993 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchAnalysis.scala @@ -150,7 +150,11 @@ trait TreeAndTypeAnalysis extends Debugging { acc: List[List[Type]]): List[List[Type]] = wl match { case hd :: tl => val children = enumerateChildren(hd) - groupChildren(tl ++ children, acc :+ filterChildren(children)) + // put each trait in a new group, since traits could belong to the same + // group as a derived class + val (traits, nonTraits) = children.partition(_.isTrait) + val filtered = (traits.map(List(_)) ++ List(nonTraits)).map(filterChildren) + groupChildren(tl ++ children, acc ++ filtered) case Nil => acc } diff --git a/test/files/pos/t9369.flags b/test/files/pos/t9369.flags new file mode 100644 index 0000000000..b5a8748652 --- /dev/null +++ b/test/files/pos/t9369.flags @@ -0,0 +1 @@ +-Xfatal-warnings -unchecked diff --git a/test/files/pos/t9369.scala b/test/files/pos/t9369.scala new file mode 100644 index 0000000000..94be2ea4e7 --- /dev/null +++ b/test/files/pos/t9369.scala @@ -0,0 +1,24 @@ +object Test { + + trait Tree + + sealed abstract class Prop + + trait Simple extends Prop + + case class Atom(tree: Tree) extends Prop with Simple + + case class Not(prop: Prop) extends Prop with Simple + + def simplify1(prop: Prop): Prop = prop match { + case Atom(tree) => ??? + case Not(prop) => ??? + case _ => ??? + } + + def simplify2(prop: Prop): Prop = prop match { + case Not(Atom(tree)) => ??? + case Not(Not(prop)) => ??? + case _ => ??? + } +}
\ No newline at end of file |