From f70ed41248eb073af736c78599257d14849025be Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Mon, 13 May 2024 19:05:04 +0200 Subject: [PATCH] Fix erasure crash for Inlined rhs of a ctxfun closure This is achieved by safely removing Inlined nodes at that point, in the same manner they would be removed later in `typedInlined`` in the same phase. --- .../src/dotty/tools/dotc/transform/Erasure.scala | 2 ++ tests/pos-macros/i16963/Macro_1.scala | 14 ++++++++++++++ tests/pos-macros/i16963/Test_2.scala | 1 + 3 files changed, 17 insertions(+) create mode 100644 tests/pos-macros/i16963/Macro_1.scala create mode 100644 tests/pos-macros/i16963/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/transform/Erasure.scala b/compiler/src/dotty/tools/dotc/transform/Erasure.scala index a25a2fcb5c6d..be00d952566c 100644 --- a/compiler/src/dotty/tools/dotc/transform/Erasure.scala +++ b/compiler/src/dotty/tools/dotc/transform/Erasure.scala @@ -945,6 +945,8 @@ object Erasure { vparams = vparams :+ param if crCount == 1 then meth.rhs.changeOwnerAfter(meth.symbol, sym, erasurePhase) else skipContextClosures(meth.rhs, crCount - 1) + case inlined: Inlined => + skipContextClosures(Inlines.dropInlined(inlined), crCount) var rhs1 = skipContextClosures(ddef.rhs.asInstanceOf[Tree], contextResultCount(sym)) diff --git a/tests/pos-macros/i16963/Macro_1.scala b/tests/pos-macros/i16963/Macro_1.scala new file mode 100644 index 000000000000..317d8947abd3 --- /dev/null +++ b/tests/pos-macros/i16963/Macro_1.scala @@ -0,0 +1,14 @@ +import scala.quoted.* + +inline def myMacro = ${ myMacroExpr } + +def myMacroExpr(using Quotes) = + import quotes.reflect.* + + '{ def innerMethod = (_: String) ?=> ???; () }.asTerm match + case block @ Inlined(_, _, Block(List(defdef: DefDef), _)) => + val rhs = + given Quotes = defdef.symbol.asQuotes + '{ (x: String) ?=> ??? }.asTerm + + Block(List(DefDef(defdef.symbol, _ => Some(rhs))), '{}.asTerm).asExprOf[Unit] diff --git a/tests/pos-macros/i16963/Test_2.scala b/tests/pos-macros/i16963/Test_2.scala new file mode 100644 index 000000000000..389f9e3233a2 --- /dev/null +++ b/tests/pos-macros/i16963/Test_2.scala @@ -0,0 +1 @@ +def method: Unit = myMacro