Skip to content

Commit ad95e58

Browse files
authored
Fix issue with certain polyfunctions not properly matching in macros (#23614)
Fixes #23589 previously the contents of TypeBoundsTree were directly compared with the types of defn.Nothing and defn.Any, which caused an issue where: * `def.Nothing.tpe` equals `TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Nothing)` * while the analogous type unpicked from quoted code was `TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Nothing)` Same situation with the upper type bound `Any`. `isAny` and `isNothingType` accept both types, so they are now used instead.
2 parents ceeac54 + 55732ab commit ad95e58

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,10 @@ class QuoteMatcher(debug: Boolean) {
460460
*/
461461
def matchTypeDef(sctypedef: TypeDef, pttypedef: TypeDef): MatchingExprs = sctypedef match
462462
case TypeDef(_, TypeBoundsTree(sclo, schi, EmptyTree))
463-
if sclo.tpe == defn.NothingType && schi.tpe == defn.AnyType =>
463+
if sclo.tpe.isNothingType && schi.tpe.isAny =>
464464
pttypedef match
465465
case TypeDef(_, TypeBoundsTree(ptlo, pthi, EmptyTree))
466-
if sclo.tpe == defn.NothingType && schi.tpe == defn.AnyType =>
466+
if sclo.tpe.isNothingType && schi.tpe.isAny =>
467467
matched
468468
case _ => notMatched
469469
case _ => notMatched

tests/pos-macros/i23589/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.quoted.*
2+
import scala.language.experimental.quotedPatternsWithPolymorphicFunctions
3+
4+
inline def testMacro(inline body: Any) = ${test('body)}
5+
def test(outsideBody: Expr[Any])(using Quotes): Expr[Unit] =
6+
val insideBody = '{[B] => (a : B, b : B) => (a, b)}
7+
outsideBody match
8+
case '{ [A] => (x : A, y : A) => $b[A](x, y): (A, A) } => ()
9+
insideBody match
10+
case '{ [A] => (x : A, y : A) => $b[A](x, y): (A, A) } => ()
11+
'{()}

tests/pos-macros/i23589/Main_2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//> using options -experimental
2+
@main def main() =
3+
testMacro([B] => (a : B, b : B) => (a, b))

0 commit comments

Comments
 (0)