Skip to content

Commit b1c9a28

Browse files
Merge pull request #8142 from dotty-staging/remove-implicits-from-tasty-reflection
Remove implicits from tasty reflection
2 parents 6ae1c92 + a04c9c4 commit b1c9a28

File tree

8 files changed

+33
-24
lines changed

8 files changed

+33
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
592592
def Closure_copy(original: Tree)(meth: Tree, tpe: Option[Type])(given Context): Closure =
593593
tpd.cpy.Closure(original)(Nil, meth, tpe.map(tpd.TypeTree(_)).getOrElse(tpd.EmptyTree))
594594

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

598598
type If = tpd.If
@@ -1204,7 +1204,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
12041204
case tpe: Types.ConstantType => Some(tpe)
12051205
case _ => None
12061206
}
1207-
1207+
12081208
def ConstantType_apply(const: Constant)(given Context): ConstantType =
12091209
Types.ConstantType(const)
12101210

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ private[quoted] object Matcher {
3232
def termMatch(scrutineeTerm: Term, patternTerm: Term, hasTypeSplices: Boolean): Option[Tuple] = {
3333
implicit val env: Env = Map.empty
3434
if (hasTypeSplices) {
35-
implicit val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
35+
val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
36+
given Context = ctx
3637
val matchings = scrutineeTerm.underlyingArgument =?= patternTerm.underlyingArgument
3738
// After matching and doing all subtype checks, we have to aproximate all the type bindings
3839
// that we have found and seal them in a quoted.Type
@@ -52,7 +53,8 @@ private[quoted] object Matcher {
5253
def typeTreeMatch(scrutineeTypeTree: TypeTree, patternTypeTree: TypeTree, hasTypeSplices: Boolean): Option[Tuple] = {
5354
implicit val env: Env = Map.empty
5455
if (hasTypeSplices) {
55-
implicit val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
56+
val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
57+
given Context = ctx
5658
val matchings = scrutineeTypeTree =?= patternTypeTree
5759
// After matching and doing all subtype checks, we have to aproximate all the type bindings
5860
// that we have found and seal them in a quoted.Type

library/src/scala/tasty/Reflection.scala

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -419,29 +419,29 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
419419
// QUOTES //
420420
////////////////
421421

422-
implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
422+
given QuotedExprOps: extension (expr: scala.quoted.Expr[?]) {
423423
/** View this expression `quoted.Expr[T]` as a `Term` */
424424
def unseal(given ctx: Context): Term =
425425
internal.QuotedExpr_unseal(expr)
426426

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

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

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

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

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

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

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

10151015
}
@@ -2785,16 +2785,20 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
27852785
case Inlined(call, bindings, expansion) =>
27862786
foldTree(foldTrees(x, bindings), expansion)
27872787
case vdef @ ValDef(_, tpt, rhs) =>
2788-
implicit val ctx = localCtx(vdef)
2788+
val ctx = localCtx(vdef)
2789+
given Context = ctx
27892790
foldTrees(foldTree(x, tpt), rhs)
27902791
case ddef @ DefDef(_, tparams, vparamss, tpt, rhs) =>
2791-
implicit val ctx = localCtx(ddef)
2792+
val ctx = localCtx(ddef)
2793+
given Context = ctx
27922794
foldTrees(foldTree(vparamss.foldLeft(foldTrees(x, tparams))(foldTrees), tpt), rhs)
27932795
case tdef @ TypeDef(_, rhs) =>
2794-
implicit val ctx = localCtx(tdef)
2796+
val ctx = localCtx(tdef)
2797+
given Context = ctx
27952798
foldTree(x, rhs)
27962799
case cdef @ ClassDef(_, constr, parents, derived, self, body) =>
2797-
implicit val ctx = localCtx(cdef)
2800+
val ctx = localCtx(cdef)
2801+
given Context = ctx
27982802
foldTrees(foldTrees(foldTrees(foldTrees(foldTree(x, constr), parents), derived), self), body)
27992803
case Import(expr, _) =>
28002804
foldTree(x, expr)
@@ -2867,15 +2871,18 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
28672871
case tree: Term =>
28682872
transformTerm(tree)
28692873
case tree: ValDef =>
2870-
implicit val ctx = localCtx(tree)
2874+
val ctx = localCtx(tree)
2875+
given Context = ctx
28712876
val tpt1 = transformTypeTree(tree.tpt)
28722877
val rhs1 = tree.rhs.map(x => transformTerm(x))
28732878
ValDef.copy(tree)(tree.name, tpt1, rhs1)
28742879
case tree: DefDef =>
2875-
implicit val ctx = localCtx(tree)
2880+
val ctx = localCtx(tree)
2881+
given Context = ctx
28762882
DefDef.copy(tree)(tree.name, transformSubTrees(tree.typeParams), tree.paramss mapConserve (transformSubTrees(_)), transformTypeTree(tree.returnTpt), tree.rhs.map(x => transformTerm(x)))
28772883
case tree: TypeDef =>
2878-
implicit val ctx = localCtx(tree)
2884+
val ctx = localCtx(tree)
2885+
given Context = ctx
28792886
TypeDef.copy(tree)(tree.name, transformTree(tree.rhs))
28802887
case tree: ClassDef =>
28812888
ClassDef.copy(tree)(tree.name, tree.constructor, tree.parents, tree.derived, tree.self, tree.body)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ trait CompilerInterface {
455455
def Closure_apply(meth: Term, tpe: Option[Type])(given ctx: Context): Closure
456456
def Closure_copy(original: Tree)(meth: Tree, tpe: Option[Type])(given ctx: Context): Closure
457457

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

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

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

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

10481048
def PolyType_param(self: PolyType, idx: Int)(given ctx: Context): Type
10491049
def PolyType_paramNames(self: PolyType)(given ctx: Context): List[String]

tests/neg-macros/macros-in-same-project-6/Foo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Foo {
55
inline def myMacro(): Unit = ${ aMacroImplementation }
66

77
def aMacroImplementation with (qctx: QuoteContext) : Expr[Unit] = {
8-
import qctx.tasty._
8+
import qctx.tasty.{given, _}
99
error("some error", rootPosition)
1010
throw new NoClassDefFoundError("Bar$")
1111
}

tests/run-macros/quote-matcher-runtime/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macros {
77
inline def matches[A, B](inline a: A, inline b: B): Unit = ${impl('a, 'b)}
88

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

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

tests/run-macros/quote-type-matcher/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macros {
77
inline def matches[A, B]: Unit = ${ matchesExpr('[A], '[B]) }
88

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

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

0 commit comments

Comments
 (0)