Skip to content

Commit 2720af2

Browse files
Merge pull request #10054 from rssh/overloaded-with-retry-path
Added in reflect Select.overloaded parameter for expected result type [WIP]
2 parents 97da3cb + cbde5bd commit 2720af2

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

+6
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
414414
withDefaultPos(tpd.Select(qualifier, name.toTermName))
415415
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term]): Apply =
416416
withDefaultPos(tpd.applyOverloaded(qualifier, name.toTermName, args, targs, Types.WildcardType).asInstanceOf[Apply])
417+
418+
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term], returnType: Type): Apply =
419+
withDefaultPos(tpd.applyOverloaded(qualifier, name.toTermName, args, targs, returnType).asInstanceOf[Apply])
417420
def copy(original: Tree)(qualifier: Term, name: String): Select =
418421
tpd.cpy.Select(original)(qualifier, name.toTermName)
419422
def unapply(x: Select): Option[(Term, String)] =
@@ -2049,6 +2052,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
20492052
object TypeBounds extends TypeBoundsModule:
20502053
def apply(low: Type, hi: Type): TypeBounds = Types.TypeBounds(low, hi)
20512054
def unapply(x: TypeBounds): Option[(Type, Type)] = Some((x.low, x.hi))
2055+
def empty: TypeBounds = Types .TypeBounds.empty
2056+
def upper(hi: Type): TypeBounds = Types .TypeBounds.upper(hi)
2057+
def lower(lo: Type): TypeBounds = Types .TypeBounds.lower(lo)
20522058
end TypeBounds
20532059

20542060
object TypeBoundsMethodsImpl extends TypeBoundsMethods:

library/src/scala/tasty/Reflection.scala

+6
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ trait Reflection { reflection =>
515515
// TODO rename, this returns an Apply and not a Select
516516
/** Call an overloaded method with the given type and term parameters */
517517
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term]): Apply
518+
519+
/** Call an overloaded method with the given type and term parameters */
520+
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term], returnType: Type): Apply
518521

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

@@ -2297,6 +2300,9 @@ trait Reflection { reflection =>
22972300
trait TypeBoundsModule { this: TypeBounds.type =>
22982301
def apply(low: Type, hi: Type): TypeBounds
22992302
def unapply(x: TypeBounds): Option[(Type, Type)]
2303+
def empty: TypeBounds
2304+
def upper(hi: Type): TypeBounds
2305+
def lower(lo: Type): TypeBounds
23002306
}
23012307

23022308
given TypeBoundsMethods as TypeBoundsMethods = TypeBoundsMethodsImpl

tests/neg-staging/i5941/macro_1.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Lens {
1717
import util._
1818
// obj.copy(field = value)
1919
def setterBody(obj: Expr[S], value: Expr[T], field: String): Expr[S] =
20-
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil).seal.cast[S]
20+
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil, TypeBounds.empty).seal.cast[S]
2121

2222
// exception: getter.unseal.underlyingArgument
2323
getter.unseal match {
@@ -51,4 +51,4 @@ object GenLens {
5151
class MkGenLens[S] {
5252
inline def apply[T](get: => (S => T)): Lens[S, T] = ${ Lens.impl('get) }
5353
}
54-
}
54+
}

tests/run-macros/i5941/macro_1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@ object GenPrism {
217217
* }(jstr => jstr)
218218
*/
219219
inline def apply[S, A <: S]: Prism[S, A] = ${ Prism.impl[S, A] }
220-
}
220+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
partialFunction
2+
partialFunction
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.quoted._
2+
3+
object X:
4+
5+
def andThen[A,B](a:A)(f: A => B): B =
6+
println("totalFunction")
7+
f(a)
8+
9+
def andThen[A,B](a:A)(f: PartialFunction[A,B]): Option[B] =
10+
println("partialFunction")
11+
f.lift.apply(a)
12+
13+
14+
object Macro:
15+
16+
inline def mThen[A,B](inline x:A=>B):B = ${
17+
mThenImpl[A,B,A=>B,B]('x)
18+
}
19+
20+
inline def mThen[A,B](inline x:PartialFunction[A,B]): Option[B] = ${
21+
mThenImpl[A,B,PartialFunction[A,B],Option[B]]('x)
22+
}
23+
24+
def mThenImpl[A:Type, B:Type, S<:(A=>B) :Type, R:Type](x:Expr[S])(using qctx: QuoteContext):Expr[R]=
25+
import qctx.reflect._
26+
val fun = '{X}.unseal
27+
val returnType = quoted.Type[(S) => ?].unseal.tpe
28+
val firstPart = Select.overloaded(fun,"andThen",
29+
List(TypeIdent(defn.IntClass).tpe, TypeIdent(defn.IntClass).tpe),
30+
List(Literal(Constant.Int(1))),
31+
quoted.Type[(S) => R].unseal.tpe
32+
)
33+
val r = Apply(firstPart,List(x.unseal))
34+
r.seal.cast[R]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test:
2+
3+
def main(args:Array[String]):Unit =
4+
val x1 = X.andThen(1){case x if (x%2 == 0) => x}
5+
val x2 = Macro.mThen{case x:Int if (x%2 == 0) => x}

0 commit comments

Comments
 (0)