Skip to content

Commit 7218284

Browse files
committed
Address review: add isImplicitFunctionType, isErasedFunctionType and isDependentFunctionType
1 parent ff87cd5 commit 7218284

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,16 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
10801080
def Type_isFunctionType(self: Type)(implicit ctx: Context): Boolean =
10811081
defn.isFunctionType(self)
10821082

1083+
def Type_isImplicitFunctionType(self: Type)(implicit ctx: Context): Boolean =
1084+
defn.isImplicitFunctionType(self)
1085+
1086+
def Type_isErasedFunctionType(self: Type)(implicit ctx: Context): Boolean =
1087+
defn.isErasedFunctionType(self)
1088+
1089+
def Type_isDependentFunctionType(self: Type)(implicit ctx: Context): Boolean = {
1090+
val tpNoRefinement = self.dropDependentRefinement
1091+
tpNoRefinement != self && defn.isNonRefinedFunction(tpNoRefinement)
1092+
}
10831093

10841094
type ConstantType = Types.ConstantType
10851095

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,32 @@ trait Kernel {
869869
*
870870
* @return true if the dealised type of `self` without refinement is `FunctionN[T1, T2, ..., Tn]`
871871
*
872-
* @note `List[Int]` is not a function type, despite that `List[Int] <:< Int => Int`.
872+
* @note The function
873+
*
874+
* - returns true for `given Int => Int` and `erased Int => Int`
875+
* - returns false for `List[Int]`, despite that `List[Int] <:< Int => Int`.
873876
*/
874877
def Type_isFunctionType(self: Type)(implicit ctx: Context): Boolean
875878

879+
880+
/** Is this type an implicit function type?
881+
*
882+
* @see `Type_isFunctionType`
883+
*/
884+
def Type_isImplicitFunctionType(self: Type)(implicit ctx: Context): Boolean
885+
886+
/** Is this type an erased function type?
887+
*
888+
* @see `Type_isFunctionType`
889+
*/
890+
def Type_isErasedFunctionType(self: Type)(implicit ctx: Context): Boolean
891+
892+
/** Is this type a dependent function type?
893+
*
894+
* @see `Type_isFunctionType`
895+
*/
896+
def Type_isDependentFunctionType(self: Type)(implicit ctx: Context): Boolean
897+
876898
/** A singleton type representing a known constant value */
877899
type ConstantType <: Type
878900

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,30 @@ trait TypeOrBoundsOps extends Core {
3131
*
3232
* @return true if the dealised type of `self` without refinement is `FunctionN[T1, T2, ..., Tn]`
3333
*
34-
* @note `List[Int]` is not a function type, despite that `List[Int] <:< Int => Int`.
34+
* @note The function
35+
*
36+
* - returns true for `given Int => Int` and `erased Int => Int`
37+
* - returns false for `List[Int]`, despite that `List[Int] <:< Int => Int`.
3538
*/
3639
def isFunctionType(implicit ctx: Context): Boolean = kernel.Type_isFunctionType(self)
40+
41+
/** Is this type an implicit function type?
42+
*
43+
* @see `isFunctionType`
44+
*/
45+
def isImplicitFunctionType(implicit ctx: Context): Boolean = kernel.Type_isImplicitFunctionType(self)
46+
47+
/** Is this type an erased function type?
48+
*
49+
* @see `isFunctionType`
50+
*/
51+
def isErasedFunctionType(implicit ctx: Context): Boolean = kernel.Type_isErasedFunctionType(self)
52+
53+
/** Is this type a dependent function type?
54+
*
55+
* @see `isFunctionType`
56+
*/
57+
def isDependentFunctionType(implicit ctx: Context): Boolean = kernel.Type_isDependentFunctionType(self)
3758
}
3859

3960
object IsType {

tests/run-macros/reflect-isFunctionType/macro_1.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,27 @@ def isFunctionTypeImpl[T](tp: Type[T])(implicit refl: Reflection): Expr[Boolean]
88
import refl._
99
tp.unseal.tpe.isFunctionType.toExpr
1010
}
11+
12+
13+
inline def isImplicitFunctionType[T:Type]: Boolean = ${ isImplicitFunctionTypeImpl('[T]) }
14+
15+
def isImplicitFunctionTypeImpl[T](tp: Type[T])(implicit refl: Reflection): Expr[Boolean] = {
16+
import refl._
17+
tp.unseal.tpe.isImplicitFunctionType.toExpr
18+
}
19+
20+
21+
inline def isErasedFunctionType[T:Type]: Boolean = ${ isErasedFunctionTypeImpl('[T]) }
22+
23+
def isErasedFunctionTypeImpl[T](tp: Type[T])(implicit refl: Reflection): Expr[Boolean] = {
24+
import refl._
25+
tp.unseal.tpe.isErasedFunctionType.toExpr
26+
}
27+
28+
inline def isDependentFunctionType[T:Type]: Boolean = ${ isDependentFunctionTypeImpl('[T]) }
29+
30+
def isDependentFunctionTypeImpl[T](tp: Type[T])(implicit refl: Reflection): Expr[Boolean] = {
31+
import refl._
32+
tp.unseal.tpe.isDependentFunctionType.toExpr
33+
}
34+

tests/run-macros/reflect-isFunctionType/test_2.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,20 @@ object Test {
4545
assert(isFunctionType[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) => Int])
4646
assert(isFunctionType[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) => Int])
4747
assert(isFunctionType[(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) => Int])
48+
49+
assert(isDependentFunctionType[(b: Box) => b.T])
50+
assert(!isDependentFunctionType[Int => Int])
51+
// type A = (b: Box) => b.T
52+
// assert(isDependentFunctionType[A])
53+
54+
assert(isImplicitFunctionType[given Int => Int])
55+
assert(!isImplicitFunctionType[Int => Int])
56+
// type B = given Set[Int] => Int
57+
// assert(isImplicitFunctionType[B])
58+
59+
assert(isErasedFunctionType[erased Int => Int])
60+
assert(!isErasedFunctionType[Int => Int])
61+
// type C = erased Set[Int] => Int
62+
// assert(isErasedFunctionType[C])
4863
}
4964
}

0 commit comments

Comments
 (0)