@@ -192,6 +192,22 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
192192 case _ => false
193193 }
194194
195+ // Apply fun may be a side-effectful function. E.g. a block, see tests/run/t4859.scala
196+ // we need to maintain expressions that were in this block
197+ private def evalReciever (a : Apply , res : Tree ) = {
198+ def receiver (t : Tree ):
199+ (Tree ) = t match {
200+ case TypeApply (fun, targs) if fun.symbol eq t.symbol => receiver(fun)
201+ case Apply (fn, args) if fn.symbol == t.symbol => receiver(fn)
202+ case Select (qual, _) => qual
203+ case x => x
204+ }
205+
206+ val recv = receiver(a)
207+
208+ if (recv.isEmpty || tpd.isPureRef(recv)) res else Block (recv :: Nil , res)
209+ }
210+
195211 /** Inline case class specific methods using desugarings assumptions.
196212 *
197213 * - CC.apply(args) → new CC(args)
@@ -230,9 +246,10 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
230246 a.symbol.owner.companionClass.is(CaseClass ) &&
231247 ! a.tpe.derivesFrom(defn.EnumClass ) &&
232248 (isPureExpr(a.fun) || a.fun.symbol.is(Synthetic )) =>
249+
233250 if (! a.symbol.owner.is(Scala2x )) {
234- if (a.tpe.derivesFrom(defn.BooleanClass )) Literal (Constant (true ))
235- else a .args.head
251+ if (a.tpe.derivesFrom(defn.BooleanClass )) evalReciever(a, Literal (Constant (true ) ))
252+ else evalReciever(a, a .args.head)
236253 }
237254 else if (a.tpe.derivesFrom(defn.OptionClass ) && a.args.head.tpe.derivesFrom(a.symbol.owner.companionClass)) {
238255 def some (e : Tree ) = {
@@ -249,7 +266,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
249266 val none = ref(defn.NoneModuleRef )
250267 def isNull (e : Tree ) = e.select(defn.Object_eq ).appliedTo(Literal (Constant (null )))
251268 def fi (e : Tree ) = If (isNull(e), none, some(e))
252- evalOnce(a.args.head)(fi)
269+ evalReciever(a, evalOnce(a.args.head)(fi) )
253270 }
254271 else a
255272 case a : Apply if (a.symbol.name == nme.unapplySeq) &&
@@ -269,7 +286,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
269286
270287 val recv = reciever(a)
271288 if (recv.typeSymbol.is(Module ))
272- New (a.tpe.translateParameterized(defn.OptionClass , defn.SomeClass ), a.args.head :: Nil )
289+ evalReciever(a, New (a.tpe.translateParameterized(defn.OptionClass , defn.SomeClass ), a.args.head :: Nil ) )
273290 else a
274291 case t => t
275292 }
0 commit comments