Skip to content

Commit 3df2ed3

Browse files
Fix macros with erased arguments (#18431)
The interpreter was dropping all arguments in the list if one of them was erased. Now we filter out only the erased arguments. Part of #18305
2 parents 450d233 + df7b345 commit 3df2ed3

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

compiler/src/dotty/tools/dotc/quoted/Interpreter.scala

+8-8
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
126126
view.toList
127127

128128
fnType.dealias match
129-
case fnType: MethodType if fnType.hasErasedParams => interpretArgs(argss, fnType.resType)
130129
case fnType: MethodType =>
131130
val argTypes = fnType.paramInfos
132131
assert(argss.head.size == argTypes.size)
133-
interpretArgsGroup(argss.head, argTypes) ::: interpretArgs(argss.tail, fnType.resType)
132+
val nonErasedArgs = argss.head.lazyZip(fnType.erasedParams).collect { case (arg, false) => arg }.toList
133+
val nonErasedArgTypes = fnType.paramInfos.lazyZip(fnType.erasedParams).collect { case (arg, false) => arg }.toList
134+
assert(nonErasedArgs.size == nonErasedArgTypes.size)
135+
interpretArgsGroup(nonErasedArgs, nonErasedArgTypes) ::: interpretArgs(argss.tail, fnType.resType)
134136
case fnType: AppliedType if defn.isContextFunctionType(fnType) =>
135137
val argTypes :+ resType = fnType.args: @unchecked
136138
interpretArgsGroup(argss.head, argTypes) ::: interpretArgs(argss.tail, resType)
@@ -328,8 +330,8 @@ object Interpreter:
328330
object Call:
329331
import tpd._
330332
/** Matches an expression that is either a field access or an application
331-
* It retruns a TermRef containing field accessed or a method reference and the arguments passed to it.
332-
*/
333+
* It returns a TermRef containing field accessed or a method reference and the arguments passed to it.
334+
*/
333335
def unapply(arg: Tree)(using Context): Option[(RefTree, List[List[Tree]])] =
334336
Call0.unapply(arg).map((fn, args) => (fn, args.reverse))
335337

@@ -339,10 +341,8 @@ object Interpreter:
339341
Some((fn, args))
340342
case fn: Ident => Some((tpd.desugarIdent(fn).withSpan(fn.span), Nil))
341343
case fn: Select => Some((fn, Nil))
342-
case Apply(f @ Call0(fn, args1), args2) =>
343-
if (f.tpe.widenDealias.hasErasedParams) Some((fn, args1))
344-
else Some((fn, args2 :: args1))
345-
case TypeApply(Call0(fn, args), _) => Some((fn, args))
344+
case Apply(f @ Call0(fn, argss), args) => Some((fn, args :: argss))
345+
case TypeApply(Call0(fn, argss), _) => Some((fn, argss))
346346
case _ => None
347347
}
348348
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted._
2+
import scala.language.experimental.erasedDefinitions
3+
4+
transparent inline def mcr: Any = ${ mcrImpl(1, 2d, "abc") }
5+
6+
def mcrImpl(x: Int, erased y: Double, z: String)(using Quotes): Expr[String] =
7+
Expr(x.toString() + z)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test: "1abc" = mcr

0 commit comments

Comments
 (0)