From 194de0418ca545462716e3226c586030fe5942b8 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Thu, 25 Jul 2019 15:45:25 +1000 Subject: [PATCH] Improve static elision of outer tests in pattern matcher When the binder's info is a bounded type, look at the prefix of the upper bound. This stops an annoying unchecked warning that appeared since a recent refactoring made `TermName` a final class. It also avoids a needless runtime check. --- .../tools/nsc/transform/patmat/MatchTreeMaking.scala | 12 +++++++++++- test/files/pos/t11534.flags | 1 + test/files/pos/t11534.scala | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/t11534.flags create mode 100644 test/files/pos/t11534.scala diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala index 4a6731744dc..c960d702823 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchTreeMaking.scala @@ -349,7 +349,17 @@ trait MatchTreeMaking extends MatchCodeGen with Debugging { override def withOuterTest(orig: Tree)(testedBinder: Symbol, expectedTp: Type): Tree = { val expectedPrefix = expectedTp.prefix - val testedPrefix = testedBinder.info.prefix + def prefixOf(tp: Type): Type = tp.normalize match { + case RefinedType(parents, _) => + val temp1 = parents.filterNot(_.typeSymbol.isStatic) + val temp2 = temp1.map(sym => prefixOf(sym)) + temp2.distinct match { + case prefix :: Nil => prefix + case _ => NoType + } + case tp => tp.prefix + } + val testedPrefix = prefixOf(abstractTypesToBounds(testedBinder.info)) // Check if a type is defined in a static location. Unlike `tp.isStatic` before `flatten`, // this also includes methods and (possibly nested) objects inside of methods. diff --git a/test/files/pos/t11534.flags b/test/files/pos/t11534.flags new file mode 100644 index 00000000000..b5a87486527 --- /dev/null +++ b/test/files/pos/t11534.flags @@ -0,0 +1 @@ +-Xfatal-warnings -unchecked diff --git a/test/files/pos/t11534.scala b/test/files/pos/t11534.scala new file mode 100644 index 00000000000..2e36dfec5b7 --- /dev/null +++ b/test/files/pos/t11534.scala @@ -0,0 +1,7 @@ +object Test1 { + val g: scala.tools.nsc.Global = ??? + import g._ + def test(sym: Symbol) = sym.name match { + case _: TermName => + } +}