-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve owner handling in Reflection #10352
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
nicolasstucki
merged 1 commit into
scala:master
from
dotty-staging:improve-owner-handling
Nov 19, 2020
Merged
Changes from all commits
Commits
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
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,93 @@ | ||
package x | ||
|
||
import scala.quoted._ | ||
|
||
trait CB[T]: | ||
def map[S](f: T=>S): CB[S] = ??? | ||
def flatMap[S](f: T=>CB[S]): CB[S] = ??? | ||
|
||
class MyArr[AK,AV]: | ||
def map1[BK,BV](f: ((AK,AV)) => (BK, BV)):MyArr[BK,BV] = ??? | ||
def map1Out[BK, BV](f: ((AK,AV)) => CB[(BK,BV)]): CB[MyArr[BK,BV]] = ??? | ||
|
||
def await[T](x:CB[T]):T = ??? | ||
|
||
object CBM: | ||
def pure[T](t:T):CB[T] = ??? | ||
|
||
object X: | ||
|
||
inline def process[T](inline f:T) = ${ | ||
processImpl[T]('f) | ||
} | ||
|
||
def processImpl[T:Type](f:Expr[T])(using qctx: QuoteContext):Expr[CB[T]] = | ||
import qctx.reflect._ | ||
|
||
def transform(term:Term):Term = | ||
term match | ||
case Apply(TypeApply(Select(obj,"map1"),targs),args) => | ||
val nArgs = args.map(x => shiftLambda(x)) | ||
val nSelect = Select.unique(obj, "map1Out") | ||
Apply(TypeApply(nSelect,targs),nArgs) | ||
case Apply(TypeApply(Ident("await"),targs),args) => args.head | ||
case a@Apply(x,List(y,z)) => | ||
val mty=MethodType(List("y1"))( _ => List(y.tpe.widen), _ => TypeRepr.of[CB].appliedTo(a.tpe.widen)) | ||
val mtz=MethodType(List("z1"))( _ => List(z.tpe.widen), _ => a.tpe.widen) | ||
Apply( | ||
TypeApply(Select.unique(transform(y),"flatMap"), | ||
List(Inferred(a.tpe.widen)) | ||
), | ||
List( | ||
Lambda(Symbol.currentOwner, mty, (meth, yArgs) => | ||
Apply( | ||
TypeApply(Select.unique(transform(z),"map"), | ||
List(Inferred(a.tpe.widen)) | ||
), | ||
List( | ||
Lambda(Symbol.currentOwner, mtz, (_, zArgs) => { | ||
val termYArgs = yArgs.asInstanceOf[List[Term]] | ||
val termZArgs = zArgs.asInstanceOf[List[Term]] | ||
Apply(x,List(termYArgs.head,termZArgs.head)) | ||
}) | ||
) | ||
).changeOwner(meth) | ||
) | ||
) | ||
) | ||
case Block(stats, last) => Block(stats, transform(last)) | ||
case Inlined(x,List(),body) => transform(body) | ||
case l@Literal(x) => | ||
l.asExpr match | ||
case '{ $l: lit } => | ||
Term.of('{ CBM.pure(${term.asExprOf[lit]}) }) | ||
case other => | ||
throw RuntimeException(s"Not supported $other") | ||
|
||
def shiftLambda(term:Term): Term = | ||
term match | ||
case lt@Lambda(params, body) => | ||
val paramTypes = params.map(_.tpt.tpe) | ||
val paramNames = params.map(_.name) | ||
val mt = MethodType(paramNames)(_ => paramTypes, _ => TypeRepr.of[CB].appliedTo(body.tpe.widen) ) | ||
Lambda(Symbol.currentOwner, mt, (meth, args) => changeArgs(params,args,transform(body)).changeOwner(meth) ) | ||
case Block(stats, last) => | ||
Block(stats, shiftLambda(last)) | ||
case _ => | ||
throw RuntimeException("lambda expected") | ||
|
||
def changeArgs(oldArgs:List[Tree], newArgs:List[Tree], body:Term):Term = | ||
val association: Map[Symbol, Term] = (oldArgs zip newArgs).foldLeft(Map.empty){ | ||
case (m, (oldParam, newParam: Term)) => m.updated(oldParam.symbol, newParam) | ||
case (m, (oldParam, newParam: Tree)) => throw RuntimeException("Term expected") | ||
} | ||
val changes = new TreeMap() { | ||
override def transformTerm(tree:Term)(using Context): Term = | ||
tree match | ||
case ident@Ident(name) => association.getOrElse(ident.symbol, super.transformTerm(tree)) | ||
case _ => super.transformTerm(tree) | ||
} | ||
changes.transformTerm(body) | ||
|
||
val r = transform(Term.of(f)).asExprOf[CB[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,14 @@ | ||
package x | ||
|
||
object Main { | ||
|
||
def main(args:Array[String]):Unit = | ||
val arr = new MyArr[Int,Int]() | ||
val r = X.process{ | ||
arr.map1( (x,y) => | ||
( 1, await(CBM.pure(x)) ) | ||
) | ||
} | ||
println("r") | ||
|
||
} |
Oops, something went wrong.
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.
This is very helpful, greatly improves debuggability 👍
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.
I would not have been able to fix the tests without this. It proved to be quite useful.