Skip to content

Commit 4beb03a

Browse files
authored
Merge pull request #9561 from dotty-staging/simplify-positions
Optimize and simplify SourcePosition handling
2 parents 17c5dc1 + 70d76ea commit 4beb03a

Some content is hidden

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

62 files changed

+485
-480
lines changed

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ object desugar {
533533
appliedRef(enumClassRef)
534534
else {
535535
report.error(TypedCaseDoesNotExplicitlyExtendTypedEnum(enumClass, cdef)
536-
, cdef.sourcePos.startPos)
536+
, cdef.srcPos.startPos)
537537
appliedTypeTree(enumClassRef, constrTparams map (_ => anyRef))
538538
}
539539

@@ -633,7 +633,7 @@ object desugar {
633633
.withSpan(cdef.span).toList
634634
if (companionDerived.nonEmpty)
635635
for (modClsDef @ TypeDef(_, _) <- mdefs)
636-
modClsDef.putAttachment(DerivingCompanion, impl.sourcePos.startPos)
636+
modClsDef.putAttachment(DerivingCompanion, impl.srcPos.startPos)
637637
mdefs
638638
}
639639

@@ -741,19 +741,19 @@ object desugar {
741741
if (!mods.isOneOf(GivenOrImplicit))
742742
Nil
743743
else if (ctx.owner.is(Package)) {
744-
report.error(TopLevelImplicitClass(cdef), cdef.sourcePos)
744+
report.error(TopLevelImplicitClass(cdef), cdef.srcPos)
745745
Nil
746746
}
747747
else if (mods.is(Trait)) {
748-
report.error(TypesAndTraitsCantBeImplicit(), cdef.sourcePos)
748+
report.error(TypesAndTraitsCantBeImplicit(), cdef.srcPos)
749749
Nil
750750
}
751751
else if (isCaseClass) {
752-
report.error(ImplicitCaseClass(cdef), cdef.sourcePos)
752+
report.error(ImplicitCaseClass(cdef), cdef.srcPos)
753753
Nil
754754
}
755755
else if (arity != 1 && !mods.is(Given)) {
756-
report.error(ImplicitClassPrimaryConstructorArity(), cdef.sourcePos)
756+
report.error(ImplicitClassPrimaryConstructorArity(), cdef.srcPos)
757757
Nil
758758
}
759759
else {
@@ -895,7 +895,7 @@ object desugar {
895895
.withSpan(mdef.span.startPos)
896896
val ValDef(selfName, selfTpt, _) = impl.self
897897
val selfMods = impl.self.mods
898-
if (!selfTpt.isEmpty) report.error(ObjectMayNotHaveSelfType(mdef), impl.self.sourcePos)
898+
if (!selfTpt.isEmpty) report.error(ObjectMayNotHaveSelfType(mdef), impl.self.srcPos)
899899
val clsSelf = ValDef(selfName, SingletonTypeTree(Ident(moduleName)), impl.self.rhs)
900900
.withMods(selfMods)
901901
.withSpan(impl.self.span.orElse(impl.span.startPos))
@@ -911,7 +911,7 @@ object desugar {
911911
for mdef <- ext.methods yield
912912
if mdef.tparams.nonEmpty then
913913
report.error("extension method cannot have type parameters here, all type parameters go after `extension`",
914-
mdef.tparams.head.sourcePos)
914+
mdef.tparams.head.srcPos)
915915
defDef(
916916
cpy.DefDef(mdef)(
917917
name = mdef.name.toExtensionName,
@@ -1015,7 +1015,7 @@ object desugar {
10151015
case Some(DefDef(name, _, (vparam :: _) :: _, _, _)) =>
10161016
s"extension_${name}_${inventTypeName(vparam.tpt)}"
10171017
case _ =>
1018-
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.sourcePos)
1018+
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.srcPos)
10191019
nme.ERROR.toString
10201020
else
10211021
impl.parents.map(inventTypeName(_)).mkString("given_", "_", "")
@@ -1189,7 +1189,7 @@ object desugar {
11891189
var tested: MemberDef = tree
11901190
def checkApplicable(flag: Flag, test: MemberDefTest): Unit =
11911191
if (tested.mods.is(flag) && !test.applyOrElse(tree, (md: MemberDef) => false)) {
1192-
report.error(ModifierNotAllowedForDefinition(flag), tree.sourcePos)
1192+
report.error(ModifierNotAllowedForDefinition(flag), tree.srcPos)
11931193
tested = tested.withMods(tested.mods.withoutFlags(flag))
11941194
}
11951195
checkApplicable(Opaque, legalOpaque)
@@ -1701,7 +1701,7 @@ object desugar {
17011701
|This can be achieved by adding the import clause 'import scala.language.postfixOps'
17021702
|or by setting the compiler option -language:postfixOps.
17031703
|See the Scaladoc for value scala.language.postfixOps for a discussion
1704-
|why the feature needs to be explicitly enabled.""".stripMargin, t.sourcePos)
1704+
|why the feature needs to be explicitly enabled.""".stripMargin, t.srcPos)
17051705
}
17061706
Select(t, op.name)
17071707
}
@@ -1823,7 +1823,7 @@ object desugar {
18231823
elems foreach collect
18241824
case Alternative(trees) =>
18251825
for (tree <- trees; (vble, _) <- getVariables(tree))
1826-
report.error(IllegalVariableInPatternAlternative(), vble.sourcePos)
1826+
report.error(IllegalVariableInPatternAlternative(), vble.srcPos)
18271827
case Annotated(arg, _) =>
18281828
collect(arg)
18291829
case InterpolatedString(_, segments) =>
@@ -1846,7 +1846,7 @@ object desugar {
18461846
def traverse(tree: untpd.Tree)(using Context): Unit = tree match {
18471847
case Splice(expr) => collect(expr)
18481848
case TypSplice(expr) =>
1849-
report.error(TypeSpliceInValPattern(expr), tree.sourcePos)
1849+
report.error(TypeSpliceInValPattern(expr), tree.srcPos)
18501850
case _ => traverseChildren(tree)
18511851
}
18521852
}.traverse(expr)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ object DesugarEnums {
217217
case Ident(name) =>
218218
val matches = tparamNames.contains(name)
219219
if (matches && (caseTypeParams.nonEmpty || vparamss.isEmpty))
220-
report.error(i"illegal reference to type parameter $name from enum case", tree.sourcePos)
220+
report.error(i"illegal reference to type parameter $name from enum case", tree.srcPos)
221221
matches
222222
case LambdaTypeTree(lambdaParams, body) =>
223223
underBinders(lambdaParams, foldOver(x, tree))

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package ast
44

55
import util.Spans._
6-
import util.{SourceFile, NoSource, SourcePosition}
6+
import util.{SourceFile, NoSource, SourcePosition, SrcPos}
77
import core.Contexts._
88
import core.Decorators._
99
import core.Flags.{JavaDefined, Extension}
@@ -17,7 +17,7 @@ import java.io.{ PrintWriter }
1717

1818
/** A base class for things that have positions (currently: modifiers and trees)
1919
*/
20-
abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Product with Cloneable {
20+
abstract class Positioned(implicit @constructorOnly src: SourceFile) extends SrcPos, Product, Cloneable {
2121

2222
private var myUniqueId: Int = _
2323
private var mySpan: Span = _
@@ -46,7 +46,12 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
4646
span = envelope(src)
4747

4848
def source: SourceFile = SourceFile.fromId(uniqueId)
49-
def sourcePos: SourcePosition = source.atSpan(span)
49+
def sourcePos(using Context): SourcePosition = source.atSpan(span)
50+
51+
/** This positioned item, widened to `SrcPos`. Used to make clear we only need the
52+
* position, typically for error reporting.
53+
*/
54+
final def srcPos: SrcPos = this
5055

5156
/** A positioned item like this one with given `span`.
5257
* If the positioned item is source-derived, a clone is returned.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class TreeMapWithImplicits extends tpd.TreeMap {
121121
}
122122
catch {
123123
case ex: TypeError =>
124-
report.error(ex, tree.sourcePos)
124+
report.error(ex, tree.srcPos)
125125
tree
126126
}
127127
}

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import language.higherKinds
1010
import collection.mutable.ListBuffer
1111
import printing.Printer
1212
import printing.Texts.Text
13-
import util.{Stats, Attachment, Property, SourceFile, NoSource, SourcePosition}
13+
import util.{Stats, Attachment, Property, SourceFile, NoSource, SrcPos, SourcePosition}
1414
import config.Config
1515
import annotation.internal.sharable
1616
import annotation.unchecked.uncheckedVariance
@@ -54,18 +54,10 @@ object Trees {
5454
* nodes.
5555
*/
5656
abstract class Tree[-T >: Untyped](implicit @constructorOnly src: SourceFile)
57-
extends Positioned
58-
with Product
59-
with Attachment.Container
60-
with printing.Showable {
57+
extends Positioned, SrcPos, Product, Attachment.Container, printing.Showable {
6158

6259
if (Stats.enabled) ntrees += 1
6360

64-
/** This tree, widened to `Positioned`. Used to make clear we only need the
65-
* position, typically for error reporting.
66-
*/
67-
final def posd: Positioned = this
68-
6961
/** The type constructor at the root of the tree */
7062
type ThisTree[T >: Untyped] <: Tree[T]
7163

compiler/src/dotty/tools/dotc/config/Feature.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import core._
66
import Contexts._, Symbols._, Names._, NameOps._, Phases._
77
import StdNames.nme
88
import Decorators.{given _}
9-
import util.SourcePosition
9+
import util.SrcPos
1010
import SourceVersion._
1111
import reporting.Message
1212

@@ -76,7 +76,7 @@ object Feature:
7676
/** If current source migrates to `version`, issue given warning message
7777
* and return `true`, otherwise return `false`.
7878
*/
79-
def warnOnMigration(msg: Message, pos: SourcePosition,
79+
def warnOnMigration(msg: Message, pos: SrcPos,
8080
version: SourceVersion = defaultSourceVersion)(using Context): Boolean =
8181
if sourceVersion.isMigrating && sourceVersion.stable == version
8282
|| version == `3.0` && migrateTo3

compiler/src/dotty/tools/dotc/core/Decorators.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package core
55
import annotation.tailrec
66
import Symbols._
77
import Contexts._, Names._, Phases._, printing.Texts._, printing.Printer
8-
import util.Spans.Span, util.SourcePosition
8+
import util.Spans.Span
99
import collection.mutable.ListBuffer
1010
import dotty.tools.dotc.transform.MegaPhase
1111
import ast.tpd._

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2508,7 +2508,7 @@ object SymDenotations {
25082508
def complete(denot: SymDenotation)(using Context): Unit = {
25092509
val sym = denot.symbol
25102510
val errMsg = BadSymbolicReference(denot)
2511-
report.error(errMsg, sym.sourcePos)
2511+
report.error(errMsg, sym.srcPos)
25122512
if (ctx.debug) throw new scala.Error()
25132513
initializeToDefaults(denot, errMsg)
25142514
}

compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ object SymbolLoaders {
134134
report.warning(i"""$what ${tree.name} is in the wrong directory.
135135
|It was declared to be in package ${path.reverse.mkString(".")}
136136
|But it is found in directory ${filePath.reverse.mkString(File.separator)}""",
137-
tree.sourcePos.focus)
137+
tree.srcPos.focus)
138138
ok
139139
}
140140

compiler/src/dotty/tools/dotc/core/Symbols.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import reporting.Message
3030
import collection.mutable
3131
import io.AbstractFile
3232
import language.implicitConversions
33-
import util.{SourceFile, NoSource, Property, SourcePosition}
33+
import util.{SourceFile, NoSource, Property, SourcePosition, SrcPos}
3434
import scala.collection.JavaConverters._
3535
import scala.annotation.internal.sharable
3636
import config.Printers.typr
@@ -47,7 +47,7 @@ object Symbols {
4747
* @param id A unique identifier of the symbol (unique per ContextBase)
4848
*/
4949
class Symbol private[Symbols] (private var myCoord: Coord, val id: Int)
50-
extends Designator with ParamInfo with printing.Showable {
50+
extends Designator, ParamInfo, SrcPos, printing.Showable {
5151

5252
type ThisName <: Name
5353

@@ -321,6 +321,11 @@ object Symbols {
321321
(if (src.exists) src else ctx.source).atSpan(span)
322322
}
323323

324+
/** This positioned item, widened to `SrcPos`. Used to make clear we only need the
325+
* position, typically for error reporting.
326+
*/
327+
final def srcPos: SrcPos = this
328+
324329
// ParamInfo types and methods
325330
def isTypeParam(using Context): Boolean = denot.is(TypeParam)
326331
def paramName(using Context): ThisName = name.asInstanceOf[ThisName]

compiler/src/dotty/tools/dotc/core/TypeOps.scala

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Contexts._, Types._, Symbols._, Names._, Flags._
66
import SymDenotations._
77
import util.Spans._
88
import util.Stats
9-
import util.SourcePosition
109
import NameKinds.DepParamName
1110
import Decorators._
1211
import StdNames._

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class TreePickler(pickler: TastyPickler) {
8484
// I believe it's a bug in typer: the type of an implicit argument refers
8585
// to a closure parameter outside the closure itself. TODO: track this down, so that we
8686
// can eliminate this case.
87-
report.log(i"pickling reference to as yet undefined $sym in ${sym.owner}", sym.sourcePos)
87+
report.log(i"pickling reference to as yet undefined $sym in ${sym.owner}", sym.srcPos)
8888
pickleForwardSymRef(sym)
8989
}
9090

@@ -622,7 +622,7 @@ class TreePickler(pickler: TastyPickler) {
622622
}
623623
catch {
624624
case ex: TypeError =>
625-
report.error(ex.toMessage, tree.sourcePos.focus)
625+
report.error(ex.toMessage, tree.srcPos.focus)
626626
case ex: AssertionError =>
627627
println(i"error when pickling tree $tree")
628628
throw ex
@@ -734,7 +734,7 @@ class TreePickler(pickler: TastyPickler) {
734734

735735
def pickle(trees: List[Tree])(using Context): Unit = {
736736
trees.foreach(tree => if (!tree.isEmpty) pickleTree(tree))
737-
def missing = forwardSymRefs.keysIterator.map(sym => sym.showLocated + "(line " + sym.sourcePos.line + ")").toList
737+
def missing = forwardSymRefs.keysIterator.map(sym => sym.showLocated + "(line " + sym.srcPos.line + ")").toList
738738
assert(forwardSymRefs.isEmpty, i"unresolved symbols: $missing%, % when pickling ${ctx.source}")
739739
}
740740

0 commit comments

Comments
 (0)