Skip to content

Fix #10709: Add missing level check before inlining #10781

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
Dec 14, 2020

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Dec 14, 2020

If an a call to an inline method is within a quote, this call must not be inlined.

  • Delay inlining within quotes.
  • Disallow inline def within quotes. Same as inline def in inline def.
  • Disable tests/run-staging/i3876-{d,e}.scala. This test requires inlining after Pickler to be able to inline the missing expression. This will be fixed by Expand non-transparent macros after Typer #9984 and it only affects scala.quoted.staging.run.

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.

LGTM

@nicolasstucki
Copy link
Contributor Author

Just to double-check. @liufengyun did you see the second commit while reviewing?

@nicolasstucki nicolasstucki marked this pull request as ready for review December 14, 2020 11:50
@liufengyun
Copy link
Contributor

Just to double-check. @liufengyun did you see the second commit while reviewing?

Thanks. For tests/run-staging/i3876-e.scala, it is not obvious to me why inlineLambda is inlined after Pickler:

import scala.quoted._
import scala.quoted.staging._
object Test {
  def main(args: Array[String]): Unit = {
    given Toolbox = Toolbox.make(getClass.getClassLoader)

    def x(using Quotes): Expr[Int] = '{ println(); 3 }

    def f4(using Quotes): Expr[Int => Int] = '{
      inlineLambda
    }
    println(run(Expr.betaReduce('{$f4($x)})))
    println(withQuotes(Expr.betaReduce('{$f4($x)}).show))
  }

  transparent inline def inlineLambda: Int => Int = x => x + x
}

If an a call to an inline method is within a quote, this call must not be inlined.

* Delay inlining within quotes.
* Disallow `inline def` within quotes. Same as `inline def` in `inline def`.
This tests requires inlining after Pickler to be able to inline the missing expression. This will be fixed by scala#9984 and it only affects `scala.quoted.staging.run`.
@nicolasstucki
Copy link
Contributor Author

nicolasstucki commented Dec 14, 2020

Indeed, it is not obvious.

why inlineLambda is inlined after Pickler

The issue is that it is not, but it should. This is only needed in scala.quoted.staging as it has a special compiler that starts after Pickler.

Previously we would evaluate '{$f4($x)} to '{ (x => x + x).apply({println(); 3}) }.

Now we evaluate '{$f4($x)} to '{ inlineLambda({println(); 3}) }. In this particular case the betaReduce operation would be a no-opp, but BetaReduce phase would reduce it later. If we were to use this value in a macro then the code inlineLambda({println(); 3}) would be inserted somewhere and then inline calls from it would be inlined by Typer. In i3876-e we do not pass again in typer after generating '{ inlineLambda({println(); 3}) } and therefore we do not get a chance to inline this call. #9984 will make it possible to inline after pickler. This phase can also be used in the special instance of the compiler used for scala.quoted.staging.run.

@liufengyun
Copy link
Contributor

Thanks for the detailed explanation, it makes sense 👍

@nicolasstucki
Copy link
Contributor Author

The downside of this PR is that it will temporarily break scala.quoted.staging.run that runs code with calls to inline methods.

@liufengyun
Copy link
Contributor

The downside of this PR is that it will temporarily break scala.quoted.staging.run that runs code with calls to inline methods.

I don't think it matters much, as it's not as commonly used as macros and the fix is coming in RC1.

@nicolasstucki nicolasstucki merged commit 041e5dd into scala:master Dec 14, 2020
@nicolasstucki nicolasstucki deleted the fix-#10709 branch December 14, 2020 16:38
@nicolasstucki nicolasstucki added the release-notes Should be mentioned in the release notes label Dec 14, 2020
@nicolasstucki
Copy link
Contributor Author

#10803 will fix scala.quoted.staging.run

@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Should be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

summonInline[B <:< A] compiles when quoted despite B not being a subtype of A
3 participants