Skip to content

Add missing positions to trees created with TASTy reflect #5940

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
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
@@ -1,7 +1,5 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.util.{Spans, SourcePosition}

trait ContextOpsImpl extends scala.tasty.reflect.ContextOps with CoreImpl {

val rootContext: Context
Expand All @@ -12,6 +10,4 @@ trait ContextOpsImpl extends scala.tasty.reflect.ContextOps with CoreImpl {
def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
}

def rootPosition: SourcePosition = SourcePosition(rootContext.source, Spans.NoSpan)

}
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/Helpers.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.ast.Trees
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts._

trait Helpers {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ReflectionImpl(val rootContext: Contexts.Context)
with PatternOpsImpl
with PositionOpsImpl
with PrintersImpl
with RootPositionImpl
with SettingsOpsImpl
with SignatureOpsImpl
with StandardDefinitions
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/RootPositionImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.util.{SourcePosition, Spans}

trait RootPositionImpl extends scala.tasty.reflect.RootPosition with ContextOpsImpl with CoreImpl {

def rootPosition: SourcePosition = SourcePosition(rootContext.source, Spans.NoSpan)

protected def withDefaultPos[T <: Tree](fn: Context => T)(implicit ctx: Context): T = {
fn(ctx.withSource(rootPosition.source)).withSpan(rootPosition.span)
}

}
51 changes: 26 additions & 25 deletions compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dotty.tools.dotc.core.Symbols.NoSymbol
import dotty.tools.dotc.core._
import dotty.tools.dotc.tastyreflect.FromSymbol.{definitionFromSym, packageDefFromSym}

trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers {
trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with RootPositionImpl with Helpers {

def TreeDeco(tree: Tree): TreeAPI = new TreeAPI {
def pos(implicit ctx: Context): Position = tree.sourcePos
Expand Down Expand Up @@ -190,7 +190,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object PackageClause extends PackageClauseModule {
def apply(pid: Term.Ref, stats: List[Tree])(implicit ctx: Context): PackageClause =
tpd.PackageDef(pid.asInstanceOf[tpd.RefTree], stats)
withDefaultPos(ctx => tpd.PackageDef(pid.asInstanceOf[tpd.RefTree], stats)(ctx))

def copy(original: PackageClause)(pid: Term.Ref, stats: List[Tree])(implicit ctx: Context): PackageClause =
tpd.cpy.PackageDef(original)(pid, stats)
Expand All @@ -210,7 +210,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Import extends ImportModule {
def apply(impliedOnly: Boolean, expr: Term, selectors: List[ImportSelector])(implicit ctx: Context): Import =
tpd.Import(impliedOnly, expr, selectors)
withDefaultPos(ctx => tpd.Import(impliedOnly, expr, selectors)(ctx))

def copy(original: Import)(impliedOnly: Boolean, expr: Term, selectors: List[ImportSelector])(implicit ctx: Context): Import =
tpd.cpy.Import(original)(impliedOnly, expr, selectors)
Expand Down Expand Up @@ -276,7 +276,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object DefDef extends DefDefModule {
def apply(symbol: DefSymbol, rhsFn: List[Type] => List[List[Term]] => Option[Term])(implicit ctx: Context): DefDef =
tpd.polyDefDef(symbol, tparams => vparamss => rhsFn(tparams)(vparamss).getOrElse(tpd.EmptyTree))
withDefaultPos(ctx => tpd.polyDefDef(symbol, tparams => vparamss => rhsFn(tparams)(vparamss).getOrElse(tpd.EmptyTree))(ctx))

def copy(original: DefDef)(name: String, typeParams: List[TypeDef], paramss: List[List[ValDef]], tpt: TypeTree, rhs: Option[Term])(implicit ctx: Context): DefDef =
tpd.cpy.DefDef(original)(name.toTermName, typeParams, paramss, tpt, rhs.getOrElse(tpd.EmptyTree))
Expand Down Expand Up @@ -323,7 +323,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
}

object TypeDef extends TypeDefModule {
def apply(symbol: TypeSymbol)(implicit ctx: Context): TypeDef = tpd.TypeDef(symbol)
def apply(symbol: TypeSymbol)(implicit ctx: Context): TypeDef = withDefaultPos(ctx => tpd.TypeDef(symbol)(ctx))
def copy(original: TypeDef)(name: String, rhs: TypeOrBoundsTree)(implicit ctx: Context): TypeDef =
tpd.cpy.TypeDef(original)(name.toTypeName, rhs)
def unapply(tree: Tree)(implicit ctx: Context): Option[(String, TypeOrBoundsTree /* TypeTree | TypeBoundsTree */)] = tree match {
Expand Down Expand Up @@ -379,7 +379,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers


object Ref extends RefModule {
def apply(sym: Symbol)(implicit ctx: Context): Ref = tpd.ref(sym).asInstanceOf[tpd.RefTree]
def apply(sym: Symbol)(implicit ctx: Context): Ref = withDefaultPos(ctx => tpd.ref(sym)(ctx).asInstanceOf[tpd.RefTree])
}

object Ident extends IdentModule {
Expand Down Expand Up @@ -422,7 +422,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
object Literal extends LiteralModule {

def apply(constant: Constant)(implicit ctx: Context): Literal =
tpd.Literal(constant)
withDefaultPos(ctx => tpd.Literal(constant)(ctx))

def copy(original: Tree)(constant: Constant)(implicit ctx: Context): Literal =
tpd.cpy.Literal(original)(constant)
Expand All @@ -444,7 +444,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
object This extends ThisModule {

def apply(cls: ClassSymbol)(implicit ctx: Context): This =
tpd.This(cls)
withDefaultPos(ctx => tpd.This(cls)(ctx))

def copy(original: Tree)(qual: Option[Id])(implicit ctx: Context): This =
tpd.cpy.This(original)(qual.getOrElse(untpd.EmptyTypeIdent))
Expand All @@ -464,7 +464,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object New extends NewModule {

def apply(tpt: TypeTree)(implicit ctx: Context): New = tpd.New(tpt)
def apply(tpt: TypeTree)(implicit ctx: Context): New = withDefaultPos(ctx => tpd.New(tpt)(ctx))

def copy(original: Tree)(tpt: TypeTree)(implicit ctx: Context): New =
tpd.cpy.New(original)(tpt)
Expand All @@ -490,7 +490,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
object NamedArg extends NamedArgModule {

def apply(name: String, arg: Term)(implicit ctx: Context): NamedArg =
tpd.NamedArg(name.toTermName, arg)
withDefaultPos(ctx => tpd.NamedArg(name.toTermName, arg)(ctx))

def copy(tree: NamedArg)(name: String, arg: Term)(implicit ctx: Context): NamedArg =
tpd.cpy.NamedArg(tree)(name.toTermName, arg)
Expand All @@ -512,7 +512,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
object Apply extends ApplyModule {

def apply(fn: Term, args: List[Term])(implicit ctx: Context): Apply =
tpd.Apply(fn, args)
withDefaultPos(ctx => tpd.Apply(fn, args)(ctx))

def copy(original: Tree)(fun: Term, args: List[Term])(implicit ctx: Context): Apply =
tpd.cpy.Apply(original)(fun, args)
Expand All @@ -534,7 +534,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
object TypeApply extends TypeApplyModule {

def apply(fn: Term, args: List[TypeTree])(implicit ctx: Context): TypeApply =
tpd.TypeApply(fn, args)
withDefaultPos(ctx => tpd.TypeApply(fn, args)(ctx))

def copy(original: Tree)(fun: Term, args: List[TypeTree])(implicit ctx: Context): TypeApply =
tpd.cpy.TypeApply(original)(fun, args)
Expand All @@ -555,7 +555,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Super extends SuperModule {
def apply(qual: Term, mix: Option[Id])(implicit ctx: Context): Super =
tpd.Super(qual, mix.getOrElse(untpd.EmptyTypeIdent), false, NoSymbol)
withDefaultPos(ctx => tpd.Super(qual, mix.getOrElse(untpd.EmptyTypeIdent), false, NoSymbol)(ctx))

def copy(original: Tree)(qual: Term, mix: Option[Id])(implicit ctx: Context): Super =
tpd.cpy.Super(original)(qual, mix.getOrElse(untpd.EmptyTypeIdent))
Expand All @@ -576,7 +576,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Typed extends TypedModule {
def apply(expr: Term, tpt: TypeTree)(implicit ctx: Context): Typed =
tpd.Typed(expr, tpt)
withDefaultPos(ctx => tpd.Typed(expr, tpt)(ctx))

def copy(original: Tree)(expr: Term, tpt: TypeTree)(implicit ctx: Context): Typed =
tpd.cpy.Typed(original)(expr, tpt)
Expand All @@ -597,7 +597,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Assign extends AssignModule {
def apply(lhs: Term, rhs: Term)(implicit ctx: Context): Assign =
tpd.Assign(lhs, rhs)
withDefaultPos(ctx => tpd.Assign(lhs, rhs)(ctx))

def copy(original: Tree)(lhs: Term, rhs: Term)(implicit ctx: Context): Assign =
tpd.cpy.Assign(original)(lhs, rhs)
Expand Down Expand Up @@ -647,7 +647,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Block extends BlockModule {
def apply(stats: List[Statement], expr: Term)(implicit ctx: Context): Block =
tpd.Block(stats, expr)
withDefaultPos(ctx => tpd.Block(stats, expr)(ctx))

def copy(original: Tree)(stats: List[Statement], expr: Term)(implicit ctx: Context): Block =
tpd.cpy.Block(original)(stats, expr)
Expand All @@ -668,7 +668,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Inlined extends InlinedModule {
def apply(call: Option[TermOrTypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, expansion)
withDefaultPos(ctx => tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, expansion)(ctx))

def copy(original: Tree)(call: Option[TermOrTypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
tpd.cpy.Inlined(original)(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], expansion)
Expand All @@ -690,7 +690,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Lambda extends LambdaModule {
def apply(meth: Term, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
tpd.Closure(Nil, meth, tpt.getOrElse(tpd.EmptyTree))
withDefaultPos(ctx => tpd.Closure(Nil, meth, tpt.getOrElse(tpd.EmptyTree))(ctx))

def copy(original: Tree)(meth: Tree, tpt: Option[TypeTree])(implicit ctx: Context): Lambda =
tpd.cpy.Closure(original)(Nil, meth, tpt.getOrElse(tpd.EmptyTree))
Expand All @@ -711,7 +711,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object If extends IfModule {
def apply(cond: Term, thenp: Term, elsep: Term)(implicit ctx: Context): If =
tpd.If(cond, thenp, elsep)
withDefaultPos(ctx => tpd.If(cond, thenp, elsep)(ctx))

def copy(original: Tree)(cond: Term, thenp: Term, elsep: Term)(implicit ctx: Context): If =
tpd.cpy.If(original)(cond, thenp, elsep)
Expand All @@ -732,7 +732,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Match extends MatchModule {
def apply(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match =
tpd.Match(selector, cases)
withDefaultPos(ctx => tpd.Match(selector, cases)(ctx))

def copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match =
tpd.cpy.Match(original)(selector, cases)
Expand All @@ -753,7 +753,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Try extends TryModule {
def apply(expr: Term, cases: List[CaseDef], finalizer: Option[Term])(implicit ctx: Context): Try =
tpd.Try(expr, cases, finalizer.getOrElse(tpd.EmptyTree))
withDefaultPos(ctx => tpd.Try(expr, cases, finalizer.getOrElse(tpd.EmptyTree))(ctx))

def copy(original: Tree)(expr: Term, cases: List[CaseDef], finalizer: Option[Term])(implicit ctx: Context): Try =
tpd.cpy.Try(original)(expr, cases, finalizer.getOrElse(tpd.EmptyTree))
Expand All @@ -774,7 +774,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Return extends ReturnModule {
def apply(expr: Term)(implicit ctx: Context): Return =
tpd.Return(expr, ctx.owner)
withDefaultPos(ctx => tpd.Return(expr, ctx.owner)(ctx))

def copy(original: Tree)(expr: Term)(implicit ctx: Context): Return =
tpd.cpy.Return(original)(expr, tpd.ref(ctx.owner))
Expand All @@ -795,7 +795,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object Repeated extends RepeatedModule {
def apply(elems: List[Term], elemtpt: TypeTree)(implicit ctx: Context): Repeated =
tpd.SeqLiteral(elems, elemtpt)
withDefaultPos(ctx => tpd.SeqLiteral(elems, elemtpt)(ctx))

def copy(original: Tree)(elems: List[Term], elemtpt: TypeTree)(implicit ctx: Context): Repeated =
tpd.cpy.SeqLiteral(original)(elems, elemtpt)
Expand All @@ -821,7 +821,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
object SelectOuter extends SelectOuterModule {

def apply(qualifier: Term, name: String, levels: Int)(implicit ctx: Context): SelectOuter =
tpd.Select(qualifier, NameKinds.OuterSelectName(name.toTermName, levels))
withDefaultPos(ctx => tpd.Select(qualifier, NameKinds.OuterSelectName(name.toTermName, levels))(ctx))

def copy(original: Tree)(qualifier: Term, name: String, levels: Int)(implicit ctx: Context): SelectOuter =
tpd.cpy.Select(original)(qualifier, NameKinds.OuterSelectName(name.toTermName, levels))
Expand All @@ -846,7 +846,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers

object While extends WhileModule {
def apply(cond: Term, body: Term)(implicit ctx: Context): While =
tpd.WhileDo(cond, body)
withDefaultPos(ctx => tpd.WhileDo(cond, body)(ctx))

def copy(original: Tree)(cond: Term, body: Term)(implicit ctx: Context): While =
tpd.cpy.WhileDo(original)(cond, body)
Expand All @@ -859,4 +859,5 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
}

def termAsTermOrTypeTree(term: Term): TermOrTypeTree = term

}
Loading