Skip to content

Fix #10910: Add missing position #11000

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 1 commit into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
object Lambda extends LambdaModule:
def apply(owner: Symbol, tpe: MethodType, rhsFn: (Symbol, List[Tree]) => Tree): Block =
val meth = dotc.core.Symbols.newSymbol(owner, nme.ANON_FUN, Synthetic | Method, tpe)
tpd.Closure(meth, tss => yCheckedOwners(rhsFn(meth, tss.head), meth))
tpd.Closure(meth, tss => yCheckedOwners(rhsFn(meth, tss.head.map(withDefaultPos)), meth))

def unapply(tree: Block): Option[(List[ValDef], Term)] = tree match {
case Block((ddef @ DefDef(_, _, params :: Nil, _, Some(body))) :: Nil, Closure(meth, _))
Expand Down
86 changes: 86 additions & 0 deletions tests/pos-macros/i10910/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package x

import scala.quoted._

trait CB[T]:
def map[S](f: T=>S): CB[S] = ???

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: Quotes):Expr[CB[T]] =
import quotes.reflect._

def transform(term:Term): Either[Term, Term] =
term match
case ap@Apply(Select(obj,"foreach"),args) =>
// println("handle-foreach")
val nArgs = args.map(x => shiftLambda(x).right.get)
val nSelect = Select.unique(obj, "foreach_async")
Right(Apply(nSelect,nArgs))
case ap@Apply(sel@Select(obj,"awrite"),args) =>
// println("handle-awrite")
transform(args.head) match
case Left(unchanded) => Left(ap)
case Right(changed) =>
val r = '{ ${changed.asExprOf[CB[?]]}.map(x => ${Apply(sel,List('x.asTerm)).asExpr}) }
Right(r.asTerm)
case Apply(tap@TypeApply(Ident("await"),targs),args) =>
// println("handle-await")
transform(args.head) match
case Left(unchanged) => Right(unchanged)
case Right(changed) =>
val r = Apply(tap,List(changed))
Right(r)
case Lambda(v, body) => ???
case Block(stats, last) => transform(last) match
case Left(unchanged) => Left(Block(stats, last))
case Right(changed) => Right(Block(stats, changed))
case Inlined(x,List(),body) => transform(body)
case Inlined(x,bindings,body) => transform(body) match
case Left(unchanged) => Left(term)
case Right(changed) => Right(Inlined(x,bindings,changed))
case Typed(arg,tp) => transform(arg)
case Ident(x) => Left(term)
case l@Literal(x) => Left(l)
case other =>
throw RuntimeException(s"Not supported $other")

def shiftLambda(term:Term): Either[Term,Term] =
term match
case lt@Lambda(params, body) =>
transform(body) match
case Left(unchanged) => Left(term)
case Right(nBody) =>
val paramTypes = params.map(_.tpt.tpe)
val paramNames = params.map(_.name+"Changed")
val mt = MethodType(paramNames)(_ => paramTypes, _ => TypeRepr.of[CB].appliedTo(body.tpe.widen) )
val r = Lambda(Symbol.spliceOwner, mt, (owner,args) => changeArgs(params,args,nBody).changeOwner(owner) )
Right(r)
case _ =>
throw RuntimeException("lambda expected")
def changeArgs(oldArgs:List[Tree], newArgs:List[Tree], body:Term):Term =
// println(s"changeArgs: oldArgs=${oldArgs.map(_.symbol.hashCode)}, newArgs=${newArgs.map(_.symbol.hashCode)} ")
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)(owner: Symbol): Term =
tree match
case ident@Ident(name) => association.getOrElse(ident.symbol, super.transformTerm(tree)(owner))
case _ => super.transformTerm(tree)(owner)
}
changes.transformTerm(body)(Symbol.spliceOwner)

transform(f.asTerm) match
case Left(unchanged) => '{ CBM.pure($f) }
case Right(changed) => changed.asExprOf[CB[T]]
31 changes: 31 additions & 0 deletions tests/pos-macros/i10910/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import x._

class Writer[A] {

def awrite(a:A): CB[Unit] = ???

inline def write(a:A):Unit =
await(awrite(a))

}

class Reader[A] {

def foreach_async(f: A=> CB[Unit]): CB[Unit] = ???

def foreach(f: A=>Unit): Unit =
foreach_async(v => CBM.pure(f(v)))

}

object Test {

def main(args:Array[String]):Unit =
val writer = new Writer[Int]()
val reader = new Reader[Int]()
val r = X.process{
reader.foreach( v => writer.write(v) )
}
println("r")

}