Skip to content

Commit

Permalink
Merge pull request #15281 from dotty-staging/fix-15227
Browse files Browse the repository at this point in the history
Fix hash code of ExprImpl and TypeImpl
  • Loading branch information
bishabosha committed May 24, 2022
2 parents 95d9171 + c6b89d4 commit 9101116
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/ExprImpl.scala
Expand Up @@ -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 = "'{ ... }"
}
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/TypeImpl.scala
Expand Up @@ -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[...]"
}
15 changes: 15 additions & 0 deletions 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

}
2 changes: 2 additions & 0 deletions tests/pos-macros/i15227a/Test_2.scala
@@ -0,0 +1,2 @@
@main def test =
mac(1)
27 changes: 27 additions & 0 deletions 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

}
2 changes: 2 additions & 0 deletions tests/pos-macros/i15227b/Test_2.scala
@@ -0,0 +1,2 @@
@main def test =
mac(1)

0 comments on commit 9101116

Please sign in to comment.