Skip to content

Add ClassDef.baseTypes to reflection API #13398

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
optional(self.rhs.asInstanceOf[tpd.Template].self)
def body: List[Statement] =
self.rhs.asInstanceOf[tpd.Template].body
def baseTypes: List[TypeRepr] = self.symbol match
case cls: dotc.core.Symbols.ClassSymbol =>
val ref = cls.classDenot.classInfo.appliedRef
ref.baseClasses.map(ref.baseType(_))
case _ => List()
end extension
end ClassDefMethods

Expand Down
5 changes: 5 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* @syntax markdown
*/
def body: List[Statement]

/** Base types of the class */
@experimental
def baseTypes: List[TypeRepr]

end extension
end ClassDefMethods

Expand Down
2 changes: 2 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ object MiMaFilters {
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.long$"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefMethods.baseTypes"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefMethods.baseTypes"),

// Should have been added in 3.1.0
// These are only allowed on imports and therefore should not be present in binaries emitted before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ trait ClassLikeSupport:
else Seq(link -> superLink) ++ getSupertypesGraph(superLink, nextTo)
}

// TODO use classDef.baseTypes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pikinier20 SInce you created this PR the code below changed. Is this method still relevant or is Symbol.baseCalsses enough?

val supertypes = getSupertypes(using qctx)(classDef)
.filterNot((s, t) => s.isHiddenByVisibility)
.map {
Expand Down
3 changes: 3 additions & 0 deletions tests/run-macros/baseTypes.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
List(scala.Int, scala.AnyVal, scala.Matchable, scala.Any)
List(java.lang.Object, scala.Matchable, scala.Any)
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this test I noticed that baseTypes is losing the type parameters. It might need to be implemented on TypeRepr directly.

10 changes: 10 additions & 0 deletions tests/run-macros/baseTypes/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import scala.quoted.*

inline def baseTypes[T]: List[String] =
${ baseTypesExpr[T] }

private def baseTypesExpr[T: Type](using Quotes): Expr[List[String]] =
import quotes.reflect.*
TypeRepr.of[T].typeSymbol.tree match
case cdef: ClassDef => Expr(cdef.baseTypes.map(_.show))
case _ => Expr(Nil)
4 changes: 4 additions & 0 deletions tests/run-macros/baseTypes/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@main def Test: Unit =
println(baseTypes[Int])
println(baseTypes[Object])
println(baseTypes[Some[Int]])