Skip to content

Fix macros with erased arguments #18431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions compiler/src/dotty/tools/dotc/quoted/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
view.toList

fnType.dealias match
case fnType: MethodType if fnType.hasErasedParams => interpretArgs(argss, fnType.resType)
case fnType: MethodType =>
val argTypes = fnType.paramInfos
assert(argss.head.size == argTypes.size)
interpretArgsGroup(argss.head, argTypes) ::: interpretArgs(argss.tail, fnType.resType)
val nonErasedArgs = argss.head.lazyZip(fnType.erasedParams).collect { case (arg, false) => arg }.toList
val nonErasedArgTypes = fnType.paramInfos.lazyZip(fnType.erasedParams).collect { case (arg, false) => arg }.toList
assert(nonErasedArgs.size == nonErasedArgTypes.size)
interpretArgsGroup(nonErasedArgs, nonErasedArgTypes) ::: interpretArgs(argss.tail, fnType.resType)
case fnType: AppliedType if defn.isContextFunctionType(fnType) =>
val argTypes :+ resType = fnType.args: @unchecked
interpretArgsGroup(argss.head, argTypes) ::: interpretArgs(argss.tail, resType)
Expand Down Expand Up @@ -328,8 +330,8 @@ object Interpreter:
object Call:
import tpd._
/** Matches an expression that is either a field access or an application
* It retruns a TermRef containing field accessed or a method reference and the arguments passed to it.
*/
* It returns a TermRef containing field accessed or a method reference and the arguments passed to it.
*/
def unapply(arg: Tree)(using Context): Option[(RefTree, List[List[Tree]])] =
Call0.unapply(arg).map((fn, args) => (fn, args.reverse))

Expand All @@ -339,10 +341,8 @@ object Interpreter:
Some((fn, args))
case fn: Ident => Some((tpd.desugarIdent(fn).withSpan(fn.span), Nil))
case fn: Select => Some((fn, Nil))
case Apply(f @ Call0(fn, args1), args2) =>
if (f.tpe.widenDealias.hasErasedParams) Some((fn, args1))
else Some((fn, args2 :: args1))
case TypeApply(Call0(fn, args), _) => Some((fn, args))
case Apply(f @ Call0(fn, argss), args) => Some((fn, args :: argss))
case TypeApply(Call0(fn, argss), _) => Some((fn, argss))
case _ => None
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/pos-macros/erasedArgs/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted._
import scala.language.experimental.erasedDefinitions

transparent inline def mcr: Any = ${ mcrImpl(1, 2d, "abc") }

def mcrImpl(x: Int, erased y: Double, z: String)(using Quotes): Expr[String] =
Expr(x.toString() + z)
1 change: 1 addition & 0 deletions tests/pos-macros/erasedArgs/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test: "1abc" = mcr