Skip to content

Allow inline vals with alias to constant type #8840

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

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Apr 30, 2020

Allow inline vals with alias to constant type and make them constant fold

@nicolasstucki nicolasstucki self-assigned this Apr 30, 2020
@plokhotnyuk
Copy link

@nicolasstucki nicolasstucki marked this pull request as ready for review April 30, 2020 12:09
@nicolasstucki
Copy link
Contributor Author

@plokhotnyuk that kind of computation is out of the scope of inline val. Inline vals are basically an extension of the current final vals with constant RHS with the difference that the rhs must be a constant and hence is guaranteed to be inlined. What you describe seems to be still in the realm of macros.

@nicolasstucki
Copy link
Contributor Author

@plokhotnyuk you could use the following macro (or something simillar)

  private final val f64Pow5InvSplit: Array[Long] = ${ evalF64Pow5InvSplit }
 
  // Put this in another file (not necessarily another project)
  // Contents of Expr evaluated at compiletime and result lifted into the code to create that array
  import scala.quoted._
  def evalF64Pow5InvSplit(using QuoteContext): Expr[Array[Long]] = Expr {
    val ss = new Array[Long](582)
    var pow5 = BigInt(1)
    var i = 0
    while (i < 582) {
      val inv = ((BigInt(1) << (pow5.bitLength + 121)) / pow5) + 1
      ss(i) = inv.longValue & 0x3FFFFFFFFFFFFFFFL
      ss(i + 1) = (inv >> 62).longValue & 0x3FFFFFFFFFFFFFFFL
      pow5 *= 5
      i += 2
    }
   ss
  }

@nicolasstucki nicolasstucki force-pushed the allow-inline-vals-with-alias-to-constant-type branch from f916747 to 8b23e2b Compare April 30, 2020 13:59
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

@@ -510,7 +510,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
*/
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
val tree1 = ConstFold(tree)
tree1.tpe.widenTermRefExpr match {
tree1.tpe.widenTermRefExpr.dealias match {
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 we should call .normalized, as dealias is just one of many possible normalization operations on types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have that in #8843. But when I tried to normalize it here it did not work. Maybe I need to deeply dealias and then normalize.

@nicolasstucki nicolasstucki merged commit 6f51dbb into scala:master Apr 30, 2020
@nicolasstucki nicolasstucki deleted the allow-inline-vals-with-alias-to-constant-type branch April 30, 2020 18:45
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.

3 participants