Skip to content

Commit 1f96b09

Browse files
committed
Remove old owner context from reflection API
1 parent d348877 commit 1f96b09

File tree

9 files changed

+36
-53
lines changed

9 files changed

+36
-53
lines changed

compiler/src/scala/quoted/internal/impl/Matcher.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ object Matcher {
137137

138138
extension (scrutinees: List[Tree]):
139139
/** Check that all trees match with =?= and concatenate the results with &&& */
140-
private def =?= (patterns: List[Tree])(using Context, Env): Matching =
140+
private def =?= (patterns: List[Tree])(using Env): Matching =
141141
matchLists(scrutinees, patterns)(_ =?= _)
142142

143143
extension (scrutinee0: Tree):
@@ -149,7 +149,7 @@ object Matcher {
149149
* @param `summon[Env]` Set of tuples containing pairs of symbols (s, p) where s defines a symbol in `scrutinee` which corresponds to symbol p in `pattern`.
150150
* @return `None` if it did not match or `Some(tup: Tuple)` if it matched where `tup` contains the contents of the holes.
151151
*/
152-
private def =?= (pattern0: Tree)(using Context, Env): Matching = {
152+
private def =?= (pattern0: Tree)(using Env): Matching = {
153153

154154
/* Match block flattening */ // TODO move to cases
155155
/** Normalize the tree */
@@ -299,7 +299,7 @@ object Matcher {
299299
/* Match val */
300300
case (ValDef(_, tpt1, rhs1), ValDef(_, tpt2, rhs2)) if checkValFlags() =>
301301
def rhsEnv = summon[Env] + (scrutinee.symbol -> pattern.symbol)
302-
tpt1 =?= tpt2 &&& treeOptMatches(rhs1, rhs2)(using summon[Context], rhsEnv)
302+
tpt1 =?= tpt2 &&& treeOptMatches(rhs1, rhs2)(using rhsEnv)
303303

304304
/* Match def */
305305
case (DefDef(_, typeParams1, paramss1, tpt1, Some(rhs1)), DefDef(_, typeParams2, paramss2, tpt2, Some(rhs2))) =>
@@ -348,11 +348,11 @@ object Matcher {
348348

349349
private object ClosedPatternTerm {
350350
/** Matches a term that does not contain free variables defined in the pattern (i.e. not defined in `Env`) */
351-
def unapply(term: Term)(using Context, Env): Option[term.type] =
351+
def unapply(term: Term)(using Env): Option[term.type] =
352352
if freePatternVars(term).isEmpty then Some(term) else None
353353

354354
/** Return all free variables of the term defined in the pattern (i.e. defined in `Env`) */
355-
def freePatternVars(term: Term)(using ctx: Context, env: Env): Set[Symbol] =
355+
def freePatternVars(term: Term)(using env: Env): Set[Symbol] =
356356
val accumulator = new TreeAccumulator[Set[Symbol]] {
357357
def foldTree(x: Set[Symbol], tree: Tree)(owner: Symbol): Set[Symbol] =
358358
tree match
@@ -363,7 +363,7 @@ object Matcher {
363363
}
364364

365365
private object IdentArgs {
366-
def unapply(args: List[Term])(using Context): Option[List[Ident]] =
366+
def unapply(args: List[Term]): Option[List[Ident]] =
367367
args.foldRight(Option(List.empty[Ident])) {
368368
case (id: Ident, Some(acc)) => Some(id :: acc)
369369
case (Block(List(DefDef("$anonfun", Nil, List(params), Inferred(), Some(Apply(id: Ident, args)))), Closure(Ident("$anonfun"), None)), Some(acc))
@@ -373,7 +373,7 @@ object Matcher {
373373
}
374374
}
375375

376-
private def treeOptMatches(scrutinee: Option[Tree], pattern: Option[Tree])(using Context, Env): Matching = {
376+
private def treeOptMatches(scrutinee: Option[Tree], pattern: Option[Tree])(using Env): Matching = {
377377
(scrutinee, pattern) match {
378378
case (Some(x), Some(y)) => x =?= y
379379
case (None, None) => matched

compiler/src/scala/quoted/internal/impl/QuoteContextImpl.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ object QuoteContextImpl {
2626
type ScopeId = Int
2727

2828
def apply()(using Context): QuoteContext =
29-
new QuoteContextImpl(ctx)
29+
new QuoteContextImpl
3030

3131
def showDecompiledTree(tree: tpd.Tree)(using Context): String = {
32-
val qctx: QuoteContextImpl = new QuoteContextImpl(MacroExpansion.context(tree))
32+
val qctx: QuoteContextImpl = new QuoteContextImpl(using MacroExpansion.context(tree))
3333
if ctx.settings.color.value == "always" then
3434
qctx.reflect.TreeMethodsImpl.temporaryShowAnsiColored(tree)
3535
else
@@ -43,7 +43,7 @@ object QuoteContextImpl {
4343

4444
}
4545

46-
class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickler, QuoteMatching:
46+
class QuoteContextImpl private (using val ctx: Context) extends QuoteContext, QuoteUnpickler, QuoteMatching:
4747

4848
private val yCheck: Boolean =
4949
ctx.settings.Ycheck.value(using ctx).exists(x => x == "all" || x == "macros")
@@ -81,10 +81,6 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
8181

8282
object reflect extends Reflection:
8383

84-
def rootContext: Context = ctx
85-
86-
type Context = dotc.core.Contexts.Context
87-
8884
type Tree = tpd.Tree
8985

9086
object Tree extends TreeModule:
@@ -2483,7 +2479,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
24832479

24842480
object Position extends PositionModule:
24852481
def ofMacroExpansion: dotc.util.SourcePosition =
2486-
MacroExpansion.position.getOrElse(dotc.util.SourcePosition(rootContext.source, dotc.util.Spans.NoSpan))
2482+
MacroExpansion.position.getOrElse(dotc.util.SourcePosition(ctx.source, dotc.util.Spans.NoSpan))
24872483
end Position
24882484

24892485
object PositionMethodsImpl extends PositionMethods:
@@ -2599,11 +2595,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
25992595
end reflect
26002596

26012597
def unpickleExpr[T](pickled: String | List[String], typeHole: (Int, Seq[Any]) => scala.quoted.Type[?], termHole: (Int, Seq[Any], scala.quoted.QuoteContext) => scala.quoted.Expr[?]): scala.quoted.Expr[T] =
2602-
val tree = PickledQuotes.unpickleTerm(pickled, typeHole, termHole)(using reflect.rootContext)
2598+
val tree = PickledQuotes.unpickleTerm(pickled, typeHole, termHole)
26032599
new ExprImpl(tree, hash).asInstanceOf[scala.quoted.Expr[T]]
26042600

26052601
def unpickleType[T <: AnyKind](pickled: String | List[String], typeHole: (Int, Seq[Any]) => scala.quoted.Type[?], termHole: (Int, Seq[Any], scala.quoted.QuoteContext) => scala.quoted.Expr[?]): scala.quoted.Type[T] =
2606-
val tree = PickledQuotes.unpickleTypeTree(pickled, typeHole, termHole)(using reflect.rootContext)
2602+
val tree = PickledQuotes.unpickleTypeTree(pickled, typeHole, termHole)
26072603
new TypeImpl(tree, hash).asInstanceOf[scala.quoted.Type[T]]
26082604

26092605
object ExprMatch extends ExprMatchModule:
@@ -2622,7 +2618,6 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
26222618

26232619
private def treeMatch(scrutinee: reflect.Tree, pattern: reflect.Tree): Option[Tuple] = {
26242620
import reflect._
2625-
given Context = rootContext
26262621
def isTypeHoleDef(tree: Tree): Boolean =
26272622
tree match
26282623
case tree: TypeDef =>

library/src/scala/quoted/QuoteContext.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,6 @@ trait QuoteContext { self: internal.QuoteUnpickler & internal.QuoteMatching =>
170170
*/
171171
trait Reflection { self: reflect.type =>
172172

173-
//////////////
174-
// CONTEXTS //
175-
//////////////
176-
177-
/** Context containing information on the current owner */
178-
type Context <: AnyRef
179-
180-
/** Context of the macro expansion */
181-
def rootContext: Context // TODO: Should this be moved to QuoteContext?
182-
given Context = rootContext // TODO: Should be an implicit converion from QuoteContext to Context
183-
184173
///////////////
185174
// TREES //
186175
///////////////

scala3doc/src/dotty/dokka/tasty/BasicSupport.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ trait BasicSupport:
3434

3535

3636
extension (sym: Symbol):
37-
def documentation(using cxt: Context) = sym.documentation match
37+
def documentation = sym.documentation match
3838
case Some(comment) =>
3939
Map(sourceSet -> parseComment(comment, sym.tree))
4040
case None =>
4141
Map.empty
4242

43-
def source(using ctx: Context) =
43+
def source =
4444
val path = Some(sym.pos.sourceFile.jpath).filter(_ != null).map(_.toAbsolutePath).map(_.toString)
4545
path.map(TastyDocumentableSource(_, sym.pos.startLine))
4646

scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ trait ClassLikeSupport:
125125
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
126126
Some(parseTypeDef(td))
127127

128-
case vd: ValDef if !isSyntheticField(vd.symbol)
129-
&& (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum))
130-
&& vd.symbol.isGiven =>
128+
case vd: ValDef if !isSyntheticField(vd.symbol)
129+
&& (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum))
130+
&& vd.symbol.isGiven =>
131131
val classDef = Some(vd.tpt.tpe).flatMap(_.classSymbol.map(_.tree.asInstanceOf[ClassDef]))
132132
Some(classDef.filter(_.symbol.flags.is(Flags.ModuleClass)).fold[Member](parseValDef(vd))(parseGivenClasslike(_)))
133133

@@ -213,12 +213,12 @@ trait ClassLikeSupport:
213213
parseMethod(d, constructorWithoutParamLists(c), paramModifierFunc.getOrElse(s => c.getParameterModifier(s)))
214214
)
215215

216-
def parseClasslike(classDef: ClassDef, signatureOnly: Boolean = false)(using ctx: Context): DClass = classDef match
216+
def parseClasslike(classDef: ClassDef, signatureOnly: Boolean = false): DClass = classDef match
217217
case c: ClassDef if classDef.symbol.flags.is(Flags.Object) => parseObject(c, signatureOnly)
218218
case c: ClassDef if classDef.symbol.flags.is(Flags.Enum) => parseEnum(c, signatureOnly)
219219
case clazz => DClass(classDef)(signatureOnly = signatureOnly)
220220

221-
def parseObject(classDef: ClassDef, signatureOnly: Boolean = false)(using ctx: Context): DClass =
221+
def parseObject(classDef: ClassDef, signatureOnly: Boolean = false): DClass =
222222
DClass(classDef)(
223223
name = classDef.name.stripSuffix("$"),
224224
// All objects are final so we do not need final modifer!
@@ -227,7 +227,7 @@ trait ClassLikeSupport:
227227
)
228228

229229
// TODO check withNewExtras?
230-
def parseEnum(classDef: ClassDef, signatureOnly: Boolean = false)(using ctx: Context): DClass =
230+
def parseEnum(classDef: ClassDef, signatureOnly: Boolean = false): DClass =
231231
val extraModifiers = classDef.symbol.getExtraModifiers().filter(_ != Modifier.Sealed).filter(_ != Modifier.Abstract)
232232
val companion = classDef.symbol.getCompanionSymbol.map(_.tree.asInstanceOf[ClassDef]).get
233233

scala3doc/src/dotty/dokka/tasty/SymOps.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class SymOps[Q <: QuoteContext](val q: Q):
1515

1616
given Q = q
1717
extension (sym: Symbol):
18-
def packageName(using ctx: Context): String =
18+
def packageName: String =
1919
if (sym.isPackageDef) sym.fullName
2020
else sym.maybeOwner.packageName
2121

22-
def topLevelEntryName(using ctx: Context): Option[String] = if (sym.isPackageDef) None else
22+
def topLevelEntryName: Option[String] = if (sym.isPackageDef) None else
2323
if (sym.owner.isPackageDef) Some(sym.name) else sym.owner.topLevelEntryName
2424

2525
def getVisibility(): Visibility =
@@ -50,7 +50,7 @@ class SymOps[Q <: QuoteContext](val q: Q):
5050
// TODO: #49 Remove it after TASTY-Reflect release with published flag Extension
5151
def hackIsOpen: Boolean = {
5252
import dotty.tools.dotc
53-
given dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
53+
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
5454
val symbol = sym.asInstanceOf[dotc.core.Symbols.Symbol]
5555
symbol.is(dotc.core.Flags.Open)
5656
}

scala3doc/src/dotty/dokka/tasty/SyntheticSupport.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ trait SyntheticsSupport:
1414

1515
def hackIsTupleType(using QuoteContext)(rtpe: qctx.reflect.TypeRepr): Boolean =
1616
import dotty.tools.dotc
17-
given ctx as dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
17+
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
1818
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
1919
ctx.definitions.isTupleType(tpe)
2020

2121
def hackIsCompiletimeAppliedType(using QuoteContext)(rtpe: qctx.reflect.TypeRepr): Boolean =
2222
import dotty.tools.dotc
23-
given ctx as dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
23+
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
2424
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
2525
ctx.definitions.isCompiletimeAppliedType(tpe.typeSymbol)
2626

@@ -55,7 +55,7 @@ trait SyntheticsSupport:
5555
def hackIsInfix(using QuoteContext)(rsym: qctx.reflect.Symbol): Boolean = {
5656
import qctx.reflect._
5757
import dotty.tools.dotc
58-
given ctx as dotc.core.Contexts.Context = rootContext.asInstanceOf
58+
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
5959
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
6060
ctx.definitions.isInfix(sym)
6161
}
@@ -66,7 +66,7 @@ trait SyntheticsSupport:
6666
def hackGetAllMembers(using QuoteContext)(rsym: qctx.reflect.Symbol): List[qctx.reflect.Symbol] = {
6767
import qctx.reflect._
6868
import dotty.tools.dotc
69-
given ctx as dotc.core.Contexts.Context = rootContext.asInstanceOf
69+
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
7070
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
7171
sym.typeRef.appliedTo(sym.typeParams.map(_.typeRef)).allMembers.iterator.map(_.symbol)
7272
.collect {
@@ -80,7 +80,7 @@ trait SyntheticsSupport:
8080
def hackGetSupertypes(using QuoteContext)(rdef: qctx.reflect.ClassDef) = {
8181
import qctx.reflect._
8282
import dotty.tools.dotc
83-
given dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
83+
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
8484
val classdef = rdef.asInstanceOf[dotc.ast.tpd.TypeDef]
8585
val ref = classdef.symbol.info.asInstanceOf[dotc.core.Types.ClassInfo].appliedRef
8686
val baseTypes: List[(dotc.core.Symbols.Symbol, dotc.core.Types.Type)] =
@@ -93,7 +93,7 @@ trait SyntheticsSupport:
9393
def typeForClass(c: ClassDef): TypeRepr =
9494
import qctx.reflect._
9595
import dotty.tools.dotc
96-
given dotc.core.Contexts.Context = rootContext.asInstanceOf
96+
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
9797
val cSym = c.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
9898
cSym.typeRef.appliedTo(cSym.typeParams.map(_.typeRef)).asInstanceOf[TypeRepr]
9999

scala3doc/src/dotty/dokka/tasty/TypesSupport.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait TypesSupport:
3737

3838
given TreeSyntax as AnyRef:
3939
extension (tpeTree: Tree):
40-
def dokkaType(using cxt: Context): Bound =
40+
def dokkaType: Bound =
4141
val data = tpeTree match
4242
case TypeBoundsTree(low, high) => typeBound(low.tpe, low = true) ++ typeBound(high.tpe, low = false)
4343
case tpeTree: TypeTree => inner(tpeTree.tpe)
@@ -47,7 +47,7 @@ trait TypesSupport:
4747

4848
given TypeSyntax as AnyRef:
4949
extension (tpe: TypeRepr):
50-
def dokkaType(using ctx: Context): Bound =
50+
def dokkaType: Bound =
5151
val data = inner(tpe)
5252
val dri = data.collect{
5353
case o: TypeParameter => o
@@ -59,7 +59,7 @@ trait TypesSupport:
5959
private def texts(str: String): List[JProjection] = List(text(str))
6060

6161

62-
private def link(symbol: Symbol)(using cxt: Context): List[JProjection] = {
62+
private def link(symbol: Symbol): List[JProjection] = {
6363
val suffix = if symbol.isValDef then texts(".type") else Nil
6464
(new TypeParameter(symbol.dri, symbol.name, null)) :: suffix
6565
}
@@ -74,7 +74,7 @@ trait TypesSupport:
7474
tpeAnnotation.tpe.typeSymbol.toString == "class Repeated"
7575

7676
// TODO #23 add support for all types signatures that makes sense
77-
private def inner(tp: TypeRepr)(using cxt: Context): List[JProjection] =
77+
private def inner(tp: TypeRepr): List[JProjection] =
7878
def noSupported(name: String): List[JProjection] =
7979
println(s"WARN: Unsupported type: $name: ${tp.show}")
8080
List(text(s"Unsupported[$name]"))

scala3doc/src/dotty/dokka/tasty/comments/MemberLookup.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,16 @@ trait MemberLookup {
6262
private def hackMembersOf(using QuoteContext)(rsym: qctx.reflect.Symbol) = {
6363
import qctx.reflect._
6464
import dotty.tools.dotc
65-
given dotc.core.Contexts.Context = rootContext.asInstanceOf
65+
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
6666
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
6767
val members = sym.info.decls.iterator.filter(_.isCompleted)
6868
// println(s"members of ${sym.show} : ${members.map(_.show).mkString(", ")}")
6969
members.asInstanceOf[Iterator[Symbol]]
7070
}
7171

7272
private def hackIsNotAbsent(using QuoteContext)(rsym: qctx.reflect.Symbol) = {
73-
import qctx.reflect._
7473
import dotty.tools.dotc
75-
given dotc.core.Contexts.Context = rootContext.asInstanceOf
74+
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.internal.impl.QuoteContextImpl].ctx
7675
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
7776
sym.isCompleted
7877
}

0 commit comments

Comments
 (0)