From 7aca3c4feb514190ae31000e7138215e86caeca9 Mon Sep 17 00:00:00 2001 From: Yichen Xu Date: Sun, 3 Mar 2024 23:16:37 +0100 Subject: [PATCH] Fix the pickling of `This` in a capture set --- .../tools/dotc/core/tasty/TreePickler.scala | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala index def27742c189..7d2d95aa9601 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala @@ -405,19 +405,21 @@ class TreePickler(pickler: TastyPickler, attributes: Attributes) { pickleType(tp) } case This(qual) => + // This may be needed when pickling a `This` inside a capture set. See #19662 and #19859. + // In this case, we pickle the tree as null.asInstanceOf[tree.tpe]. + // Since the pickled tree is not the same as the input, special handling is needed + // in the tree printer when testing the pickler. See [[PlainPrinter#homogenize]]. + inline def pickleCapturedThis = + pickleTree(Literal(Constant(null)).cast(tree.tpe).withSpan(tree.span)) if (qual.isEmpty) if tree.tpe.isSingleton then pickleType(tree.tpe) - else - // This may happen when pickling a `This` inside a capture set. See #19662. - // In this case, we pickle the tree as null.asInstanceOf[tree.tpe]. - // Since the pickled tree is not the same as the input, special handling is needed - // in the tree printer when testing the pickler. See [[PlainPrinter#homogenize]]. - pickleTree(Literal(Constant(null)).cast(tree.tpe).withSpan(tree.span)) - else { - writeByte(QUALTHIS) - val ThisType(tref) = tree.tpe: @unchecked - pickleTree(qual.withType(tref)) - } + else pickleCapturedThis + else + tree.tpe match + case ThisType(tref) => + writeByte(QUALTHIS) + pickleTree(qual.withType(tref)) + case _ => pickleCapturedThis case Select(qual, name) => name match { case OuterSelectName(_, levels) =>