Skip to content

Commit 63765a7

Browse files
committed
Reduce explicit uses of sourcePos
1 parent 9131a08 commit 63765a7

29 files changed

+214
-189
lines changed

compiler/src/dotty/tools/backend/jvm/CollectEntryPoints.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ object CollectEntryPoints{
4747
def hasJavaMainMethod(sym: Symbol): Boolean =
4848
(toDenot(sym).info member nme.main).alternatives exists(x => isJavaMainMethod(x.symbol))
4949

50-
def fail(msg: String, pos: Position = sym.pos) = {
51-
ctx.warning( sym.name +
52-
s" has a main method with parameter type Array[String], but ${toDenot(sym).fullName} will not be a runnable program.\n Reason: $msg",
53-
sourcePos(sym.pos)
50+
def fail(msg: String, posInfo: PosInfo = sym) = {
51+
ctx.warning(
52+
i"""${sym.name} has a main method with parameter type Array[String], but ${sym.fullName} will not be a runnable program.
53+
|Reason: $msg""", sym.sourcePos
5454
// TODO: make this next claim true, if possible
5555
// by generating valid main methods as static in module classes
5656
// not sure what the jvm allows here
@@ -92,11 +92,11 @@ object CollectEntryPoints{
9292
fail("main methods cannot be generic.")
9393
case MethodTpe(paramNames, paramTypes, resultType) =>
9494
if (resultType :: paramTypes exists (_.typeSymbol.isAbstractType))
95-
fail("main methods cannot refer to type parameters or abstract types.", m.symbol.pos)
95+
fail("main methods cannot refer to type parameters or abstract types.", m.symbol)
9696
else
97-
isJavaMainMethod(m.symbol) || fail("main method must have exact signature (Array[String])Unit", m.symbol.pos)
97+
isJavaMainMethod(m.symbol) || fail("main method must have exact signature (Array[String])Unit", m.symbol)
9898
case tp =>
99-
fail(s"don't know what this is: $tp", m.symbol.pos)
99+
fail(s"don't know what this is: $tp", m.symbol)
100100
}
101101
}
102102
}

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Types._
2121
import Symbols._
2222
import Phases._
2323

24-
import dotty.tools.dotc.util.Positions
24+
import dotty.tools.dotc.util.{SourcePosition, NoSourcePosition}
2525
import Decorators._
2626
import tpd._
2727

@@ -41,7 +41,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
4141
type CompilationUnit = dotc.CompilationUnit
4242
type Constant = Constants.Constant
4343
type Literal = tpd.Literal
44-
type Position = Positions.Position
44+
type Position = SourcePosition
4545
type Name = Names.Name
4646
type ClassDef = tpd.TypeDef
4747
type TypeDef = tpd.TypeDef
@@ -77,7 +77,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
7777
type Closure = tpd.Closure
7878

7979
val NoSymbol: Symbol = Symbols.NoSymbol
80-
val NoPosition: Position = Positions.NoPosition
80+
val NoPosition: Position = NoSourcePosition
8181
val EmptyTree: Tree = tpd.EmptyTree
8282

8383

@@ -380,12 +380,11 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
380380
ctx.requiredModule(className)
381381
}
382382

383-
384383
def debuglog(msg: => String): Unit = ctx.debuglog(msg)
385384
def informProgress(msg: String): Unit = ctx.informProgress(msg)
386385
def log(msg: => String): Unit = ctx.log(msg)
387-
def error(pos: Position, msg: String): Unit = ctx.error(msg, sourcePos(pos))
388-
def warning(pos: Position, msg: String): Unit = ctx.warning(msg, sourcePos(pos))
386+
def error(pos: SourcePosition, msg: String): Unit = ctx.error(msg, pos)
387+
def warning(pos: SourcePosition, msg: String): Unit = ctx.warning(msg, pos)
389388
def abort(msg: String): Nothing = {
390389
ctx.error(msg)
391390
throw new RuntimeException(msg)
@@ -563,7 +562,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
563562

564563
implicit def positionHelper(a: Position): PositionHelper = new PositionHelper {
565564
def isDefined: Boolean = a.exists
566-
def line: Int = sourcePos(a).line + 1
565+
def line: Int = a.line + 1
567566
def finalPosition: Position = a
568567
}
569568

@@ -587,7 +586,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
587586
implicit def treeHelper(a: Tree): TreeHelper = new TreeHelper {
588587
def symbol: Symbol = a.symbol
589588

590-
def pos: Position = a.pos
589+
def pos: Position = a.sourcePos
591590

592591
def isEmpty: Boolean = a.isEmpty
593592

@@ -801,15 +800,15 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
801800

802801

803802
def freshLocal(cunit: CompilationUnit, name: String, tpe: Type, pos: Position, flags: Flags): Symbol = {
804-
ctx.newSymbol(sym, name.toTermName, termFlagSet(flags), tpe, NoSymbol, pos)
803+
ctx.newSymbol(sym, name.toTermName, termFlagSet(flags), tpe, NoSymbol, pos.pos)
805804
}
806805

807806
def getter(clz: Symbol): Symbol = decorateSymbol(sym).getter
808807
def setter(clz: Symbol): Symbol = decorateSymbol(sym).setter
809808

810809
def moduleSuffix: String = "" // todo: validate that names already have $ suffix
811810
def outputDirectory: AbstractFile = DottyBackendInterface.this.outputDirectory
812-
def pos: Position = sym.pos
811+
def pos: Position = sym.sourcePos
813812

814813
def throwsAnnotations: List[Symbol] = Nil
815814

@@ -1105,7 +1104,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
11051104
def _1: Type = field.tpe match {
11061105
case JavaArrayType(elem) => elem
11071106
case _ =>
1108-
error(field.pos, s"JavaSeqArray with type ${field.tpe} reached backend: $field")
1107+
error(field.sourcePos, s"JavaSeqArray with type ${field.tpe} reached backend: $field")
11091108
UnspecifiedErrorType
11101109
}
11111110
def _2: List[Tree] = field.elems

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.withPos(cdef.namePos))
354+
ctx.error(CaseClassMissingParamList(cdef), cdef.sourcePos.withRange(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.withPos(cdef.namePos))
357+
ctx.error("Case classes should have a non-implicit parameter list", cdef.sourcePos.withRange(cdef.namePos))
358358
ListOfNil
359359
}
360360
else originalVparamss.nestedMap(toDefParam)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object DesugarEnums {
5757
if (tparam.variance == 0) "is non variant"
5858
else "has bounds that depend on a type parameter in the same parameter list"
5959
errorType(i"""cannot determine type argument for enum parent $enumClass,
60-
|type parameter $tparam $problem""", pos)
60+
|type parameter $tparam $problem""", ctx.source.atPos(pos))
6161
}
6262
}
6363
TypeTree(enumClass.typeRef.appliedTo(targs)).withPos(pos)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.dotc
22
package ast
33

44
import util.Positions._
5+
import util.SourcePosition
56
import core.Contexts.{Context, SourceInfo}
67
import core.Decorators._
78
import core.Flags.{JavaDefined, Extension}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import printing.Printer
1111
import printing.Texts.Text
1212
import util.{Stats, Attachment, Property, SourceFile, SourcePosition}
1313
import config.Config
14+
import io.AbstractFile
1415
import annotation.internal.sharable
1516
import annotation.unchecked.uncheckedVariance
1617
import annotation.transientParam
@@ -53,6 +54,7 @@ object Trees {
5354
abstract class Tree[-T >: Untyped](implicit @transientParam src: SourceInfo)
5455
extends Positioned
5556
with Product
57+
with PosInfo
5658
with Attachment.Container
5759
with printing.Showable
5860
with Cloneable {
@@ -66,7 +68,9 @@ object Trees {
6668

6769
def uniqueId: Int = myUniqueId
6870

69-
def source(implicit ctx: Context): SourceFile = ctx.sourceOfTreeId(uniqueId)
71+
def sourceFile(implicit ctx: Context): AbstractFile = TreeIds.fileOfId(uniqueId)
72+
73+
def source(implicit ctx: Context): SourceFile = ctx.getSource(sourceFile)
7074

7175
def sourcePos(implicit ctx: Context): SourcePosition = {
7276
val src = source

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ object Comments {
125125
val newName = ctx.freshNames.newName(tree.name, NameKinds.DocArtifactName)
126126
tree.copy(name = newName)
127127
case _ =>
128-
ctx.error(ProperDefinitionNotFound(), sourcePos(codePos))
128+
ctx.error(ProperDefinitionNotFound(), ctx.source.atPos(codePos))
129129
tree
130130
}
131131
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dotty.tools
2+
package dotc
3+
package core
4+
5+
import Contexts.Context
6+
import util.SourcePosition
7+
import util.Positions.Position
8+
import io.AbstractFile
9+
10+
trait PosInfo {
11+
def sourceFile(implicit ctx: Context): AbstractFile
12+
def pos: Position
13+
def sourcePos(implicit ctx: Context): SourcePosition
14+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import DenotTransformers._
1818
import StdNames._
1919
import NameOps._
2020
import NameKinds.LazyImplicitName
21-
import ast.tpd
21+
import ast.{tpd, Positioned}
2222
import tpd.{Tree, TreeProvider, TreeOps}
2323
import ast.TreeTypeMap
2424
import Constants.Constant
@@ -404,7 +404,8 @@ object Symbols {
404404
* @param coord The coordinates of the symbol (a position or an index)
405405
* @param id A unique identifier of the symbol (unique per ContextBase)
406406
*/
407-
class Symbol private[Symbols] (private[this] var myCoord: Coord, val id: Int) extends Designator with ParamInfo with printing.Showable {
407+
class Symbol private[Symbols] (private[this] var myCoord: Coord, val id: Int)
408+
extends Designator with ParamInfo with PosInfo with printing.Showable {
408409

409410
type ThisName <: Name
410411

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package core
55
import Contexts._, Types._, Symbols._, Names._, Flags._
66
import SymDenotations._
77
import util.Positions._
8+
import util.SourcePosition
89
import NameKinds.DepParamName
910
import Decorators._
1011
import StdNames._
@@ -320,9 +321,9 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
320321
def dynamicsEnabled: Boolean =
321322
featureEnabled(defn.LanguageModuleClass, nme.dynamics)
322323

323-
def testScala2Mode(msg: => Message, pos: Position, replace: => Unit = ()): Boolean = {
324+
def testScala2Mode(msg: => Message, pos: SourcePosition, replace: => Unit = ()): Boolean = {
324325
if (scala2Mode) {
325-
migrationWarning(msg, sourcePos(pos))
326+
migrationWarning(msg, pos)
326327
replace
327328
}
328329
scala2Mode

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ class TreeUnpickler(reader: TastyReader,
850850
goto(end)
851851
setPos(start, tree)
852852
if (!sym.isType) { // Only terms might have leaky aliases, see the documentation of `checkNoPrivateLeaks`
853-
sym.info = ta.avoidPrivateLeaks(sym, tree.pos)
853+
sym.info = ta.avoidPrivateLeaks(sym, tree)
854854
}
855855

856856
sym.defTree = tree

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object Parsers {
103103
if (in.token == BACKQUOTED_IDENT) in.offset + 1 else in.offset
104104

105105
def sourcePos(off: Int = in.offset): SourcePosition =
106-
source atPos Position(off)
106+
source.atPos(Position(off))
107107

108108
/* ------------- ERROR HANDLING ------------------------------------------- */
109109
/** The offset where the last syntax error was reported, or if a skip to a

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
643643
val str: Text = nameString(tree.symbol)
644644
tree match {
645645
case tree: RefTree => withPos(str, tree.sourcePos)
646-
case tree: MemberDef => withPos(str, tree.sourcePos.withPos(tree.namePos))
646+
case tree: MemberDef => withPos(str, tree.sourcePos.withRange(tree.namePos))
647647
case _ => str
648648
}
649649
}

compiler/src/dotty/tools/dotc/transform/CollectEntryPoints.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class CollectEntryPoints extends MiniPhase {
3838
}
3939

4040
def isJavaEntryPoint(sym: Symbol)(implicit ctx: Context): Boolean = {
41-
def fail(msg: String, pos: Position = sym.pos) = {
42-
ctx.warning(sym.name +
43-
s" has a main method with parameter type Array[String], but ${sym.fullName} will not be a runnable program.\n Reason: $msg",
44-
sourcePos(sym.pos)
41+
def fail(msg: String, posInfo: PosInfo = sym) = {
42+
ctx.warning(
43+
i"""${sym.name} has a main method with parameter type Array[String], but ${sym.fullName} will not be a runnable program.
44+
|Reason: $msg""", sym.sourcePos
4545
// TODO: make this next claim true, if possible
4646
// by generating valid main methods as static in module classes
4747
// not sure what the jvm allows here
@@ -80,11 +80,11 @@ class CollectEntryPoints extends MiniPhase {
8080
fail("main methods cannot be generic.")
8181
case t: MethodType =>
8282
if (t.resultType :: t.paramInfos exists (_.typeSymbol.isAbstractType))
83-
fail("main methods cannot refer to type parameters or abstract types.", m.symbol.pos)
83+
fail("main methods cannot refer to type parameters or abstract types.", m.symbol)
8484
else
85-
ctx.platform.isMainMethod(m.symbol) || fail("main method must have exact signature (Array[String])Unit", m.symbol.pos)
85+
ctx.platform.isMainMethod(m.symbol) || fail("main method must have exact signature (Array[String])Unit", m.symbol)
8686
case tp =>
87-
fail(s"don't know what this is: $tp", m.symbol.pos)
87+
fail(s"don't know what this is: $tp", m.symbol)
8888
}
8989
}
9090
}

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ object PatternMatcher {
301301
else {
302302
letAbstract(unapp) { unappResult =>
303303
val isUnapplySeq = unapp.symbol.name == nme.unapplySeq
304-
if (isProductMatch(unapp.tpe.widen, args.length) && !isUnapplySeq) {
304+
if (isProductMatch(unapp.tpe.widen, args.length, unapp) && !isUnapplySeq) {
305305
val selectors = productSelectors(unapp.tpe).take(args.length)
306306
.map(ref(unappResult).select(_))
307307
matchArgsPlan(selectors, args, onSuccess)
308308
}
309309
else {
310-
assert(isGetMatch(unapp.tpe))
310+
assert(isGetMatch(unapp.tpe, unapp))
311311
val argsPlan = {
312312
val get = ref(unappResult).select(nme.get, _.info.isParameterless)
313313
if (isUnapplySeq)

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
219219
case Select(nu: New, nme.CONSTRUCTOR) if isCheckable(nu) =>
220220
// need to check instantiability here, because the type of the New itself
221221
// might be a type constructor.
222-
Checking.checkInstantiable(tree.tpe, nu.pos)
222+
Checking.checkInstantiable(tree.tpe, nu)
223223
withNoCheckNews(nu :: Nil)(super.transform(app))
224224
case _ =>
225225
super.transform(app)
@@ -266,7 +266,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
266266
}
267267
processMemberDef(super.transform(tree))
268268
case tree: New if isCheckable(tree) =>
269-
Checking.checkInstantiable(tree.tpe, tree.pos)
269+
Checking.checkInstantiable(tree.tpe, tree)
270270
super.transform(tree)
271271
case tree: Closure if !tree.tpt.isEmpty =>
272272
Checking.checkRealizable(tree.tpt.tpe, tree.pos, "SAM type")
@@ -289,7 +289,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
289289
case tree: AndTypeTree =>
290290
// Ideally, this should be done by Typer, but we run into cyclic references
291291
// when trying to typecheck self types which are intersections.
292-
Checking.checkNonCyclicInherited(tree.tpe, tree.left.tpe :: tree.right.tpe :: Nil, EmptyScope, tree.pos)
292+
Checking.checkNonCyclicInherited(tree.tpe, tree.left.tpe :: tree.right.tpe :: Nil, EmptyScope, tree)
293293
super.transform(tree)
294294
case tree: LambdaTypeTree =>
295295
VarianceChecker.checkLambda(tree)

0 commit comments

Comments
 (0)