-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package x | ||
|
||
import scala.annotation._ | ||
import scala.quoted._ | ||
import scala.compiletime._ | ||
|
||
|
||
trait CpsMonad[F[_]]: | ||
|
||
def pure[T](x:T):F[T] | ||
|
||
def impure[T](x:F[T]):T | ||
|
||
def map[A,B](x:F[A])(f: A=>B):F[B] | ||
|
||
|
||
@compileTimeOnly("await should be inside async block") | ||
def await[F[_],T](f:F[T])(using am:CpsMonad[F]):T = ??? | ||
|
||
inline given conversion[F[_],T](using CpsMonad[F]): Conversion[F[T],T] = | ||
x => await(x) | ||
|
||
|
||
object X { | ||
|
||
inline def process[F[_], T](inline t:T)(using m: CpsMonad[F]):F[T] = | ||
${ processImpl[F,T]('t, 'm) } | ||
|
||
|
||
def processImpl[F[_]:Type, T:Type](t:Expr[T], m:Expr[CpsMonad[F]])(using Quotes):Expr[F[T]] = | ||
import quotes.reflect._ | ||
val r = processTree[F,T](t.asTerm, m.asTerm) | ||
r.asExprOf[F[T]] | ||
|
||
|
||
def processTree[F[_]:Type, T:Type](using Quotes)(t: quotes.reflect.Term, m: quotes.reflect.Term):quotes.reflect.Term = | ||
import quotes.reflect._ | ||
val r: Term = t match | ||
case Inlined(_,List(),body) => processTree[F,T](body, m) | ||
case Inlined(d,bindings,body) => | ||
Inlined(d,bindings,processTree[F,T](body, m)) | ||
case Block(stats,expr) => Block(stats,processTree(expr, m)) | ||
case Apply(Apply(TypeApply(Ident("await"),targs),List(body)),List(m)) => body | ||
case Apply(f,List(arg)) => | ||
val nArg = processTree[F,String](arg, m) | ||
Apply(Apply(TypeApply(Select.unique(m,"map"), | ||
List(Inferred(arg.tpe.widen),Inferred(t.tpe.widen)) | ||
), | ||
List(nArg)), | ||
List(f) | ||
) | ||
case Apply(f,List()) => | ||
Apply(TypeApply(Select.unique(m,"pure"),List(Inferred(t.tpe.widen))),List(t)) | ||
case Typed(x,tp) => Typed(processTree(x,m), Inferred(TypeRepr.of[F].appliedTo(tp.tpe)) ) | ||
case _ => throw new RuntimeException(s"tree not recoginized: $t") | ||
r | ||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package x | ||
|
||
import scala.language.implicitConversions | ||
import scala.concurrent.Future | ||
|
||
given FutureAsyncMonad: CpsMonad[Future] with | ||
def pure[T](t:T): Future[T] = ??? | ||
def impure[T](t:Future[T]): T = ??? | ||
def map[A,B](x:Future[A])(f: A=>B): Future[B] = ??? | ||
|
||
|
||
object Api: | ||
|
||
def doSomething():Future[String] = | ||
Future.successful("doSomething") | ||
|
||
def println(x:String):Unit = | ||
Console.println(x) | ||
|
||
|
||
object Main: | ||
|
||
def main(args: Array[String]): Unit = | ||
X.process[Future,Unit]{ // error | ||
Api.println(Api.doSomething()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have
term.etaExpand
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, something like a
TreeMap
orReTyper
. 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.There was a problem hiding this comment.
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.