Skip to content

Create extractors for Tasty Trees #4279

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 21 commits into from
May 24, 2018

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Apr 9, 2018

  • Define TASTY trees and types in scala.tasty
  • Add method get a TASTY term from a quoted.Expr[T] and a quoted.Type[T]
  • Implement TASTY tree and type extractors

@nicolasstucki nicolasstucki self-assigned this Apr 9, 2018
@nicolasstucki nicolasstucki changed the title WIP Create extractors for Tasty Trees Create extractors for Tasty Trees Apr 9, 2018
@nicolasstucki nicolasstucki force-pushed the tasty-extractors branch 6 times, most recently from 3eb7a5e to 7fa845c Compare April 13, 2018 13:55
@nicolasstucki nicolasstucki force-pushed the tasty-extractors branch 3 times, most recently from 15670a8 to 4683e5d Compare April 20, 2018 15:58

object Lines {
inline def line(dummy: Int): Int = ~lineImpl('(dummy))
def lineImpl(dummy: Expr[Int]): Expr[Int] = (dummy.toTasty.pos.startLine + 1).toExpr
Copy link
Contributor

Choose a reason for hiding this comment

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

Would that work?

object Lines {
  implicit object CurrentPos
  inline def line(implicit pos: CurrentPos.type): Int = ~lineImpl('(pos))
  def lineImpl(dummy: Expr[CurrentPos.type]): Expr[Int] = (dummy.toTasty.pos.startLine + 1).toExpr
}

import Lines._
object Test {
  def main(args: Array[String]): Unit = {
    println(line)
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that and it does not. The issue is that currently, the implicitly inferred tree does not have a position. We should add the position in the future to allow this use case to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even better, this one works and does not need an additional implicit

class LineNumber(val value: Int) {
  override def toString: String = value.toString
}

object LineNumber {
  implicit inline def line[T]: LineNumber = ~impl2('[T])

  def impl2[T](x: Type[T]): Expr[LineNumber] = {
    val pos = {
      val (tree, ctx) = x.toTasty
      tree.pos(ctx)
    }
    '(new LineNumber(~pos.startLine.toExpr))
  }
}

Note that now the context is needed. The important part is that the quoted type has the position we need.

def foo(implicit line: LineNumber): Unit = {
  println("foo " + line)
}
foo(line)
foo

@nicolasstucki nicolasstucki requested a review from odersky April 23, 2018 17:25
@nicolasstucki nicolasstucki requested a review from smarter April 24, 2018 13:46
@nicolasstucki nicolasstucki force-pushed the tasty-extractors branch 2 times, most recently from a7f7502 to 603d753 Compare April 24, 2018 14:58
@nicolasstucki nicolasstucki force-pushed the tasty-extractors branch 4 times, most recently from 780c2e1 to 1def5a9 Compare April 25, 2018 17:36
@nicolasstucki
Copy link
Contributor Author

@odersky this PR is ready for review

@nicolasstucki
Copy link
Contributor Author

@OlivierBlanvillain this is the PR for tasty reflection

Copy link
Contributor

@odersky odersky 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

Rebased

@nicolasstucki nicolasstucki merged commit 6876240 into scala:master May 24, 2018
@Blaisorblade Blaisorblade deleted the tasty-extractors branch May 24, 2018 09:50
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.

4 participants