Skip to content

Commit 13a593d

Browse files
committed
Avoid reassigning this = this in the tailrec transform.
1 parent 8873eb0 commit 13a593d

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

compiler/src/dotty/tools/dotc/transform/TailRec.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,13 @@ class TailRec extends MiniPhase {
349349
yield
350350
(getVarForRewrittenParam(param), arg)
351351

352-
val assignThisAndParamPairs =
353-
if (prefix eq EmptyTree) assignParamPairs
354-
else
355-
// TODO Opt: also avoid assigning `this` if the prefix is `this.`
352+
val assignThisAndParamPairs = prefix match
353+
case EmptyTree =>
354+
assignParamPairs
355+
case prefix: This if prefix.symbol == enclosingClass =>
356+
// Avoid assigning `this = this`
357+
assignParamPairs
358+
case _ =>
356359
(getVarForRewrittenThis(), noTailTransform(prefix)) :: assignParamPairs
357360

358361
val assignments = assignThisAndParamPairs match {

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -976,28 +976,24 @@ class TestBCode extends DottyBytecodeTest {
976976
Op(ICONST_0),
977977
Jump(IF_ICMPNE, Label(7)),
978978
VarOp(ILOAD, 2),
979-
Jump(GOTO, Label(26)),
979+
Jump(GOTO, Label(22)),
980980
Label(7),
981-
VarOp(ALOAD, 0),
982-
VarOp(ASTORE, 3),
983981
VarOp(ILOAD, 1),
984982
Op(ICONST_1),
985983
Op(ISUB),
986-
VarOp(ISTORE, 4),
984+
VarOp(ISTORE, 3),
987985
VarOp(ILOAD, 2),
988986
VarOp(ILOAD, 1),
989987
Op(IMUL),
990-
VarOp(ISTORE, 5),
991-
VarOp(ALOAD, 3),
992-
VarOp(ASTORE, 0),
993-
VarOp(ILOAD, 4),
988+
VarOp(ISTORE, 4),
989+
VarOp(ILOAD, 3),
994990
VarOp(ISTORE, 1),
995-
VarOp(ILOAD, 5),
991+
VarOp(ILOAD, 4),
996992
VarOp(ISTORE, 2),
997-
Jump(GOTO, Label(29)),
998-
Label(26),
993+
Jump(GOTO, Label(25)),
994+
Label(22),
999995
Op(IRETURN),
1000-
Label(29),
996+
Label(25),
1001997
Jump(GOTO, Label(0)),
1002998
Op(NOP),
1003999
Op(ATHROW),

0 commit comments

Comments
 (0)