@@ -584,67 +584,80 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
584
584
}
585
585
586
586
/** The index of the first difference between lists of trees `xs` and `ys`,
587
- * where `EmptyTree`s in the second list are skipped.
587
+ * where initial `EmptyTree`s in the second list are skipped.
588
+ * -1 if there are no differences.
589
+ */
590
+ private def firstDiffSkipInitEmptyTrees [T <: Trees .Tree [_]](xs : List [T ], ys : List [T ], n : Int = 0 ): Int = xs match {
591
+ case x :: xs1 =>
592
+ ys match {
593
+ case EmptyTree :: ys1 => firstDiffSkipInitEmptyTrees(xs, ys1, n)
594
+ case y :: ys1 => if (x ne y) n else firstDiff(xs1, ys1, n + 1 )
595
+ case nil => n
596
+ }
597
+ case nil =>
598
+ ys match {
599
+ case EmptyTree :: ys1 => firstDiffSkipInitEmptyTrees(xs, ys1, n)
600
+ case y :: ys1 => n
601
+ case nil => - 1
602
+ }
603
+ }
604
+
605
+ /** The index of the first difference between lists of trees `xs` and `ys`
588
606
* -1 if there are no differences.
589
607
*/
590
608
private def firstDiff [T <: Trees .Tree [_]](xs : List [T ], ys : List [T ], n : Int = 0 ): Int = xs match {
591
609
case x :: xs1 =>
592
610
ys match {
593
- case EmptyTree :: ys1 => firstDiff(xs, ys1, n)
594
611
case y :: ys1 => if (x ne y) n else firstDiff(xs1, ys1, n + 1 )
595
612
case nil => n
596
613
}
597
614
case nil =>
598
615
ys match {
599
- case EmptyTree :: ys1 => firstDiff(xs, ys1, n)
600
616
case y :: ys1 => n
601
617
case nil => - 1
602
618
}
603
619
}
604
- private def sameSeq [T <: Trees .Tree [_]](xs : List [T ], ys : List [T ]): Boolean = firstDiff(xs, ys) < 0
605
620
606
621
val result = {
607
622
var typedArgs = typedArgBuf.toList
608
623
def app0 = cpy.Apply (app)(normalizedFun, typedArgs) // needs to be a `def` because typedArgs can change later
609
624
val app1 =
610
625
if (! success) app0.withType(UnspecifiedErrorType )
611
626
else {
612
- if (! (sameSeq( args, orderedArgs) && ! orderedArgs.contains( EmptyTree )) && ! isJavaAnnotConstr(methRef.symbol)) {
627
+ if (firstDiffSkipInitEmptyTrees( args, orderedArgs) >= 0 && ! isJavaAnnotConstr(methRef.symbol)) {
613
628
// need to lift arguments to maintain evaluation order in the
614
629
// presence of argument reorderings.
615
630
liftFun()
616
- val eqSuffixLength = firstDiff (app.args.reverse, orderedArgs.reverse)
631
+ val eqSuffixLength = firstDiffSkipInitEmptyTrees (app.args.reverse, orderedArgs.reverse)
617
632
val (liftable, rest) = typedArgs splitAt (typedArgs.length - eqSuffixLength)
618
633
634
+ // Mapping of index of each `liftable` into original args ordering
619
635
var indices = ListBuffer .empty[Int ]
620
636
var nextDefaultParamIndex = args.size
621
637
var prefixShift = 0
622
638
if (liftedDefs.nonEmpty) {
623
639
indices += 0
624
640
prefixShift = 1
625
641
}
626
- for (l <- liftable) {
627
- val pure = l match {
628
- case NamedArg (_, arg) => isPureExpr(arg)
629
- case arg => isPureExpr(arg)
630
- }
631
-
632
- if (pure) { }
633
- else if (! args.contains(l)) {
634
- indices += prefixShift + nextDefaultParamIndex
635
- nextDefaultParamIndex += 1
636
- }
637
- else indices += prefixShift + args.indexOf(l)
642
+ liftable.foreach {
643
+ case NamedArg (_, arg) if isPureExpr(arg) =>
644
+ case arg if isPureExpr(arg) =>
645
+ case arg =>
646
+ if (args.contains(arg)) {
647
+ indices += prefixShift + args.indexOf(arg)
648
+ } else {
649
+ indices += prefixShift + nextDefaultParamIndex
650
+ nextDefaultParamIndex += 1
651
+ }
638
652
}
639
653
640
654
typedArgs = liftArgs(liftedDefs, methType, liftable) ++ rest
641
655
642
- assert(liftedDefs.size == indices.size)
643
- val newOrder = (liftedDefs zip indices).sortBy(_._2).unzip._1.toList
656
+ val newOrder = (liftedDefs zip indices).sortBy(_._2).unzip._1
644
657
liftedDefs = ListBuffer .empty
645
658
liftedDefs ++= newOrder
646
659
}
647
- if (sameSeq (typedArgs, args)) // trick to cut down on tree copying
660
+ if (firstDiff (typedArgs, args) < 0 ) // trick to cut down on tree copying
648
661
typedArgs = args.asInstanceOf [List [Tree ]]
649
662
assignType(app0, normalizedFun, typedArgs)
650
663
}
0 commit comments