Skip to content

Commit acefb73

Browse files
committed
Add missing positions to trees created with TASTy reflect
1 parent 2b0e4a1 commit acefb73

File tree

9 files changed

+63
-44
lines changed

9 files changed

+63
-44
lines changed
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dotty.tools.dotc.tastyreflect
22

3-
import dotty.tools.dotc.util.{Spans, SourcePosition}
4-
53
trait ContextOpsImpl extends scala.tasty.reflect.ContextOps with CoreImpl {
64

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

15-
def rootPosition: SourcePosition = SourcePosition(rootContext.source, Spans.NoSpan)
16-
1713
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dotty.tools.dotc.tastyreflect
22

33
import dotty.tools.dotc.ast.Trees
4+
import dotty.tools.dotc.ast.tpd
5+
import dotty.tools.dotc.core.Contexts._
46

57
trait Helpers {
68

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ReflectionImpl(val rootContext: Contexts.Context)
1515
with PatternOpsImpl
1616
with PositionOpsImpl
1717
with PrintersImpl
18+
with RootPositionImpl
1819
with SettingsOpsImpl
1920
with SignatureOpsImpl
2021
with StandardDefinitions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.util.{SourcePosition, Spans}
4+
5+
trait RootPositionImpl extends scala.tasty.reflect.RootPosition with ContextOpsImpl with CoreImpl {
6+
7+
def rootPosition: SourcePosition = SourcePosition(rootContext.source, Spans.NoSpan)
8+
9+
protected def withDefaultPos[T <: Tree](fn: Context => T)(implicit ctx: Context): T = {
10+
fn(ctx.withSource(rootPosition.source)).withSpan(rootPosition.span)
11+
}
12+
13+
}

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dotty.tools.dotc.core.Symbols.NoSymbol
77
import dotty.tools.dotc.core._
88
import dotty.tools.dotc.tastyreflect.FromSymbol.{definitionFromSym, packageDefFromSym}
99

10-
trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers {
10+
trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with RootPositionImpl with Helpers {
1111

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

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

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

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

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

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

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

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

380380

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

385385
object Ident extends IdentModule {
@@ -422,7 +422,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
422422
object Literal extends LiteralModule {
423423

424424
def apply(constant: Constant)(implicit ctx: Context): Literal =
425-
tpd.Literal(constant)
425+
withDefaultPos(ctx => tpd.Literal(constant)(ctx))
426426

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

446446
def apply(cls: ClassSymbol)(implicit ctx: Context): This =
447-
tpd.This(cls)
447+
withDefaultPos(ctx => tpd.This(cls)(ctx))
448448

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

465465
object New extends NewModule {
466466

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

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

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

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

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

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

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

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

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

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

577577
object Typed extends TypedModule {
578578
def apply(expr: Term, tpt: TypeTree)(implicit ctx: Context): Typed =
579-
tpd.Typed(expr, tpt)
579+
withDefaultPos(ctx => tpd.Typed(expr, tpt)(ctx))
580580

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

598598
object Assign extends AssignModule {
599599
def apply(lhs: Term, rhs: Term)(implicit ctx: Context): Assign =
600-
tpd.Assign(lhs, rhs)
600+
withDefaultPos(ctx => tpd.Assign(lhs, rhs)(ctx))
601601

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

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

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

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

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

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

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

712712
object If extends IfModule {
713713
def apply(cond: Term, thenp: Term, elsep: Term)(implicit ctx: Context): If =
714-
tpd.If(cond, thenp, elsep)
714+
withDefaultPos(ctx => tpd.If(cond, thenp, elsep)(ctx))
715715

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

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

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

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

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

775775
object Return extends ReturnModule {
776776
def apply(expr: Term)(implicit ctx: Context): Return =
777-
tpd.Return(expr, ctx.owner)
777+
withDefaultPos(ctx => tpd.Return(expr, ctx.owner)(ctx))
778778

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

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

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

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

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

847847
object While extends WhileModule {
848848
def apply(cond: Term, body: Term)(implicit ctx: Context): While =
849-
tpd.WhileDo(cond, body)
849+
withDefaultPos(ctx => tpd.WhileDo(cond, body)(ctx))
850850

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

861861
def termAsTermOrTypeTree(term: Term): TermOrTypeTree = term
862+
862863
}

0 commit comments

Comments
 (0)