diff --git a/compiler/src/scala/quoted/runtime/impl/ExprImpl.scala b/compiler/src/scala/quoted/runtime/impl/ExprImpl.scala index ffd12b2e89b0..b33ba14b9e70 100644 --- a/compiler/src/scala/quoted/runtime/impl/ExprImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/ExprImpl.scala @@ -19,5 +19,7 @@ final class ExprImpl(val tree: tpd.Tree, val scope: Scope) extends Expr[Any] { case _ => false } + override def hashCode(): Int = tree.hashCode() + override def toString: String = "'{ ... }" } diff --git a/compiler/src/scala/quoted/runtime/impl/TypeImpl.scala b/compiler/src/scala/quoted/runtime/impl/TypeImpl.scala index 51df479e4663..36da30e112c8 100644 --- a/compiler/src/scala/quoted/runtime/impl/TypeImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/TypeImpl.scala @@ -13,5 +13,7 @@ final class TypeImpl(val typeTree: tpd.Tree, val scope: Scope) extends Type[?] { case _ => false } + override def hashCode(): Int = typeTree.hashCode() + override def toString: String = "Type.of[...]" } diff --git a/tests/pos-macros/i15227a/Macro_1.scala b/tests/pos-macros/i15227a/Macro_1.scala new file mode 100644 index 000000000000..6eeaca630f06 --- /dev/null +++ b/tests/pos-macros/i15227a/Macro_1.scala @@ -0,0 +1,15 @@ +import scala.quoted.* + +inline def mac[T](inline expr: T): T = + ${ impl('expr) } + +def impl[T: Type](expr: Expr[T])(using Quotes): Expr[T] = { + import quotes.reflect.* + val expr2 = expr.asTerm.asExpr + + assert(expr == expr2) + assert(expr.hashCode() == expr2.hashCode()) + + expr + +} diff --git a/tests/pos-macros/i15227a/Test_2.scala b/tests/pos-macros/i15227a/Test_2.scala new file mode 100644 index 000000000000..e0313a80b286 --- /dev/null +++ b/tests/pos-macros/i15227a/Test_2.scala @@ -0,0 +1,2 @@ +@main def test = + mac(1) diff --git a/tests/pos-macros/i15227b/Macro_1.scala b/tests/pos-macros/i15227b/Macro_1.scala new file mode 100644 index 000000000000..10983379b07c --- /dev/null +++ b/tests/pos-macros/i15227b/Macro_1.scala @@ -0,0 +1,27 @@ +import scala.quoted.* + +inline def mac[T](inline expr: T): T = + ${ impl('expr) } + +class MyMap() extends ExprMap { + var expressions: List[Expr[Any]] = Nil + + override def transform[T](e: Expr[T])(using Type[T])(using q: Quotes): Expr[T] = + expressions ::= e + transformChildren(e) + +} + +def impl[T: Type](expr: Expr[T])(using Quotes): Expr[T] = { + + val List(es1, es2) = List(MyMap(), MyMap()).map { m => + m.transform(expr) + m.expressions + } + + assert(es1 == es2) + assert(es1.map(_.hashCode()) == es2.map(_.hashCode()), s"hash codes not equal:\n${es1.map(_.hashCode())}\n${es2.map(_.hashCode())}") + + expr + +} diff --git a/tests/pos-macros/i15227b/Test_2.scala b/tests/pos-macros/i15227b/Test_2.scala new file mode 100644 index 000000000000..e0313a80b286 --- /dev/null +++ b/tests/pos-macros/i15227b/Test_2.scala @@ -0,0 +1,2 @@ +@main def test = + mac(1)