Skip to content

Match Error in dotty when trying to do Apply with block as first argument from macro API #18113

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
rssh opened this issue Jul 1, 2023 · 6 comments · Fixed by #18242
Closed
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:enhancement

Comments

@rssh
Copy link
Contributor

rssh commented Jul 1, 2023

Compiler version

3.3.0, 3.3.2-RC1-bin-SNAPSHOT as today

Minimized code

file X.scala:

package x

import scala.quoted.*

class IntRef(var x: Int) {

   def plus(y:Int): Int = ???

}


object X {

   inline def test(ref:IntRef):Int = ${ testImpl('ref) }

   def testImpl(ref:Expr[IntRef])(using Quotes):Expr[Int] = {
     import quotes.reflect.*
     val fun0 = Select.unique(ref.asTerm,"plus")
     val fun1 = Block(List(Assign(Select.unique(ref.asTerm,"x"),Literal(IntConstant(1)))),fun0)
     val r = Apply(fun1,List(Literal(IntConstant(2))))
     r.asExprOf[Int]
   }

}

fille Main.scala:

package x

object Main {

  val ref = IntRef(0)

  X.test(ref)

}

Output

info] compiling 2 Scala sources to /Users/rssh/tests/dotty/bad-apply/target/scala-3.3.2-RC1-bin-SNAPSHOT/classes ...
[error] -- Error: /Users/rssh/tests/dotty/bad-apply/src/main/scala/x/Main.scala:7:8 ----
[error]  7 |  X.test(ref)
[error]    |  ^^^^^^^^^^^
[error]    |Exception occurred while executing macro expansion.
[error]    |java.lang.AssertionError: assertion failed
[error]    |	at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:11)
[error]    |	at dotty.tools.dotc.ast.tpd$.Apply(tpd.scala:51)
[error]    |	at scala.quoted.runtime.impl.QuotesImpl.scala$quoted$runtime$impl$QuotesImpl$reflect$Apply$$$_$apply$$anonfun$14(QuotesImpl.scala:628)
[error]    |	at scala.quoted.runtime.impl.QuotesImpl$reflect$.scala$quoted$runtime$impl$QuotesImpl$reflect$$$withDefaultPos(QuotesImpl.scala:3010)
[error]    |	at scala.quoted.runtime.impl.QuotesImpl$reflect$Apply$.apply(QuotesImpl.scala:628)
[error]    |	at scala.quoted.runtime.impl.QuotesImpl$reflect$Apply$.apply(QuotesImpl.scala:626)
[error]    |	at x.X$.testImpl(X.scala:20)
[error]    |
[error]    |----------------------------------------------------------------------------
[error]    |Inline stack trace
[error]    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]    |This location contains code that was inlined from X.scala:14
[error] 14 |   inline def test(ref:IntRef):Int = ${ testImpl('ref) }
[error]    |                                     ^^^^^^^^^^^^^^^^^^^
[error]     ----------------------------------------------------------------------------
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 3 s, completed 1 Jul 2023, 12:46:27

Expectation

It should be compiled.

@rssh rssh added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 1, 2023
rssh added a commit to dotty-cps-async/dotty-cps-async that referenced this issue Jul 1, 2023
@odersky
Copy link
Contributor

odersky commented Jul 1, 2023

The block is valid only if the result is mapped to a function value. Right now it is a method, which cannot be returned from a block. We could maybe move the block outwards as a convenience feature. But I don't see how not doing that is a bug.

@odersky odersky added itype:enhancement area:metaprogramming:quotes Issues related to quotes and splices and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 1, 2023
@rssh
Copy link
Contributor Author

rssh commented Jul 1, 2023

Hmm, I think that the obvious interpretation is that the result of a block is the last expression of that block, so the block is a functional value.

I.e. Apply with block is a native sequence of calls to recreate the following expression:

({  ref.x=1;  ref.plus })(2) 

(which is possible to write in code).

@odersky
Copy link
Contributor

odersky commented Jul 1, 2023

It's not possible in typed code, though. The typed code equivalent would be

{ ref.x = 1; x => ref.plus(x) }.apply(2)

@odersky
Copy link
Contributor

odersky commented Jul 1, 2023

Or, written out

{ ref.x = 1; { def $anonfun(x: Int) = ref.plus(x); closure($anonfun) }}.apply(2)

@nicolasstucki
Copy link
Contributor

We could detect ill-formed Block expressions like that one by making sure the type of the expression is not a methodic type. This could be tested under -Xcheck-macros.

@rssh
Copy link
Contributor Author

rssh commented Jul 18, 2023

Eta-expanded application also can't be passed to apply.

package x

import scala.quoted.*

class IntRef(var x: Int) {

   def plus(y:Int): Int = ???

}


object X {

   inline def test(ref:IntRef):Int = ${ testImpl('ref) }

   def testImpl(ref:Expr[IntRef])(using Quotes):Expr[Int] = {
     import quotes.reflect.*
     val fun0 = Select.unique(ref.asTerm,"plus")
     val mt = MethodType(List("p"))(
                _ => List(TypeRepr.of[Int]),
                _ => TypeRepr.of[Int]
              )
     val etaExpanded = Lambda(Symbol.spliceOwner, mt, (owner, params) => {
             Block(
               List(Assign(Select.unique(ref.asTerm,"x"),Literal(IntConstant(1)))),
               Apply(fun0,params.map(_.asInstanceOf[Term]))
             )
     })
     val r = Apply(etaExpanded,List(Literal(IntConstant(2))))
     r.asExprOf[Int]
   }


}

We have the same error.

What actually works in ```Apply(Select(etaExpaned,"apply"), params)````

  def testImpl(ref:Expr[IntRef])(using Quotes):Expr[Int] = {
     import quotes.reflect.*
     val fun0 = Select.unique(ref.asTerm,"plus")
     val mt = MethodType(List("p"))(
                _ => List(TypeRepr.of[Int]),
                _ => TypeRepr.of[Int]
              )
     val etaExpanded = Lambda(Symbol.spliceOwner, mt, (owner, params) => {
             Block(
               List(Assign(Select.unique(ref.asTerm,"x"),Literal(IntConstant(1)))),
               Apply(fun0,params.map(_.asInstanceOf[Term]))
             )
     })
     val r = Apply(Select.unique(etaExpanded,"apply"),List(Literal(IntConstant(2))))
     r.asExprOf[Int]
   }

This is not an issue itself, but it should be documented.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 18, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 18, 2023
Also improve the stack trace shown to the user when a macro assertion fails.

Fixes scala#18113
nicolasstucki added a commit that referenced this issue Jul 19, 2023
…18242)

Also improve the stack trace shown to the user when a macro assertion
fails.

Fixes #18113
Kordyjan pushed a commit that referenced this issue Dec 1, 2023
Also improve the stack trace shown to the user when a macro assertion fails.

Fixes #18113

[Cherry-picked ac3edcd]
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:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants