Skip to content

ExprMap produces expressions that are equals but have different hashCode #15227

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

Closed
rmgk opened this issue May 18, 2022 · 0 comments · Fixed by #15281
Closed

ExprMap produces expressions that are equals but have different hashCode #15227

rmgk opened this issue May 18, 2022 · 0 comments · Fixed by #15281
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@rmgk
Copy link

rmgk commented May 18, 2022

Compiler version

3.1.2, 3.1.3-RC3, 3.2.0-RC1-bin-20220517-e5abec0-NIGHTLY

Minimized code

The gist of the code is, that it uses an ExprMap to collect all subexpressions. It does that twice, and the compares the found expressions.

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: 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

}

then call the macro in another file with any expression, say mac(1)

Output

[error] java.lang.AssertionError: assertion failed: hash codes not equal:
[error] List(290429894, 1544224853)
[error] List(1268450819, 1544224853)

Expectation

the expressions of the two runs are are equal, so the hashcodes should also be equal

@rmgk rmgk added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 18, 2022
@nicolasstucki nicolasstucki added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 19, 2022
@nicolasstucki nicolasstucki self-assigned this May 19, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 24, 2022
bishabosha pushed a commit to dotty-staging/dotty that referenced this issue Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants