Skip to content
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

Remove implicits from tasty reflection #8142

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
def Closure_copy(original: Tree)(meth: Tree, tpe: Option[Type])(given Context): Closure =
tpd.cpy.Closure(original)(Nil, meth, tpe.map(tpd.TypeTree(_)).getOrElse(tpd.EmptyTree))

def Lambda_apply(tpe: MethodType, rhsFn: List[Tree] => Tree)(implicit ctx: Context): Block =
def Lambda_apply(tpe: MethodType, rhsFn: List[Tree] => Tree)(given ctx: Context): Block =
tpd.Lambda(tpe, rhsFn)

type If = tpd.If
Expand Down Expand Up @@ -1204,7 +1204,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
case tpe: Types.ConstantType => Some(tpe)
case _ => None
}

def ConstantType_apply(const: Constant)(given Context): ConstantType =
Types.ConstantType(const)

Expand Down
6 changes: 4 additions & 2 deletions library/src/scala/internal/quoted/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ private[quoted] object Matcher {
def termMatch(scrutineeTerm: Term, patternTerm: Term, hasTypeSplices: Boolean): Option[Tuple] = {
implicit val env: Env = Map.empty
if (hasTypeSplices) {
implicit val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
given Context = ctx
val matchings = scrutineeTerm.underlyingArgument =?= patternTerm.underlyingArgument
// After matching and doing all subtype checks, we have to aproximate all the type bindings
// that we have found and seal them in a quoted.Type
Expand All @@ -52,7 +53,8 @@ private[quoted] object Matcher {
def typeTreeMatch(scrutineeTypeTree: TypeTree, patternTypeTree: TypeTree, hasTypeSplices: Boolean): Option[Tuple] = {
implicit val env: Env = Map.empty
if (hasTypeSplices) {
implicit val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
given Context = ctx
val matchings = scrutineeTypeTree =?= patternTypeTree
// After matching and doing all subtype checks, we have to aproximate all the type bindings
// that we have found and seal them in a quoted.Type
Expand Down
35 changes: 21 additions & 14 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -419,29 +419,29 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
// QUOTES //
////////////////

implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
given QuotedExprOps: extension (expr: scala.quoted.Expr[?]) {
nicolasstucki marked this conversation as resolved.
Show resolved Hide resolved
/** View this expression `quoted.Expr[T]` as a `Term` */
def unseal(given ctx: Context): Term =
internal.QuotedExpr_unseal(expr)

/** Checked cast to a `quoted.Expr[U]` */
def cast[U: scala.quoted.Type](given ctx: Context): scala.quoted.Expr[U] =
def cast[U](given tp: scala.quoted.Type[U], ctx: Context): scala.quoted.Expr[U] =
internal.QuotedExpr_cast[U](expr)
}

implicit class QuotedTypeAPI[T <: AnyKind](tpe: scala.quoted.Type[T]) {
given QuotedTypeAPI: extension [T <: AnyKind](tpe: scala.quoted.Type[T]) {
/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(given ctx: Context): TypeTree =
internal.QuotedType_unseal(tpe)
}

implicit class TermToQuotedAPI(term: Term) {
given TermToQuotedOps: extension (term: Term) {
/** Convert `Term` to an `quoted.Expr[Any]` */
def seal(given ctx: Context): scala.quoted.Expr[Any] =
internal.QuotedExpr_seal(term)
}

implicit class TypeToQuotedAPI(tpe: Type) {
given TypeToQuotedOps: extension (tpe: Type) {
/** Convert `Type` to an `quoted.Type[_]` */
def seal(given ctx: Context): scala.quoted.Type[_] =
internal.QuotedType_seal(tpe)
Expand All @@ -452,7 +452,7 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
//////////////

/** Context of the macro expansion */
implicit def rootContext: Context = internal.rootContext // TODO: Use given // TODO: Should this be moved to QuoteContext?
given rootContext: Context = internal.rootContext // TODO: Use given // TODO: Should this be moved to QuoteContext?

given ContextOps: extension (self: Context) {
/** Returns the owner of the context */
Expand Down Expand Up @@ -1009,7 +1009,7 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
case _ => None
}

def apply(tpe: MethodType, rhsFn: List[Tree] => Tree)(implicit ctx: Context): Block =
def apply(tpe: MethodType, rhsFn: List[Tree] => Tree)(given ctx: Context): Block =
internal.Lambda_apply(tpe, rhsFn)

}
Expand Down Expand Up @@ -2785,16 +2785,20 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
case Inlined(call, bindings, expansion) =>
foldTree(foldTrees(x, bindings), expansion)
case vdef @ ValDef(_, tpt, rhs) =>
implicit val ctx = localCtx(vdef)
val ctx = localCtx(vdef)
given Context = ctx
foldTrees(foldTree(x, tpt), rhs)
case ddef @ DefDef(_, tparams, vparamss, tpt, rhs) =>
implicit val ctx = localCtx(ddef)
val ctx = localCtx(ddef)
given Context = ctx
foldTrees(foldTree(vparamss.foldLeft(foldTrees(x, tparams))(foldTrees), tpt), rhs)
case tdef @ TypeDef(_, rhs) =>
implicit val ctx = localCtx(tdef)
val ctx = localCtx(tdef)
given Context = ctx
foldTree(x, rhs)
case cdef @ ClassDef(_, constr, parents, derived, self, body) =>
implicit val ctx = localCtx(cdef)
val ctx = localCtx(cdef)
given Context = ctx
foldTrees(foldTrees(foldTrees(foldTrees(foldTree(x, constr), parents), derived), self), body)
case Import(expr, _) =>
foldTree(x, expr)
Expand Down Expand Up @@ -2867,15 +2871,18 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
case tree: Term =>
transformTerm(tree)
case tree: ValDef =>
implicit val ctx = localCtx(tree)
val ctx = localCtx(tree)
given Context = ctx
val tpt1 = transformTypeTree(tree.tpt)
val rhs1 = tree.rhs.map(x => transformTerm(x))
ValDef.copy(tree)(tree.name, tpt1, rhs1)
case tree: DefDef =>
implicit val ctx = localCtx(tree)
val ctx = localCtx(tree)
given Context = ctx
DefDef.copy(tree)(tree.name, transformSubTrees(tree.typeParams), tree.paramss mapConserve (transformSubTrees(_)), transformTypeTree(tree.returnTpt), tree.rhs.map(x => transformTerm(x)))
case tree: TypeDef =>
implicit val ctx = localCtx(tree)
val ctx = localCtx(tree)
given Context = ctx
TypeDef.copy(tree)(tree.name, transformTree(tree.rhs))
case tree: ClassDef =>
ClassDef.copy(tree)(tree.name, tree.constructor, tree.parents, tree.derived, tree.self, tree.body)
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ trait CompilerInterface {
def Closure_apply(meth: Term, tpe: Option[Type])(given ctx: Context): Closure
def Closure_copy(original: Tree)(meth: Tree, tpe: Option[Type])(given ctx: Context): Closure

def Lambda_apply(tpe: MethodType, rhsFn: List[Tree] => Tree)(implicit ctx: Context): Block
def Lambda_apply(tpe: MethodType, rhsFn: List[Tree] => Tree)(given ctx: Context): Block

/** Tree representing an if/then/else `if (...) ... else ...` in the source code */
type If <: Term
Expand Down Expand Up @@ -1043,7 +1043,7 @@ trait CompilerInterface {

def isInstanceOfPolyType(given ctx: Context): IsInstanceOf[PolyType]

def PolyType_apply(paramNames: List[String])(paramBoundsExp: PolyType => List[TypeBounds], resultTypeExp: PolyType => Type)(given ctx: Context): PolyType
def PolyType_apply(paramNames: List[String])(paramBoundsExp: PolyType => List[TypeBounds], resultTypeExp: PolyType => Type)(given ctx: Context): PolyType

def PolyType_param(self: PolyType, idx: Int)(given ctx: Context): Type
def PolyType_paramNames(self: PolyType)(given ctx: Context): List[String]
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/macros-in-same-project-6/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Foo {
inline def myMacro(): Unit = ${ aMacroImplementation }

def aMacroImplementation with (qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
import qctx.tasty.{given, _}
error("some error", rootPosition)
throw new NoClassDefFoundError("Bar$")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/quote-matcher-runtime/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Macros {
inline def matches[A, B](inline a: A, inline b: B): Unit = ${impl('a, 'b)}

private def impl[A, B](a: Expr[A], b: Expr[B]) with (qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty.{Bind => _, _}
import qctx.tasty.{Bind => _, given, _}

val res = scala.internal.quoted.Expr.unapply[Tuple, Tuple](a)(b, true, qctx).map { tup =>
tup.toArray.toList.map {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/quote-type-matcher/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Macros {
inline def matches[A, B]: Unit = ${ matchesExpr('[A], '[B]) }

private def matchesExpr[A, B](a: Type[A], b: Type[B]) with (qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty.{Bind => _, _}
import qctx.tasty.{Bind => _, given, _}

val res = scala.internal.quoted.Type.unapply[Tuple, Tuple](a)(b, true, qctx).map { tup =>
tup.toArray.toList.map {
Expand Down