Skip to content

Copying of function application with default parameter in macro cause an error with -Xcheck-macro. (regression) #17445

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

Open
rssh opened this issue May 9, 2023 · 1 comment · May be fixed by #21323
Assignees
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:bug needs-sip A SIP needs to be raised to move this issue/PR along.

Comments

@rssh
Copy link
Contributor

rssh commented May 9, 2023

Compiler version

3.1.1-RC1-bin-SNAPSHOT. (master at May. 9.2023)

Minimized code

Macro.scala:

package x

import scala.quoted.*
import scala.collection.*


object Macro {

    def myIndexWhere[A](arrOps: ArrayOps[A])(p: A=>Boolean, from:Int):Int =
      ???

    inline def changeIndexWhere[A](inline expr:A):A = ${
        changeIndexWhereImpl('expr)
    }

    def changeIndexWhereImpl[A:Type](expr:Expr[A])(using Quotes):Expr[A] = {
        import quotes.reflect.*
        val r0 = expr.asTerm 
        val checker = new TreeMap() {}
        val r = checker.transformTerm(r0)(Symbol.spliceOwner)
        r.asExprOf[A]
    }

}

X.scala:

package x

object X {

  def complileMe:Int = {
    Macro.changeIndexWhere{
      val arr = Array(1,2,3)
      val result = arr.indexWhere(_==2)
      result
    }
  }

}

Output

When compiling with -Xcheck-macros

info] Executing in batch mode. For better performance use sbt's shell
[info] compiling 2 Scala sources to /Users/rssh/tests/dotty/fail-defparam/target/scala-3.3.1-RC1-bin-SNAPSHOT/classes ...
[error] -- Error: /Users/rssh/tests/dotty/fail-defparam/src/main/scala/x/X.scala:8:26 --
[error]  8 |    Macro.changeIndexWhere{
[error]    |    ^
[error]    |Malformed tree was found while expanding macro with -Xcheck-macros.
[error]    |               |The tree does not conform to the compiler's tree invariants.
[error]    |               |
[error]    |               |Macro was:
[error]    |               |scala.quoted.runtime.Expr.splice[scala.Int](((evidence$2: scala.quoted.Quotes) ?=> x.Macro.changeIndexWhereImpl[scala.Int](scala.quoted.runtime.Expr.quote[scala.Int]({
[error]    |  val arr: scala.Array[scala.Int] = scala.Array.apply(1, 2, 3)
[error]    |  val result: scala.Int = {
[error]    |    val $2$: scala.collection.ArrayOps[scala.Int] = scala.Predef.intArrayOps(arr)
[error]    |    $2$.indexWhere(((_$2: scala.Int) => _$2.==(2)), $2$.indexWhere$default$2)
[error]    |  }
[error]    |
[error]    |  (result: scala.Int)
[error]    |}).apply(using evidence$2))(scala.quoted.Type.of[scala.Int](evidence$2), evidence$2)))
[error]    |               |
[error]    |               |The macro returned:
[error]    |               |{
[error]    |  val arr: scala.Array[scala.Int] = scala.Array.apply(1, 2, 3)
[error]    |  val result: scala.Int = {
[error]    |    val $2$: scala.collection.ArrayOps[scala.Int] = scala.Predef.intArrayOps(arr)
[error]    |    $2$.indexWhere(((_$2: scala.Int) => _$2.==(2)), $2$.indexWhere$default$2)
[error]    |  }
[error]    |
[error]    |  (result: scala.Int)
[error]    |}
[error]    |               |
[error]    |               |Error:
[error]    |               |assertion failed: symbols differ for $2$.indexWhere$default$2
[error]    |was                 : method indexWhere$default$2
[error]    |alternatives by type:  of types 
[error]    |qualifier type      : ($2$ : scala.collection.ArrayOps[Int])
[error]    |tree type           : ($2$.indexWhere$default$2 : => Int @uncheckedVariance) of class class dotty.tools.dotc.core.Types$CachedTermRe
[error]    |               |
[error]    |stacktrace available when compiling with `-Ydebug`
[error]    |               |
[error]  9 |      val arr = Array(1,2,3)
[error] 10 |      val result = arr.indexWhere(_==2)
[error] 11 |      result
[error] 12 |    }
[error]    |----------------------------------------------------------------------------
[error]    |Inline stack trace
[error]    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]    |This location contains code that was inlined from Macro.scala:12
[error] 12 |    inline def changeIndexWhere[A](inline expr:A):A = ${
[error]    |                                                      ^
[error] 13 |        changeIndexWhereImpl('expr)
[error] 14 |    }
[error]     ----------------------------------------------------------------------------
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 5 s, completed 9 May 2023, 18:12:50

Expectation

It should be compiled.

After a short investigation, I have discovered that in TreeChecker, we seek for existence of SimpleName for indexWhere$default$2
when decl.members of the appropriate symbol contains DerivedName

@rssh rssh added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 9, 2023
@nicolasstucki nicolasstucki added area:metaprogramming:reflection Issues related to the quotes reflection API and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 10, 2023
@nicolasstucki nicolasstucki self-assigned this May 10, 2023
@prolativ prolativ added the regression This worked in a previous version but doesn't anymore label May 10, 2023
@prolativ
Copy link
Contributor

According to the bisect script:

Last good release: 3.3.0-RC1-bin-20230115-ed5b119-NIGHTLY
First bad release: 3.3.0-RC1-bin-20230116-d99d9bf-NIGHTLY

bisect found first bad commitPrevious HEAD position was 49df37931f Use tree checker for macro expanded trees

This seems to point to #16570

@rssh rssh changed the title Copying of function application with default macro casue an error with -Xcheck-macro. (regression) Copying of function application with default parameter in macro casue an error with -Xcheck-macro. (regression) May 11, 2023
@SethTisue SethTisue changed the title Copying of function application with default parameter in macro casue an error with -Xcheck-macro. (regression) Copying of function application with default parameter in macro cause an error with -Xcheck-macro. (regression) Nov 9, 2023
hamzaremmal added a commit to hamzaremmal/scala3 that referenced this issue Aug 2, 2024
@hamzaremmal hamzaremmal linked a pull request Aug 2, 2024 that will close this issue
hamzaremmal added a commit to hamzaremmal/scala3 that referenced this issue Nov 19, 2024
@Gedochao Gedochao added the needs-sip A SIP needs to be raised to move this issue/PR along. label Dec 16, 2024
@Gedochao Gedochao removed the regression This worked in a previous version but doesn't anymore label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:bug needs-sip A SIP needs to be raised to move this issue/PR along.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants