-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix #9894: clean ownership when destructing lambda-expressions #10132
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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,67 @@ | ||
package x | ||
|
||
import scala.quoted._ | ||
|
||
trait CB[T]: | ||
def map[S](f: T=>S): CB[S] = ??? | ||
|
||
class MyArr[A]: | ||
def map1[B](f: A=>B):MyArr[B] = ??? | ||
def map1Out[B](f: A=> CB[B]): CB[MyArr[B]] = ??? | ||
|
||
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 ap@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) | ||
//Apply.copy(ap)(TypeApply(nSelect,targs),nArgs) | ||
case Apply(TypeApply(Ident("await"),targs),args) => args.head | ||
case Apply(x,y) => | ||
Apply(x, y.map(transform)) | ||
case Block(stats, last) => Block(stats, transform(last)) | ||
case Inlined(x,List(),body) => transform(body) | ||
case l@Literal(x) => | ||
'{ CBM.pure(${term.seal}) }.unseal | ||
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, _ => Type[CB].unseal.tpe.appliedTo(body.tpe.widen) ) | ||
val r = Lambda(mt, args => changeArgs(params,args,transform(body)) ) | ||
r | ||
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) | ||
|
||
transform(f.unseal).seal.cast[CB[T]] |
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,15 @@ | ||
package x | ||
|
||
|
||
object Main { | ||
|
||
def main(args:Array[String]):Unit = | ||
val arr = new MyArr[Int]() | ||
val r = X.process{ | ||
arr.map1( zDebug => | ||
await(CBM.pure(1).map(a => zDebug + a)) | ||
) | ||
} | ||
println("r") | ||
|
||
} |
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.
We cannot rely on this body being used inside
ctx.owner
. We need to change adapt it when we construct theLambda
. We have some logic that knows how to do this. I created an alternative fix that does just that in #10142.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 fix was helped me understand the source of the issue. Thank you.
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.
ok, thanks