Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hash code of ExprImpl and TypeImpl #15281

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/ExprImpl.scala
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def test =
mac(1)
27 changes: 27 additions & 0 deletions tests/pos-macros/i15227b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def test =
mac(1)