Skip to content

Commit 8a66a25

Browse files
committed
Simplify: do not drop complex prefixes to case-class apply & unapply.
Enable all tests under optimise.
1 parent 3cc3121 commit 8a66a25

File tree

5 files changed

+22
-37
lines changed

5 files changed

+22
-37
lines changed

compiler/src/dotty/tools/dotc/transform/linker/Simplify.scala

+21-4
Original file line numberDiff line numberDiff line change
@@ -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
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

+1-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ class CompilationTests extends ParallelTesting {
167167

168168
@Test def runAll: Unit =
169169
compileFilesInDir("../tests/run", defaultOptions).checkRuns()
170-
171-
// The two tests that current fail under -optimise
172-
@Test def runNotOptimised: Unit =
173-
compileFilesInDir("../tests/run-not-optimised", defaultOptions.filterNot("-optimise".==)).checkRuns()
170+
174171

175172
// Pickling Tests ------------------------------------------------------------
176173
//

tests/run-not-optimised/t4859.scala

-29
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)