Skip to content

Commit f9fb06e

Browse files
committed
withPos -> withSpan
1 parent bf3946d commit f9fb06e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+191
-186
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object desugar {
113113
def derivedTypeParam(tdef: TypeDef, suffix: String = "")(implicit ctx: Context): TypeDef =
114114
cpy.TypeDef(tdef)(
115115
name = tdef.name ++ suffix,
116-
rhs = new DerivedFromParamTree(suffix).withPos(tdef.rhs.pos).watching(tdef)
116+
rhs = new DerivedFromParamTree(suffix).withPosOf(tdef.rhs).watching(tdef)
117117
)
118118

119119
/** A derived type definition watching `sym` */
@@ -123,7 +123,7 @@ object desugar {
123123
/** A value definition copied from `vdef` with a tpt typetree derived from it */
124124
def derivedTermParam(vdef: ValDef)(implicit ctx: Context): ValDef =
125125
cpy.ValDef(vdef)(
126-
tpt = new DerivedFromParamTree("").withPos(vdef.tpt.pos).watching(vdef))
126+
tpt = new DerivedFromParamTree("").withPosOf(vdef.tpt).watching(vdef))
127127

128128
// ----- Desugar methods -------------------------------------------------
129129

@@ -351,10 +351,10 @@ object desugar {
351351
val constrVparamss =
352352
if (originalVparamss.isEmpty) { // ensure parameter list is non-empty
353353
if (isCaseClass && originalTparams.isEmpty)
354-
ctx.error(CaseClassMissingParamList(cdef), cdef.sourcePos.withRange(cdef.namePos))
354+
ctx.error(CaseClassMissingParamList(cdef), cdef.sourcePos.withSpan(cdef.namePos))
355355
ListOfNil
356356
} else if (isCaseClass && originalVparamss.head.exists(_.mods.is(Implicit))) {
357-
ctx.error("Case classes should have a non-implicit parameter list", cdef.sourcePos.withRange(cdef.namePos))
357+
ctx.error("Case classes should have a non-implicit parameter list", cdef.sourcePos.withSpan(cdef.namePos))
358358
ListOfNil
359359
}
360360
else originalVparamss.nestedMap(toDefParam)
@@ -398,7 +398,7 @@ object desugar {
398398

399399
def appliedTypeTree(tycon: Tree, args: List[Tree]) =
400400
(if (args.isEmpty) tycon else AppliedTypeTree(tycon, args))
401-
.withPos(cdef.pos.startPos)
401+
.withSpan(cdef.pos.startPos)
402402

403403
def isHK(tparam: Tree): Boolean = tparam match {
404404
case TypeDef(_, LambdaTypeTree(tparams, body)) => true
@@ -564,7 +564,7 @@ object desugar {
564564
ModuleDef(
565565
className.toTermName, Template(emptyConstructor, parentTpt :: Nil, EmptyValDef, defs))
566566
.withMods(companionMods | Synthetic))
567-
.withPos(cdef.pos).toList
567+
.withPosOf(cdef).toList
568568

569569
val companionMembers = defaultGetters ::: eqInstances ::: enumCases
570570

@@ -653,7 +653,7 @@ object desugar {
653653
// we can reuse the constructor parameters; no derived params are needed.
654654
DefDef(className.toTermName, constrTparams, constrVparamss, classTypeRef, creatorExpr)
655655
.withMods(companionMods | Synthetic | Implicit)
656-
.withPos(cdef.pos) :: Nil
656+
.withPosOf(cdef) :: Nil
657657

658658
val self1 = {
659659
val selfType = if (self.tpt.isEmpty) classTypeRef else self.tpt
@@ -710,17 +710,17 @@ object desugar {
710710
val clsRef = Ident(clsName)
711711
val modul = ValDef(moduleName, clsRef, New(clsRef, Nil))
712712
.withMods(mods.toTermFlags & RetainedModuleValFlags | ModuleValCreationFlags)
713-
.withPos(mdef.pos.startPos)
713+
.withSpan(mdef.pos.startPos)
714714
val ValDef(selfName, selfTpt, _) = impl.self
715715
val selfMods = impl.self.mods
716716
if (!selfTpt.isEmpty) ctx.error(ObjectMayNotHaveSelfType(mdef), impl.self.sourcePos)
717717
val clsSelf = ValDef(selfName, SingletonTypeTree(Ident(moduleName)), impl.self.rhs)
718718
.withMods(selfMods)
719-
.withPos(impl.self.pos orElse impl.pos.startPos)
719+
.withSpan(impl.self.pos.orElse(impl.pos.startPos))
720720
val clsTmpl = cpy.Template(impl)(self = clsSelf, body = impl.body)
721721
val cls = TypeDef(clsName, clsTmpl)
722722
.withMods(mods.toTypeFlags & RetainedModuleClassFlags | ModuleClassCreationFlags)
723-
Thicket(modul, classDef(cls).withPos(mdef.pos))
723+
Thicket(modul, classDef(cls).withPosOf(mdef))
724724
}
725725
}
726726

@@ -842,7 +842,7 @@ object desugar {
842842
mods & Lazy | Synthetic | (if (ctx.owner.isClass) PrivateLocal else EmptyFlags)
843843
val firstDef =
844844
ValDef(tmpName, TypeTree(), matchExpr)
845-
.withPos(pat.pos.union(rhs.pos)).withMods(patMods)
845+
.withSpan(pat.pos.union(rhs.pos)).withMods(patMods)
846846
def selector(n: Int) = Select(Ident(tmpName), nme.selectorName(n))
847847
val restDefs =
848848
for (((named, tpt), n) <- vars.zipWithIndex)
@@ -856,7 +856,7 @@ object desugar {
856856
/** Expand variable identifier x to x @ _ */
857857
def patternVar(tree: Tree)(implicit ctx: Context): Bind = {
858858
val Ident(name) = tree
859-
Bind(name, Ident(nme.WILDCARD)).withPos(tree.pos)
859+
Bind(name, Ident(nme.WILDCARD)).withPosOf(tree)
860860
}
861861

862862
def defTree(tree: Tree)(implicit ctx: Context): Tree = tree match {
@@ -879,7 +879,7 @@ object desugar {
879879
def block(tree: Block)(implicit ctx: Context): Block = tree.expr match {
880880
case EmptyTree =>
881881
cpy.Block(tree)(tree.stats,
882-
unitLiteral withPos (if (tree.stats.isEmpty) tree.pos else tree.pos.endPos))
882+
unitLiteral.withSpan(if (tree.stats.isEmpty) tree.pos else tree.pos.endPos))
883883
case _ =>
884884
tree
885885
}
@@ -900,7 +900,7 @@ object desugar {
900900
case Tuple(args) => args.mapConserve(assignToNamedArg)
901901
case _ => arg :: Nil
902902
}
903-
Apply(Select(fn, op.name).withPos(selectPos), args)
903+
Apply(Select(fn, op.name).withSpan(selectPos), args)
904904
}
905905

906906
if (isLeftAssoc(op.name))
@@ -984,7 +984,7 @@ object desugar {
984984
val vdefs =
985985
params.zipWithIndex.map{
986986
case (param, idx) =>
987-
DefDef(param.name, Nil, Nil, TypeTree(), selector(idx)).withPos(param.pos)
987+
DefDef(param.name, Nil, Nil, TypeTree(), selector(idx)).withPosOf(param)
988988
}
989989
Function(param :: Nil, Block(vdefs, body))
990990
}
@@ -1020,15 +1020,15 @@ object desugar {
10201020
private def derivedValDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit ctx: Context) = {
10211021
val vdef = ValDef(named.name.asTermName, tpt, rhs)
10221022
.withMods(mods)
1023-
.withPos(original.pos.withPoint(named.pos.start))
1023+
.withSpan(original.pos.withPoint(named.pos.start))
10241024
val mayNeedSetter = valDef(vdef)
10251025
mayNeedSetter
10261026
}
10271027

10281028
private def derivedDefDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit src: SourceInfo) =
10291029
DefDef(named.name.asTermName, Nil, Nil, tpt, rhs)
10301030
.withMods(mods)
1031-
.withPos(original.pos.withPoint(named.pos.start))
1031+
.withSpan(original.pos.withPoint(named.pos.start))
10321032

10331033
/** Main desugaring method */
10341034
def apply(tree: Tree)(implicit ctx: Context): Tree = {
@@ -1278,7 +1278,7 @@ object desugar {
12781278
finalizer)
12791279
}
12801280
}
1281-
desugared.withPos(tree.pos)
1281+
desugared.withPosOf(tree)
12821282
}
12831283

12841284
/** Create a class definition with the same info as the refined type given by `parent`

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object DesugarEnums {
6060
|type parameter $tparam $problem""", ctx.source.atPos(pos))
6161
}
6262
}
63-
TypeTree(enumClass.typeRef.appliedTo(targs)).withPos(pos)
63+
TypeTree(enumClass.typeRef.appliedTo(targs)).withSpan(pos)
6464
}
6565

6666
/** A type tree referring to `enumClass` */
@@ -207,7 +207,7 @@ object DesugarEnums {
207207
val (tagMeth, scaffolding) = enumTagMeth(CaseKind.Object)
208208
val impl1 = cpy.Template(impl)(body = List(tagMeth, toStringMeth) ++ registerCall)
209209
val vdef = ValDef(name, TypeTree(), New(impl1)).withMods(mods | Final)
210-
flatTree(scaffolding ::: vdef :: Nil).withPos(pos)
210+
flatTree(scaffolding ::: vdef :: Nil).withSpan(pos)
211211
}
212212
}
213213

@@ -223,6 +223,6 @@ object DesugarEnums {
223223
val (tag, scaffolding) = nextEnumTag(CaseKind.Simple)
224224
val creator = Apply(Ident(nme.DOLLAR_NEW), List(Literal(Constant(tag)), Literal(Constant(name.toString))))
225225
val vdef = ValDef(name, enumClassRef, creator).withMods(mods | Final)
226-
flatTree(scaffolding ::: vdef :: Nil).withPos(pos)
226+
flatTree(scaffolding ::: vdef :: Nil).withSpan(pos)
227227
}
228228
}

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
3535
setId(TreeIds.nextIdFor(initialFile(src)))
3636
setPos(initialPos, srcfile)
3737

38-
protected def setId(id: Int): Unit = myUniqueId = id
38+
protected def setId(id: Int): Unit = {
39+
myUniqueId = id
40+
//assert(id != 2067, getClass)
41+
}
3942

4043
/** Destructively update `curPos` to given position. Also, set any missing
4144
* positions in children.
@@ -50,37 +53,32 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
5053
* If the positioned item is synthetic, the position is updated
5154
* destructively and the item itself is returned.
5255
*/
53-
def withPos(pos: Position)(implicit src: SourceInfo): this.type = {
56+
def withSpan(pos: Position): this.type = {
5457
val ownPos = this.pos
5558
val newpd: this.type =
5659
if (pos == ownPos || ownPos.isSynthetic) this else cloneIn(srcfile)
5760
newpd.setPos(pos, srcfile)
5861
newpd
5962
}
6063

61-
def withPosOf(posd: Positioned)(implicit ctx: Context): this.type = {
64+
def withPosOf(posd: Positioned): this.type = {
6265
val ownPos = this.pos
6366
val newpd: this.type =
64-
if (posd.source == source && posd.pos == ownPos || ownPos.isSynthetic) this
67+
if (posd.srcfile == srcfile && posd.pos == ownPos || ownPos.isSynthetic) this
6568
else cloneIn(posd.srcfile)
6669
newpd.setPos(posd.pos, posd.srcfile)
6770
newpd
6871
}
6972

70-
def withSourcePos(sourcePos: SourcePosition)(implicit ctx: Context): this.type = {
73+
def withSourcePos(sourcePos: SourcePosition): this.type = {
7174
val ownPos = this.pos
7275
val newpd: this.type =
73-
if (sourcePos.source == source && sourcePos.pos == ownPos || ownPos.isSynthetic) this
76+
if (sourcePos.source.file == srcfile && sourcePos.pos == ownPos || ownPos.isSynthetic) this
7477
else cloneIn(sourcePos.source.file)
7578
newpd.setPos(sourcePos.pos, sourcePos.source.file)
7679
newpd
7780
}
7881

79-
/** This item with a position that's the union of the given `pos` and the
80-
* current position.
81-
*/
82-
def addPos(pos: Position)(implicit src: SourceInfo): this.type = withPos(pos union this.pos)
83-
8482
/** Set position of this tree only, without performing
8583
* any checks of consistency with - or updates of - other positions.
8684
* Called from Unpickler when entering positions.

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class TreeTypeMap(
8989
case tree1 =>
9090
tree1.withType(mapType(tree1.tpe)) match {
9191
case id: Ident if tpd.needsSelect(id.tpe) =>
92-
ref(id.tpe.asInstanceOf[TermRef]).withPos(id.pos)
92+
ref(id.tpe.asInstanceOf[TermRef]).withPosOf(id)
9393
case ddef @ DefDef(name, tparams, vparamss, tpt, _) =>
9494
val (tmap1, tparams1) = transformDefs(ddef.tparams)
9595
val (tmap2, vparamss1) = tmap1.transformVParamss(vparamss)
@@ -122,7 +122,7 @@ class TreeTypeMap(
122122
val expr1 = tmap.transform(expr)
123123
cpy.Labeled(labeled)(bind1, expr1)
124124
case Hole(n, args) =>
125-
Hole(n, args.mapConserve(transform)).withPos(tree.pos).withType(mapType(tree.tpe))
125+
Hole(n, args.mapConserve(transform)).withPosOf(tree).withType(mapType(tree.tpe))
126126
case tree1 =>
127127
super.transform(tree1)
128128
}

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ object Trees {
8585
/** Copy `tpe` attribute from tree `from` into this tree, independently
8686
* whether it is null or not.
8787
final def copyAttr[U >: Untyped](from: Tree[U]): ThisTree[T] = {
88-
val t1 = this.withPos(from.pos)
88+
val t1 = this.withPosOf(from)
8989
val t2 =
9090
if (from.myTpe != null) t1.withType(from.myTpe.asInstanceOf[Type])
9191
else t1
@@ -783,21 +783,30 @@ object Trees {
783783
case class Thicket[-T >: Untyped](trees: List[Tree[T]])(implicit @transientParam src: SourceInfo)
784784
extends Tree[T] with WithoutTypeOrPos[T] {
785785
myTpe = NoType.asInstanceOf[T]
786-
787786
type ThisTree[-T >: Untyped] = Thicket[T]
788-
override def isEmpty: Boolean = trees.isEmpty
789-
override def toList: List[Tree[T]] = flatten(trees)
790-
override def toString: String = if (isEmpty) "EmptyTree" else "Thicket(" + trees.mkString(", ") + ")"
791-
override def withPos(pos: Position)(implicit src: SourceInfo): this.type = {
792-
val newTrees = trees.mapConserve(_.withPos(pos))
787+
788+
def mapElems(op: Tree[T] => Tree[T] @uncheckedVariance): Thicket[T] = {
789+
val newTrees = trees.mapConserve(op)
793790
if (trees eq newTrees)
794791
this
795792
else
796793
Thicket[T](newTrees).asInstanceOf[this.type]
797794
}
798-
override def pos: Position = (NoPosition /: trees) ((pos, t) => pos union t.pos)
795+
799796
override def foreachInThicket(op: Tree[T] => Unit): Unit =
800797
trees foreach (_.foreachInThicket(op))
798+
799+
override def isEmpty: Boolean = trees.isEmpty
800+
override def toList: List[Tree[T]] = flatten(trees)
801+
override def toString: String = if (isEmpty) "EmptyTree" else "Thicket(" + trees.mkString(", ") + ")"
802+
override def pos: Position = (NoPosition /: trees) ((pos, t) => pos union t.pos)
803+
804+
override def withSpan(pos: Position): this.type =
805+
mapElems(_.withSpan(pos)).asInstanceOf[this.type]
806+
override def withPosOf(posd: Positioned): this.type =
807+
mapElems(_.withPosOf(posd)).asInstanceOf[this.type]
808+
override def withSourcePos(sourcePos: SourcePosition): this.type =
809+
mapElems(_.withSourcePos(sourcePos)).asInstanceOf[this.type]
801810
}
802811

803812
class EmptyTree[T >: Untyped] extends Thicket(Nil)(NoContext) {
@@ -965,10 +974,10 @@ object Trees {
965974
protected def postProcess(tree: Tree, copied: untpd.MemberDef): copied.ThisTree[T]
966975

967976
protected def finalize(tree: Tree, copied: untpd.Tree)(implicit src: SourceInfo): copied.ThisTree[T] =
968-
postProcess(tree, copied.withPos(tree.pos).withAttachmentsFrom(tree))
977+
postProcess(tree, copied.withPosOf(tree).withAttachmentsFrom(tree))
969978

970979
protected def finalize(tree: Tree, copied: untpd.MemberDef)(implicit src: SourceInfo): copied.ThisTree[T] =
971-
postProcess(tree, copied.withPos(tree.pos).withAttachmentsFrom(tree))
980+
postProcess(tree, copied.withPosOf(tree).withAttachmentsFrom(tree))
972981

973982
protected def srcCtx(tree: Tree)(implicit ctx: Context) = ctx.withSource(tree.source)
974983

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
404404
def newArray(elemTpe: Type, returnTpe: Type, pos: Position, dims: JavaSeqLiteral)(implicit ctx: Context): Tree = {
405405
val elemClass = elemTpe.classSymbol
406406
def newArr =
407-
ref(defn.DottyArraysModule).select(defn.newArrayMethod).withPos(pos)
407+
ref(defn.DottyArraysModule).select(defn.newArrayMethod).withSpan(pos)
408408

409409
if (!ctx.erasedTypes) {
410410
assert(!TypeErasure.isGeneric(elemTpe)) //needs to be done during typer. See Applications.convertNewGenericArray
411-
newArr.appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
411+
newArr.appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withSpan(pos)
412412
} else // after erasure
413-
newArr.appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
413+
newArr.appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withSpan(pos)
414414
}
415415

416416
/** The wrapped array method name for an array of type elemtp */
@@ -1012,7 +1012,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10121012
tree
10131013
else {
10141014
ctx.warning(i"conversion from ${tree.tpe.widen} to ${numericCls.typeRef} will always fail at runtime.")
1015-
Throw(New(defn.ClassCastExceptionClass.typeRef, Nil)) withPos tree.pos
1015+
Throw(New(defn.ClassCastExceptionClass.typeRef, Nil)).withPosOf(tree)
10161016
}
10171017
}
10181018

@@ -1204,7 +1204,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12041204
// Give a zero-extent position to the qualifier to prevent it from being included several
12051205
// times in results in the language server.
12061206
val noPosExpr = focusPositions(imp.expr)
1207-
val selectTree = Select(noPosExpr, sym.name).withPos(id.pos)
1207+
val selectTree = Select(noPosExpr, sym.name).withPosOf(id)
12081208
rename match {
12091209
case None =>
12101210
selectTree :: Nil
@@ -1213,7 +1213,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12131213
// node with the new name and the type of the real symbol.
12141214
val name = if (sym.name.isTypeName) rename.name.toTypeName else rename.name
12151215
val actual = Select(noPosExpr, sym.name)
1216-
val renameTree = Select(noPosExpr, name).withPos(rename.pos).withType(actual.tpe)
1216+
val renameTree = Select(noPosExpr, name).withPosOf(rename).withType(actual.tpe)
12171217
selectTree :: renameTree :: Nil
12181218
}
12191219
}
@@ -1236,7 +1236,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12361236
private def focusPositions(tree: Tree)(implicit ctx: Context): Tree = {
12371237
val transformer = new tpd.TreeMap {
12381238
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
1239-
super.transform(tree).withPos(tree.pos.focus)
1239+
super.transform(tree).withSpan(tree.pos.focus)
12401240
}
12411241
}
12421242
transformer.transform(tree)

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
325325
(tycon, targs)
326326
case TypedSplice(tpt1: tpd.Tree) =>
327327
val argTypes = tpt1.tpe.dealias.argTypesLo
328-
def wrap(tpe: Type) = TypeTree(tpe).withPos(tpt.pos)
328+
def wrap(tpe: Type) = TypeTree(tpe).withPosOf(tpt)
329329
(tpt, argTypes.map(wrap))
330330
case _ =>
331331
(tpt, Nil)

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ object PickledQuotes {
140140
new TreeTypeMap(
141141
oldOwners = ddef.symbol :: Nil,
142142
newOwners = ctx.owner :: Nil,
143-
treeMap = tree => paramToVals.get(tree.symbol).map(_.withPos(tree.pos)).getOrElse(tree)
143+
treeMap = tree => paramToVals.get(tree.symbol).map(_.withPosOf(tree)).getOrElse(tree)
144144
).transform(ddef.rhs)
145145
case Block(stats, expr) =>
146146
seq(stats, rec(expr))

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ class TreeUnpickler(reader: TastyReader,
10321032

10331033
def readQualId(): (untpd.Ident, TypeRef) = {
10341034
val qual = readTerm().asInstanceOf[untpd.Ident]
1035-
(untpd.Ident(qual.name).withPos(qual.pos), qual.tpe.asInstanceOf[TypeRef])
1035+
(untpd.Ident(qual.name).withPosOf(qual), qual.tpe.asInstanceOf[TypeRef])
10361036
}
10371037

10381038
def accessibleDenot(pre: Type, name: Name, sig: Signature) = {

0 commit comments

Comments
 (0)