@@ -192,6 +192,22 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
192
192
case _ => false
193
193
}
194
194
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
+
195
211
/** Inline case class specific methods using desugarings assumptions.
196
212
*
197
213
* - CC.apply(args) → new CC(args)
@@ -230,9 +246,10 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
230
246
a.symbol.owner.companionClass.is(CaseClass ) &&
231
247
! a.tpe.derivesFrom(defn.EnumClass ) &&
232
248
(isPureExpr(a.fun) || a.fun.symbol.is(Synthetic )) =>
249
+
233
250
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)
236
253
}
237
254
else if (a.tpe.derivesFrom(defn.OptionClass ) && a.args.head.tpe.derivesFrom(a.symbol.owner.companionClass)) {
238
255
def some (e : Tree ) = {
@@ -249,7 +266,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
249
266
val none = ref(defn.NoneModuleRef )
250
267
def isNull (e : Tree ) = e.select(defn.Object_eq ).appliedTo(Literal (Constant (null )))
251
268
def fi (e : Tree ) = If (isNull(e), none, some(e))
252
- evalOnce(a.args.head)(fi)
269
+ evalReciever(a, evalOnce(a.args.head)(fi) )
253
270
}
254
271
else a
255
272
case a : Apply if (a.symbol.name == nme.unapplySeq) &&
@@ -269,7 +286,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
269
286
270
287
val recv = reciever(a)
271
288
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 ) )
273
290
else a
274
291
case t => t
275
292
}
0 commit comments