Skip to content

Commit ad4a92b

Browse files
Merge pull request #9481 from pshirshov/wip/fix-8514-expose-baseType
fixes #8514, TypeApi#baseType exposed into Tasty
2 parents bb23fea + 24a7940 commit ad4a92b

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
12051205
def Type_baseClasses(self: Type)(using Context): List[Symbol] =
12061206
self.baseClasses
12071207

1208+
def Type_baseType(self: Type)(cls: Symbol)(using Context): Type =
1209+
self.baseType(cls)
1210+
12081211
def Type_derivesFrom(self: Type)(cls: Symbol)(using Context): Boolean =
12091212
self.derivesFrom(cls)
12101213

library/src/scala/tasty/Reflection.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,17 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
18121812
/** The base classes of this type with the class itself as first element. */
18131813
def baseClasses(using ctx: Context): List[Symbol] = internal.Type_baseClasses(self)
18141814

1815+
1816+
/** The least type instance of given class which is a super-type
1817+
* of this type. Example:
1818+
* {{{
1819+
* class D[T]
1820+
* class C extends p.D[Int]
1821+
* ThisType(C).baseType(D) = p.D[Int]
1822+
* }}}
1823+
*/
1824+
def baseType(cls: Symbol)(using ctx: Context): Type = internal.Type_baseType(self)(cls)
1825+
18151826
/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
18161827
def derivesFrom(cls: Symbol)(using ctx: Context): Boolean =
18171828
internal.Type_derivesFrom(self)(cls)

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,16 @@ trait CompilerInterface {
876876
/** The base classes of this type with the class itself as first element. */
877877
def Type_baseClasses(self: Type)(using ctx: Context): List[Symbol]
878878

879+
/** The least type instance of given class which is a super-type
880+
* of this type. Example:
881+
* {{{
882+
* class D[T]
883+
* class C extends p.D[Int]
884+
* ThisType(C).baseType(D) = p.D[Int]
885+
* }}}
886+
*/
887+
def Type_baseType(self: Type)(cls: Symbol)(using ctx: Context): Type
888+
879889
/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
880890
def Type_derivesFrom(self: Type)(cls: Symbol)(using ctx: Context): Boolean
881891

tests/run-macros/i8514b.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
B
2+
A[[T >: scala.Nothing <: scala.Any] => P[T], scala.Predef.String]
3+
java.lang.Object
4+
scala.Any

tests/run-macros/i8514b/Macro_1.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.quoted._
2+
3+
class A[+X[_], -Y]
4+
class P[T]
5+
class B extends A[P, String]
6+
7+
inline def test(): Unit = ${ testExpr }
8+
9+
def testExpr(using QuoteContext): Expr[Unit] = {
10+
import qctx.tasty._
11+
12+
val t = '[B].unseal.tpe
13+
val baseTypes = t.baseClasses.map(b => t.baseType(b))
14+
15+
'{
16+
println(${Expr(baseTypes.map(_.show).mkString("\n"))})
17+
}
18+
}

tests/run-macros/i8514b/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test = test()

0 commit comments

Comments
 (0)