Skip to content

Commit 2ef93ad

Browse files
committed
Merge pull request scala#4863 from retronym/topic/leaner-patmat-codegen
DRY-er trees in pattern matcher code gen.
2 parents e8f3836 + f5f777e commit 2ef93ad

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/compiler/scala/tools/nsc/transform/patmat/MatchCodeGen.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ trait MatchCodeGen extends Interface {
5555
def flatMap(prev: Tree, b: Symbol, next: Tree): Tree
5656
def flatMapCond(cond: Tree, res: Tree, nextBinder: Symbol, next: Tree): Tree
5757
def flatMapGuard(cond: Tree, next: Tree): Tree
58-
def ifThenElseZero(c: Tree, thenp: Tree): Tree = IF (c) THEN thenp ELSE zero
58+
def ifThenElseZero(c: Tree, thenp: Tree): Tree = {
59+
val z = zero
60+
thenp match {
61+
case If(c1, thenp1, elsep1) if z equalsStructure elsep1 =>
62+
If(c AND c1, thenp1, elsep1) // cleaner, leaner trees
63+
case _ =>
64+
If(c, thenp, zero)
65+
}
66+
}
5967
protected def zero: Tree
6068
}
6169

test/files/run/t6288.check

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@
3333
[175]case <synthetic> val x1: [175]Any = [175]"";
3434
[175]case5()[195]{
3535
[195]<synthetic> val o7: [195]Option[List[Int]] = [195][195]Case4.unapplySeq([195]x1);
36-
[195]if ([195]o7.isEmpty.unary_!)
37-
[195]if ([195][195][195][195]o7.get.!=([195]null).&&([195][195][195][195]o7.get.lengthCompare([195]1).==([195]0)))
38-
[208][208]matchEnd4([208]())
39-
else
40-
[195][195]case6()
36+
[195]if ([195][195]o7.isEmpty.unary_!.&&([195][195][195][195]o7.get.!=([195]null).&&([195][195][195][195]o7.get.lengthCompare([195]1).==([195]0))))
37+
[208][208]matchEnd4([208]())
4138
else
4239
[195][195]case6()
4340
};
@@ -59,11 +56,8 @@
5956
[273]case <synthetic> val x1: [273]Any = [273]"";
6057
[273]case5()[293]{
6158
[293]<synthetic> val o7: [293]Option[List[Int]] = [293][293]Case4.unapplySeq([293]x1);
62-
[293]if ([293]o7.isEmpty.unary_!)
63-
[293]if ([293][293][293][293]o7.get.!=([293]null).&&([293][293][293][293]o7.get.lengthCompare([293]0).==([293]0)))
64-
[304][304]matchEnd4([304]())
65-
else
66-
[293][293]case6()
59+
[293]if ([293][293]o7.isEmpty.unary_!.&&([293][293][293][293]o7.get.!=([293]null).&&([293][293][293][293]o7.get.lengthCompare([293]0).==([293]0))))
60+
[304][304]matchEnd4([304]())
6761
else
6862
[293][293]case6()
6963
};

0 commit comments

Comments
 (0)