Skip to content

Added in reflect Select.overloaded parameter for expected result type [WIP] #10054

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 13 commits into from
Oct 22, 2020
Merged
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
withDefaultPos(tpd.Select(qualifier, name.toTermName))
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term]): Apply =
withDefaultPos(tpd.applyOverloaded(qualifier, name.toTermName, args, targs, Types.WildcardType).asInstanceOf[Apply])

def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term], returnType: Type): Apply =
withDefaultPos(tpd.applyOverloaded(qualifier, name.toTermName, args, targs, returnType).asInstanceOf[Apply])
def copy(original: Tree)(qualifier: Term, name: String): Select =
tpd.cpy.Select(original)(qualifier, name.toTermName)
def unapply(x: Select): Option[(Term, String)] =
Expand Down Expand Up @@ -2049,6 +2052,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
object TypeBounds extends TypeBoundsModule:
def apply(low: Type, hi: Type): TypeBounds = Types.TypeBounds(low, hi)
def unapply(x: TypeBounds): Option[(Type, Type)] = Some((x.low, x.hi))
def empty: TypeBounds = Types .TypeBounds.empty
def upper(hi: Type): TypeBounds = Types .TypeBounds.upper(hi)
def lower(lo: Type): TypeBounds = Types .TypeBounds.lower(lo)
end TypeBounds

object TypeBoundsMethodsImpl extends TypeBoundsMethods:
Expand Down
6 changes: 6 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ trait Reflection { reflection =>
// TODO rename, this returns an Apply and not a Select
/** Call an overloaded method with the given type and term parameters */
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term]): Apply

/** Call an overloaded method with the given type and term parameters */
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term], returnType: Type): Apply

def copy(original: Tree)(qualifier: Term, name: String): Select

Expand Down Expand Up @@ -2297,6 +2300,9 @@ trait Reflection { reflection =>
trait TypeBoundsModule { this: TypeBounds.type =>
def apply(low: Type, hi: Type): TypeBounds
def unapply(x: TypeBounds): Option[(Type, Type)]
def empty: TypeBounds
def upper(hi: Type): TypeBounds
def lower(lo: Type): TypeBounds
}

given TypeBoundsMethods as TypeBoundsMethods = TypeBoundsMethodsImpl
Expand Down
4 changes: 2 additions & 2 deletions tests/neg-staging/i5941/macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Lens {
import util._
// obj.copy(field = value)
def setterBody(obj: Expr[S], value: Expr[T], field: String): Expr[S] =
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil).seal.cast[S]
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil, TypeBounds.empty).seal.cast[S]

// exception: getter.unseal.underlyingArgument
getter.unseal match {
Expand Down Expand Up @@ -51,4 +51,4 @@ object GenLens {
class MkGenLens[S] {
inline def apply[T](get: => (S => T)): Lens[S, T] = ${ Lens.impl('get) }
}
}
}
2 changes: 1 addition & 1 deletion tests/run-macros/i5941/macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,4 @@ object GenPrism {
* }(jstr => jstr)
*/
inline def apply[S, A <: S]: Prism[S, A] = ${ Prism.impl[S, A] }
}
}
2 changes: 2 additions & 0 deletions tests/run-macros/tasty-overload-secondargs.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
partialFunction
partialFunction
34 changes: 34 additions & 0 deletions tests/run-macros/tasty-overload-secondargs/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import scala.quoted._

object X:

def andThen[A,B](a:A)(f: A => B): B =
println("totalFunction")
f(a)

def andThen[A,B](a:A)(f: PartialFunction[A,B]): Option[B] =
println("partialFunction")
f.lift.apply(a)


object Macro:

inline def mThen[A,B](inline x:A=>B):B = ${
mThenImpl[A,B,A=>B,B]('x)
}

inline def mThen[A,B](inline x:PartialFunction[A,B]): Option[B] = ${
mThenImpl[A,B,PartialFunction[A,B],Option[B]]('x)
}

def mThenImpl[A:Type, B:Type, S<:(A=>B) :Type, R:Type](x:Expr[S])(using qctx: QuoteContext):Expr[R]=
import qctx.reflect._
val fun = '{X}.unseal
val returnType = quoted.Type[(S) => ?].unseal.tpe
val firstPart = Select.overloaded(fun,"andThen",
List(TypeIdent(defn.IntClass).tpe, TypeIdent(defn.IntClass).tpe),
List(Literal(Constant.Int(1))),
quoted.Type[(S) => R].unseal.tpe
)
val r = Apply(firstPart,List(x.unseal))
r.seal.cast[R]
5 changes: 5 additions & 0 deletions tests/run-macros/tasty-overload-secondargs/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test:

def main(args:Array[String]):Unit =
val x1 = X.andThen(1){case x if (x%2 == 0) => x}
val x2 = Macro.mThen{case x:Int if (x%2 == 0) => x}