Skip to content

Commit

Permalink
fixed bug with interleaved typeapply and apply in extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Apr 2, 2024
1 parent 17b4c8d commit ee403b4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ val dottyVersion = "3.3.3"
//val dottyVersion = "3.4.2-RC1-bin-SNAPSHOT"


ThisBuild/version := "0.9.21"
ThisBuild/version := "0.9.21-SNAPSHOT"
ThisBuild/versionScheme := Some("semver-spec")
ThisBuild/resolvers ++= Opts.resolver.sonatypeOssSnapshots

Expand Down
12 changes: 11 additions & 1 deletion compiler-plugin/src/test/scala/cc/Test15PE.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc

import cc.DotcInvocations.IsAlreadyCompiledFlag
import org.junit.Test
import org.junit.{Ignore, Test}
import org.junit.Assert.*

import scala.concurrent.*
Expand Down Expand Up @@ -112,6 +112,16 @@ class Test15PE {
)
}

@Test
def testExtDirectRefM9() = {
compileAndRunTestAfterCommon("testdata/set15pe/m9", "cps.pe.TestExtDirectRefM9",
DotcInvocationArgs(checkAll = true,
extraDotcArgs = List(
// "-Vprint:rssh.cps", "-Vprint:erasure"
)
)
)
}


}
Expand Down
8 changes: 8 additions & 0 deletions compiler-plugin/testdata/set15pe/common/DirectRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ object directRef {
await(ref.compareAndSet(except,v))

}


extension (self: DirectRef.type) {

def make[T<:AnyRef](t:T)(using CpsDirect[PureEffect]): DirectRef[T] =
DirectRef[T](await(PERef.make(t)))

}

object DirectIntRef {

Expand Down
44 changes: 44 additions & 0 deletions compiler-plugin/testdata/set15pe/m9/TestExtDirectRefM9.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cps.pe

import org.junit.{Test,Ignore}
import org.junit.Assert.*

import scala.util.*


import scala.annotation.experimental
import scala.concurrent.*
import scala.concurrent.duration.*

import cps.*
import cps.plugin.annotation.CpsDebugLevel
import scala.concurrent.ExecutionContext.Implicits.global

@experimental
@CpsDebugLevel(20)
class TestExtDirectRefM9:

import cps.pe.directRef.*


def runExtDirectRef(): PureEffect[String] = {
implicit val printCode = cps.macros.flags.PrintCode
implicit val debugLevel = cps.macros.flags.DebugLevel(20)
async[PureEffect] {
//val logger = PEToyLogger.make()
val vs = PureEffect.directRef("AAAa")
vs.set("0")
//logger.log(vs.get)
vs.get
}
}

@Test def testExtFirectRefM9(): Unit =
val c = runExtDirectRef()
println(s"PE:extDirectRef, c=${c} ")
val future = c.unsafeRunFuture().map{ vs =>
println(s"PE:extDirectRef, vs=${vs} ")
assertTrue(vs=="0")
}
val r = Await.result(future,1.second)

21 changes: 12 additions & 9 deletions shared/src/main/scala/cps/macros/forest/ApplyTreeTransform.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cps.macros.forest

import scala.quoted.*
import scala.util.control.NonFatal
import cps.*
import cps.macros.*
import cps.macros.common.*
Expand Down Expand Up @@ -185,8 +186,9 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
case Ident(name) =>
handleArgs1(applyTerm, fun, CpsTree.pure(owner, fun), args, tails)(owner)
case _ =>
val cpsObj = runRoot(obj)(owner)
handleArgs1(applyTerm, fun, cpsObj, args, tails)(owner)
//
val cpsFun = runRoot(fun)(owner)
handleArgs1(applyTerm, fun, cpsFun, args, tails)(owner)
}


