Skip to content

Remove old owner context from reflection API #10414

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

Merged
merged 1 commit into from
Nov 21, 2020
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
14 changes: 7 additions & 7 deletions compiler/src/scala/quoted/runtime/impl/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ object Matcher {

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

extension (scrutinee0: Tree):
Expand All @@ -149,7 +149,7 @@ object Matcher {
* @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`.
* @return `None` if it did not match or `Some(tup: Tuple)` if it matched where `tup` contains the contents of the holes.
*/
private def =?= (pattern0: Tree)(using Context, Env): Matching = {
private def =?= (pattern0: Tree)(using Env): Matching = {

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

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

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

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

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

private def treeOptMatches(scrutinee: Option[Tree], pattern: Option[Tree])(using Context, Env): Matching = {
private def treeOptMatches(scrutinee: Option[Tree], pattern: Option[Tree])(using Env): Matching = {
(scrutinee, pattern) match {
case (Some(x), Some(y)) => x =?= y
case (None, None) => matched
Expand Down
17 changes: 6 additions & 11 deletions compiler/src/scala/quoted/runtime/impl/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ object QuoteContextImpl {
type ScopeId = Int

def apply()(using Context): QuoteContext =
new QuoteContextImpl(ctx)
new QuoteContextImpl

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

}

class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickler, QuoteMatching:
class QuoteContextImpl private (using val ctx: Context) extends QuoteContext, QuoteUnpickler, QuoteMatching:

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

object reflect extends Reflection:

def rootContext: Context = ctx

type Context = dotc.core.Contexts.Context

type Tree = tpd.Tree

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

object Position extends PositionModule:
def ofMacroExpansion: dotc.util.SourcePosition =
MacroExpansion.position.getOrElse(dotc.util.SourcePosition(rootContext.source, dotc.util.Spans.NoSpan))
MacroExpansion.position.getOrElse(dotc.util.SourcePosition(ctx.source, dotc.util.Spans.NoSpan))
end Position

object PositionMethodsImpl extends PositionMethods:
Expand Down Expand Up @@ -2599,11 +2595,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
end reflect

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] =
val tree = PickledQuotes.unpickleTerm(pickled, typeHole, termHole)(using reflect.rootContext)
val tree = PickledQuotes.unpickleTerm(pickled, typeHole, termHole)
new ExprImpl(tree, hash).asInstanceOf[scala.quoted.Expr[T]]

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] =
val tree = PickledQuotes.unpickleTypeTree(pickled, typeHole, termHole)(using reflect.rootContext)
val tree = PickledQuotes.unpickleTypeTree(pickled, typeHole, termHole)
new TypeImpl(tree, hash).asInstanceOf[scala.quoted.Type[T]]

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

private def treeMatch(scrutinee: reflect.Tree, pattern: reflect.Tree): Option[Tuple] = {
import reflect._
given Context = rootContext
def isTypeHoleDef(tree: Tree): Boolean =
tree match
case tree: TypeDef =>
Expand Down
11 changes: 0 additions & 11 deletions library/src/scala/quoted/QuoteContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,6 @@ trait QuoteContext { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*/
trait Reflection { self: reflect.type =>

//////////////
// CONTEXTS //
//////////////

/** Context containing information on the current owner */
type Context <: AnyRef

/** Context of the macro expansion */
def rootContext: Context // TODO: Should this be moved to QuoteContext?
given Context = rootContext // TODO: Should be an implicit converion from QuoteContext to Context

///////////////
// TREES //
///////////////
Expand Down
4 changes: 2 additions & 2 deletions scala3doc/src/dotty/dokka/tasty/BasicSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ trait BasicSupport:


extension (sym: Symbol):
def documentation(using cxt: Context) = sym.documentation match
def documentation = sym.documentation match
case Some(comment) =>
Map(sourceSet -> parseComment(comment, sym.tree))
case None =>
Map.empty

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

Expand Down
12 changes: 6 additions & 6 deletions scala3doc/src/dotty/dokka/tasty/ClassLikeSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ trait ClassLikeSupport:
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td))

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

Expand Down Expand Up @@ -213,12 +213,12 @@ trait ClassLikeSupport:
parseMethod(d, constructorWithoutParamLists(c), paramModifierFunc.getOrElse(s => c.getParameterModifier(s)))
)

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions scala3doc/src/dotty/dokka/tasty/SymOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class SymOps[Q <: QuoteContext](val q: Q):

given Q = q
extension (sym: Symbol):
def packageName(using ctx: Context): String =
def packageName: String =
if (sym.isPackageDef) sym.fullName
else sym.maybeOwner.packageName

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

def getVisibility(): Visibility =
Expand Down Expand Up @@ -50,7 +50,7 @@ class SymOps[Q <: QuoteContext](val q: Q):
// TODO: #49 Remove it after TASTY-Reflect release with published flag Extension
def hackIsOpen: Boolean = {
import dotty.tools.dotc
given dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val symbol = sym.asInstanceOf[dotc.core.Symbols.Symbol]
symbol.is(dotc.core.Flags.Open)
}
Expand Down
12 changes: 6 additions & 6 deletions scala3doc/src/dotty/dokka/tasty/SyntheticSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ trait SyntheticsSupport:

def hackIsTupleType(using QuoteContext)(rtpe: qctx.reflect.TypeRepr): Boolean =
import dotty.tools.dotc
given ctx as dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
ctx.definitions.isTupleType(tpe)

def hackIsCompiletimeAppliedType(using QuoteContext)(rtpe: qctx.reflect.TypeRepr): Boolean =
import dotty.tools.dotc
given ctx as dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
ctx.definitions.isCompiletimeAppliedType(tpe.typeSymbol)

Expand Down Expand Up @@ -55,7 +55,7 @@ trait SyntheticsSupport:
def hackIsInfix(using QuoteContext)(rsym: qctx.reflect.Symbol): Boolean = {
import qctx.reflect._
import dotty.tools.dotc
given ctx as dotc.core.Contexts.Context = rootContext.asInstanceOf
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
ctx.definitions.isInfix(sym)
}
Expand All @@ -66,7 +66,7 @@ trait SyntheticsSupport:
def hackGetAllMembers(using QuoteContext)(rsym: qctx.reflect.Symbol): List[qctx.reflect.Symbol] = {
import qctx.reflect._
import dotty.tools.dotc
given ctx as dotc.core.Contexts.Context = rootContext.asInstanceOf
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why it needs the cast --- ideally APIs in scala.quote._ will be enough for usage, so that there is no need to use both reflect API and compiler API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This hacky method needs to be removed. Nobody should ever do this kind of cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is in the list of TODOs

val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
sym.typeRef.appliedTo(sym.typeParams.map(_.typeRef)).allMembers.iterator.map(_.symbol)
.collect {
Expand All @@ -80,7 +80,7 @@ trait SyntheticsSupport:
def hackGetSupertypes(using QuoteContext)(rdef: qctx.reflect.ClassDef) = {
import qctx.reflect._
import dotty.tools.dotc
given dotc.core.Contexts.Context = qctx.reflect.rootContext.asInstanceOf
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val classdef = rdef.asInstanceOf[dotc.ast.tpd.TypeDef]
val ref = classdef.symbol.info.asInstanceOf[dotc.core.Types.ClassInfo].appliedRef
val baseTypes: List[(dotc.core.Symbols.Symbol, dotc.core.Types.Type)] =
Expand All @@ -93,7 +93,7 @@ trait SyntheticsSupport:
def typeForClass(c: ClassDef): TypeRepr =
import qctx.reflect._
import dotty.tools.dotc
given dotc.core.Contexts.Context = rootContext.asInstanceOf
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val cSym = c.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
cSym.typeRef.appliedTo(cSym.typeParams.map(_.typeRef)).asInstanceOf[TypeRepr]

Expand Down
8 changes: 4 additions & 4 deletions scala3doc/src/dotty/dokka/tasty/TypesSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait TypesSupport:

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

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


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

// TODO #23 add support for all types signatures that makes sense
private def inner(tp: TypeRepr)(using cxt: Context): List[JProjection] =
private def inner(tp: TypeRepr): List[JProjection] =
def noSupported(name: String): List[JProjection] =
println(s"WARN: Unsupported type: $name: ${tp.show}")
List(text(s"Unsupported[$name]"))
Expand Down
5 changes: 2 additions & 3 deletions scala3doc/src/dotty/dokka/tasty/comments/MemberLookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ trait MemberLookup {
private def hackMembersOf(using QuoteContext)(rsym: qctx.reflect.Symbol) = {
import qctx.reflect._
import dotty.tools.dotc
given dotc.core.Contexts.Context = rootContext.asInstanceOf
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
val members = sym.info.decls.iterator.filter(_.isCompleted)
// println(s"members of ${sym.show} : ${members.map(_.show).mkString(", ")}")
members.asInstanceOf[Iterator[Symbol]]
}

private def hackIsNotAbsent(using QuoteContext)(rsym: qctx.reflect.Symbol) = {
import qctx.reflect._
import dotty.tools.dotc
given dotc.core.Contexts.Context = rootContext.asInstanceOf
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuoteContextImpl].ctx
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
sym.isCompleted
}
Expand Down