Skip to content

Commit 9101116

Browse files
authored
Merge pull request #15281 from dotty-staging/fix-15227
Fix hash code of ExprImpl and TypeImpl
2 parents 95d9171 + c6b89d4 commit 9101116

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ final class ExprImpl(val tree: tpd.Tree, val scope: Scope) extends Expr[Any] {
1919
case _ => false
2020
}
2121

22+
override def hashCode(): Int = tree.hashCode()
23+
2224
override def toString: String = "'{ ... }"
2325
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ final class TypeImpl(val typeTree: tpd.Tree, val scope: Scope) extends Type[?] {
1313
case _ => false
1414
}
1515

16+
override def hashCode(): Int = typeTree.hashCode()
17+
1618
override def toString: String = "Type.of[...]"
1719
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted.*
2+
3+
inline def mac[T](inline expr: T): T =
4+
${ impl('expr) }
5+
6+
def impl[T: Type](expr: Expr[T])(using Quotes): Expr[T] = {
7+
import quotes.reflect.*
8+
val expr2 = expr.asTerm.asExpr
9+
10+
assert(expr == expr2)
11+
assert(expr.hashCode() == expr2.hashCode())
12+
13+
expr
14+
15+
}

tests/pos-macros/i15227a/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@main def test =
2+
mac(1)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import scala.quoted.*
2+
3+
inline def mac[T](inline expr: T): T =
4+
${ impl('expr) }
5+
6+
class MyMap() extends ExprMap {
7+
var expressions: List[Expr[Any]] = Nil
8+
9+
override def transform[T](e: Expr[T])(using Type[T])(using q: Quotes): Expr[T] =
10+
expressions ::= e
11+
transformChildren(e)
12+
13+
}
14+
15+
def impl[T: Type](expr: Expr[T])(using Quotes): Expr[T] = {
16+
17+
val List(es1, es2) = List(MyMap(), MyMap()).map { m =>
18+
m.transform(expr)
19+
m.expressions
20+
}
21+
22+
assert(es1 == es2)
23+
assert(es1.map(_.hashCode()) == es2.map(_.hashCode()), s"hash codes not equal:\n${es1.map(_.hashCode())}\n${es2.map(_.hashCode())}")
24+
25+
expr
26+
27+
}

tests/pos-macros/i15227b/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@main def test =
2+
mac(1)

0 commit comments

Comments
 (0)