Expand Down Expand Up @@ -314,6 +316,7 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
*
*@param applyTerm = Apply(fun, args) - origin apply
*@param fun - function to apply (with type-paerameters)
*@param cpsFun - cps-sed of representation of functions
*@param args - first argument list
*@param tails - next argument lists if any
*@param unpure - if true, that this is call from shifted substitution, which is already return F[_] by design.
Expand Down Expand Up @@ -350,14 +353,12 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
&& !fun.symbol.name.contains("$")
if (callCpsDirect && !fun.symbol.flags.is(Flags.Inline)) then
val funSym = TransformUtil.extractCarriedFunSym(fun)
println(s"funSym=${funSym}")
if(!funSym.isDefinedInCurrentRun &&
!funSym.flags.is(Flags.Inline) &&
!funSym.hasAnnotation(cpsTransformedSymbol) ) then
// situation, when external function is not transformed by cps-async.
// note, that we can't determinate for function in the current compilation run, because
// we can ba called from transparent inline function which run0s before compiler-plusing phase.
println(s"!11:fun=${fun}, fun.symbol.fullName=${fun.symbol.fullName}, hashCode=${fun.symbol.hashCode} isDefinedInCurrentRun=${fun.symbol.isDefinedInCurrentRun}, flags=${fun.symbol.flags}")
throw MacroError(s"Function ${fun.symbol} is cps-=direct function compiled without dotty-cps-async-compiler plugin", applyTerm.asExpr)
if cpsCtx.flags.debugLevel >= 15 then
cpsCtx.log(s" existsShiftedLambda=${existsShiftedLambda}")
Expand Down Expand Up @@ -666,7 +667,6 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
),
List(cpsCtx.monadContext.asTerm)
)
println(s"shiftedApplyCps CPS_DEFERRED: retval=${retval.show}")
CpsTree.impure(owner, retval, applyTerm.tpe.widen)

}
Expand Down Expand Up @@ -760,7 +760,6 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
PartialShiftedApply(ApplicationShiftType.CPS_AWAIT, delayedShift)

def applyCpsDeferredShift(): PartialShiftedApply =
println(s"applyCpsDeferredShift: funTerm=${funTerm.show}")
PartialShiftedApply(ApplicationShiftType.CPS_DEFERR_TO_PLUGIN,
(unused) => Apply(funTerm, argRecords.map(_.term).toList) // TODO: we can eliminate this if we have origin term
)
Expand Down Expand Up @@ -1014,7 +1013,13 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
case _ =>
cpsFun.syncOrigin match
case Some(fun) =>
val applied = fun.appliedToArgss(argss)
val applied = try {
fun.appliedToArgss(argss)
} catch {
case ex: Throwable =>
cpsCtx.log(s"buildApply: fun=${safeShow(fun)}")
throw ex
}
if (inShiftedCallChain)
shiftedResultCpsTree(applyTerm, applied, argsProperties.cpsDirectArg)(owner)
else
Expand Down Expand Up @@ -1139,8 +1144,6 @@ trait ApplyTreeTransform[F[_],CT, CC<:CpsMonadContext[F]]:
case _ =>
TypeRepr.of[F].appliedTo(tpe.widen)

println(s"shiftedType(${tpe.show}) = ${retval.show}")

retval
}

Expand Down
14 changes: 14 additions & 0 deletions shared/src/main/scala/cps/macros/forest/RootTreeTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ trait RootTreeTransform[F[_], CT, CC <: CpsMonadContext[F] ]:
x.asInstanceOf[nestScope.qctx.reflect.Term],
args.asInstanceOf[List[nestScope.qctx.reflect.Term]],
Nil)(owner.asInstanceOf[nestScope.qctx.reflect.Symbol]).inCake(thisTransform)
case typeApply@TypeApply(tfun,targs) =>
val thisScope = this
val nestContext = cpsCtx.nestSame(muted)
val nestScope = new TreeTransformScope[F,CT,CC] {
override val cpsCtx = nestContext
override implicit val qctx = owner.asQuotes
override val fType = thisScope.fType
override val ctType = thisScope.ctType
override val ccType = thisScope.ccType
}
nestScope.runTypeApply(typeApply.asInstanceOf[nestScope.qctx.reflect.TypeApply],
tfun.asInstanceOf[nestScope.qctx.reflect.Term],
targs.asInstanceOf[List[nestScope.qctx.reflect.TypeTree]]
)(owner.asInstanceOf[nestScope.qctx.reflect.Symbol]).inCake(thisTransform)
case _ =>
throw MacroError(s"cps tree transform is not supported yet to ${term}",cpsCtx.patternCode)
}
Expand Down

0 comments on commit ee403b4

Please sign in to comment.