Skip to content

Commit c16be38

Browse files
philwalknicolasstucki
authored andcommitted
Add ClassDef.basetypes to reflection API
1 parent 847cce2 commit c16be38

File tree

7 files changed

+30
-0
lines changed

7 files changed

+30
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
247247
optional(self.rhs.asInstanceOf[tpd.Template].self)
248248
def body: List[Statement] =
249249
self.rhs.asInstanceOf[tpd.Template].body
250+
def baseTypes: List[TypeRepr] = self.symbol match
251+
case cls: dotc.core.Symbols.ClassSymbol =>
252+
val ref = cls.classDenot.classInfo.appliedRef
253+
ref.baseClasses.map(ref.baseType(_))
254+
case _ => List()
250255
end extension
251256
end ClassDefMethods
252257

library/src/scala/quoted/Quotes.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
502502
* @syntax markdown
503503
*/
504504
def body: List[Statement]
505+
506+
/** Base types of the class */
507+
@experimental
508+
def baseTypes: List[TypeRepr]
509+
505510
end extension
506511
end ClassDefMethods
507512

project/MiMaFilters.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ object MiMaFilters {
1919
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.long$"),
2020
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
2121
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
22+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefMethods.baseTypes"),
23+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefMethods.baseTypes"),
2224

2325
// Should have been added in 3.1.0
2426
// These are only allowed on imports and therefore should not be present in binaries emitted before

scaladoc/src/dotty/tools/scaladoc/tasty/ClassLikeSupport.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ trait ClassLikeSupport:
7979
else Seq(link -> superLink) ++ getSupertypesGraph(superLink, nextTo)
8080
}
8181

82+
// TODO use classDef.baseTypes
8283
val supertypes = getSupertypes(using qctx)(classDef)
8384
.filterNot((s, t) => s.isHiddenByVisibility)
8485
.map {

tests/run-macros/baseTypes.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
List(scala.Int, scala.AnyVal, scala.Matchable, scala.Any)
2+
List(java.lang.Object, scala.Matchable, scala.Any)
3+
List(scala.Some[Some.this.A], scala.Option[Some.this.A], java.io.Serializable, scala.Product, scala.Equals, scala.collection.IterableOnce[Some.this.A], java.lang.Object, scala.Matchable, scala.Any)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted.*
2+
3+
inline def baseTypes[T]: List[String] =
4+
${ baseTypesExpr[T] }
5+
6+
private def baseTypesExpr[T: Type](using Quotes): Expr[List[String]] =
7+
import quotes.reflect.*
8+
TypeRepr.of[T].typeSymbol.tree match
9+
case cdef: ClassDef => Expr(cdef.baseTypes.map(_.show))
10+
case _ => Expr(Nil)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@main def Test: Unit =
2+
println(baseTypes[Int])
3+
println(baseTypes[Object])
4+
println(baseTypes[Some[Int]])

0 commit comments

Comments
 (0)