From 014ebc4606b6c3ecd305cee672f9ca734df77079 Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Wed, 21 Dec 2016 14:39:34 -0800 Subject: Small cleanups to pattern matcher While investigating https://github.com/scala/scala-dev/issues/251 --- .../nsc/transform/patmat/MatchOptimization.scala | 4 +-- .../nsc/transform/patmat/MatchTranslation.scala | 30 ++++++++++++---------- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'src/compiler/scala/tools/nsc/transform') diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchOptimization.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchOptimization.scala index cca8d2dbb8..f827043094 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/MatchOptimization.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchOptimization.scala @@ -116,8 +116,8 @@ trait MatchOptimization extends MatchTreeMaking with MatchAnalysis { case _ => } - debug.patmat("sharedPrefix: "+ sharedPrefix) - debug.patmat("suffix: "+ sharedPrefix) + debug.patmat(s"sharedPrefix: $sharedPrefix") + debug.patmat(s"suffix: $suffix") // if the shared prefix contains interesting conditions (!= True) // and the last of such interesting shared conditions reuses another treemaker's test // replace the whole sharedPrefix by a ReusingCondTreeMaker diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala index a2afb76b0e..bf3bc6b26e 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala @@ -110,25 +110,29 @@ trait MatchTranslation { // example check: List[Int] <:< ::[Int] private def extractorStep(): TranslationStep = { - def paramType = extractor.aligner.wholeType import extractor.treeMaker - // chain a type-testing extractor before the actual extractor call - // it tests the type, checks the outer pointer and casts to the expected type - // TODO: the outer check is mandated by the spec for case classes, but we do it for user-defined unapplies as well [SPEC] - // (the prefix of the argument passed to the unapply must equal the prefix of the type of the binder) - lazy val typeTest = TypeTestTreeMaker(binder, binder, paramType, paramType)(pos, extractorArgTypeTest = true) - // check whether typetest implies binder is not null, - // even though the eventual null check will be on typeTest.nextBinder - // it'll be equal to binder casted to paramType anyway (and the type test is on binder) - def extraction: TreeMaker = treeMaker(typeTest.nextBinder, typeTest impliesBinderNonNull binder, pos) // paramType = the type expected by the unapply // TODO: paramType may contain unbound type params (run/t2800, run/t3530) - val makers = ( + val makers = { + val paramType = extractor.aligner.wholeType // Statically conforms to paramType if (this ensureConformsTo paramType) treeMaker(binder, false, pos) :: Nil - else typeTest :: extraction :: Nil - ) + else { + // chain a type-testing extractor before the actual extractor call + // it tests the type, checks the outer pointer and casts to the expected type + // TODO: the outer check is mandated by the spec for case classes, but we do it for user-defined unapplies as well [SPEC] + // (the prefix of the argument passed to the unapply must equal the prefix of the type of the binder) + val typeTest = TypeTestTreeMaker(binder, binder, paramType, paramType)(pos, extractorArgTypeTest = true) + val binderKnownNonNull = typeTest impliesBinderNonNull binder + + // check whether typetest implies binder is not null, + // even though the eventual null check will be on typeTest.nextBinder + // it'll be equal to binder casted to paramType anyway (and the type test is on binder) + typeTest :: treeMaker(typeTest.nextBinder, binderKnownNonNull, pos) :: Nil + } + } + step(makers: _*)(extractor.subBoundTrees: _*) } -- cgit v1.2.3