Skip to content

fixes #8514, TypeApi#baseType exposed into Tasty #9481

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 3 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
def Type_baseClasses(self: Type)(using Context): List[Symbol] =
self.baseClasses

def Type_baseType(self: Type)(cls: Symbol)(using Context): Type =
self.baseType(cls)

def Type_derivesFrom(self: Type)(cls: Symbol)(using Context): Boolean =
self.derivesFrom(cls)

Expand Down
11 changes: 11 additions & 0 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,17 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
/** The base classes of this type with the class itself as first element. */
def baseClasses(using ctx: Context): List[Symbol] = internal.Type_baseClasses(self)


/** The least type instance of given class which is a super-type
* of this type. Example:
* {{{
* class D[T]
* class C extends p.D[Int]
* ThisType(C).baseType(D) = p.D[Int]
* }}}
*/
def baseType(cls: Symbol)(using ctx: Context): Type = internal.Type_baseType(self)(cls)

/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
def derivesFrom(cls: Symbol)(using ctx: Context): Boolean =
internal.Type_derivesFrom(self)(cls)
Expand Down
10 changes: 10 additions & 0 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,16 @@ trait CompilerInterface {
/** The base classes of this type with the class itself as first element. */
def Type_baseClasses(self: Type)(using ctx: Context): List[Symbol]

/** The least type instance of given class which is a super-type
* of this type. Example:
* {{{
* class D[T]
* class C extends p.D[Int]
* ThisType(C).baseType(D) = p.D[Int]
* }}}
*/
def Type_baseType(self: Type)(cls: Symbol)(using ctx: Context): Type

/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
def Type_derivesFrom(self: Type)(cls: Symbol)(using ctx: Context): Boolean

Expand Down
1 change: 1 addition & 0 deletions tests/run-macros/i8514b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),B), AppliedType(TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class A),List(HKTypeLambda(List(T), List(TypeBounds(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),module scala),Nothing),TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),module scala),Any))), AppliedType(TypeRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,module class <root>)),module class <empty>)),P),List(TypeParamRef(T)))), TypeRef(TermRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),module scala),Predef),String))), TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class Object), TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Any))
16 changes: 16 additions & 0 deletions tests/run-macros/i8514b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.quoted._

class A[+X[_], -Y]
class P[T]
class B extends A[P, String]

inline def test(): Unit = ${ testExpr }

def testExpr(using QuoteContext): Expr[Unit] = {
import qctx.tasty._

val t = '[B].unseal.tpe
'{
println(${Expr(t.baseClasses.map(b => t.baseType(b).toString))})
}
}
1 change: 1 addition & 0 deletions tests/run-macros/i8514b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test = test()