Skip to content

Commit 95c9006

Browse files
committed
Fix a bug where QuoteMatcher does not compare nested erased params
1 parent 9fdd013 commit 95c9006

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,14 @@ object QuoteMatcher {
383383
case scrutinee @ DefDef(_, paramss1, tpt1, _) =>
384384
pattern match
385385
case pattern @ DefDef(_, paramss2, tpt2, _) =>
386-
def matchErasedParams(scrutinee: DefDef, pattern: DefDef): optional[MatchingExprs] =
387-
(scrutinee.tpe.widenTermRefExpr, pattern.tpe.widenTermRefExpr) match
386+
def matchErasedParams(sctype: Type, pttype: Type): optional[MatchingExprs] =
387+
(sctype, pttype) match
388388
case (sctpe: MethodType, pttpe: MethodType) =>
389-
if sctpe.erasedParams.sameElements(pttpe.erasedParams) then matched else notMatched
390-
case _ => notMatched
389+
if sctpe.erasedParams.sameElements(pttpe.erasedParams) then
390+
matchErasedParams(sctpe.resType, pttpe.resType)
391+
else
392+
notMatched
393+
case _ => matched
391394

392395
def matchParamss(scparamss: List[ParamClause], ptparamss: List[ParamClause])(using Env): optional[(Env, MatchingExprs)] =
393396
(scparamss, ptparamss) match {
@@ -400,7 +403,7 @@ object QuoteMatcher {
400403
case _ => notMatched
401404
}
402405

403-
val ematch = matchErasedParams(scrutinee, pattern)
406+
val ematch = matchErasedParams(scrutinee.tpe.widenTermRefExpr, pattern.tpe.widenTermRefExpr)
404407
val (pEnv, pmatch) = matchParamss(paramss1, paramss2)
405408
val defEnv = pEnv + (scrutinee.symbol -> pattern.symbol)
406409

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
case erased: [erased case]
2+
case erased nested: -10

tests/run-custom-args/run-macros-erased/i17105/Macro_1.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ def testExprImpl(body: Expr[Any])(using Quotes): Expr[String] =
88
Expr("This case should not match")
99
case '{ def erasedfn(erased y: String) = "placeholder"; $a(erasedfn): String } =>
1010
'{ $a((erased z: String) => "[erased case]") }
11+
case '{
12+
def erasedfn(a: Int)(b: Int): Int = a
13+
$y(erasedfn): String
14+
} => Expr("This should not match")
15+
case '{
16+
def erasedfn(a: Int)(erased b: Int): Int = a
17+
$y(erasedfn): String
18+
} =>
19+
'{ $y((a: Int) => (erased b: Int) => -a) }
1120
case _ => Expr("not matched")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
@main def Test: Unit =
22
println("case erased: " + testExpr { def erasedfn1(erased x: String) = "placeholder"; erasedfn1("arg1")})
3+
println("case erased nested: " + testExpr { def erasedfn2(p: Int)(erased q: Int) = p; erasedfn2(10)(0).toString() })

0 commit comments

Comments
 (0)