Skip to content

Inlined non-eta-expanded trees message #11537

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

Merged
merged 2 commits into from
Feb 26, 2021

Conversation

nicolasstucki
Copy link
Contributor

Closes #11483

@nicolasstucki
Copy link
Contributor Author

-- Error: tests/pos-macros/i11483/Test_2.scala:25:11 -------------------------------------------------------------------
25 |       Api.println(Api.doSomething())
   |       ^^^^^^^^^^^
   |       Cannot automaticaly eta-expand calls in macros: x.Api.println
   | This location contains code that was inlined from Test_2.scala:25

inlined at Test_2.scala:24:

@nicolasstucki nicolasstucki marked this pull request as ready for review February 25, 2021 15:18
liufengyun
liufengyun previously approved these changes Feb 25, 2021
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, LGTM

@@ -223,6 +223,9 @@ object EtaExpansion extends LiftImpure {
*/
def etaExpand(tree: Tree, mt: MethodType, xarity: Int)(using Context): untpd.Tree = {
import untpd._
if ctx.phase == Phases.inliningPhase then
report.error(em"Cannot automaticaly eta-expand calls in macros: $tree", tree.srcPos)
return EmptyTree
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the check from the place where etaExpand is called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we can catch it as soon as it is created by the macro and give the stack trace to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it will fail under ycheck with

24 |    X.process[Future,Unit]{ // error
   |    ^
   |Exception occurred while executing macro expansion.
   |java.lang.AssertionError: assertion failed: Reference to a method must be eta-expanded before it is used as an expression: (
   |  {
   |    def $anonfun(x: concurrent.Future[String]): String = 
   |      {
   |        x.await[concurrent.Future, String](x)(x.FutureAsyncMonad)
   |      }
   |    closure($anonfun:Conversion[concurrent.Future[String], String])
   |  }
   |:Conversion[concurrent.Future[String], String]).apply
   |    at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
   |    at scala.quoted.runtime.impl.QuotesImpl$reflect$.scala$quoted$runtime$impl$QuotesImpl$reflect$$$yCheckValidExpr(QuotesImpl.scala:2884)
   |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Apply$.yCheckArgs$$anonfun$1(QuotesImpl.scala:603)
   |    at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
   |    at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
   |    at scala.collection.immutable.List.foreach(List.scala:333)
   |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Apply$.yCheckArgs(QuotesImpl.scala:603)
   |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Apply$.apply(QuotesImpl.scala:595)
   |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Apply$.apply(QuotesImpl.scala:594)
   |    at x.X$.processTree(Macro_1.scala:50)
   |    at x.X$.processTree(Macro_1.scala:45)
   |    at x.X$.processTree(Macro_1.scala:42)
   |    at x.X$.processTree(Macro_1.scala:39)
   |    at x.X$.processImpl(Macro_1.scala:32)

This lets the user know that he created the wrong tree at Macro_1.scala:50

private def yCheckValidExpr(term: Term): term.type =
if yCheck then
assert(!term.tpe.widenDealias.isInstanceOf[dotc.core.Types.MethodicType],
"Reference to a method must be eta-expanded before it is used as an expression: " + term.show)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: maybe provide a reflection API to perform eta-expansion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems such well-formedness checks are very useful in macros development.

Is it possible to centralize the check for the expanded tree? I guess for debugging, the current approach is more helpful, as it shows the stacktrace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: maybe provide a reflection API to perform eta-expansion?

We already have term.etaExpand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to centralize the check for the expanded tree?

Centralize how? Like in normal yCheck?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Centralize how? Like in normal yCheck?

Yes, something like a TreeMap or ReTyper. But as I mentioned above, this might not be a good idea, as it cannot show the stacktrace how an ill-formed tree is created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That did not work in this case because the crash happened before we finished the transformation in the Inlining phase.

@nicolasstucki nicolasstucki merged commit e1de43d into scala:master Feb 26, 2021
@nicolasstucki nicolasstucki deleted the fix-#11483 branch February 26, 2021 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assertion failed during retyping of inlined call.
2 participants