Skip to content

Commit ec723f4

Browse files
committed
Give positions to synthetic annotation trees.
This seems like a good idea in general and is required to enforce the added invariant in the previous commit that all pickled TypeTrees have a position.
1 parent 596e017 commit ec723f4

File tree

10 files changed

+52
-51
lines changed

10 files changed

+52
-51
lines changed

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,34 +80,34 @@ object Annotations {
8080

8181
def apply(tree: Tree) = ConcreteAnnotation(tree)
8282

83-
def apply(cls: ClassSymbol)(implicit ctx: Context): Annotation =
84-
apply(cls, Nil)
83+
def apply(cls: ClassSymbol, pos: Position)(implicit ctx: Context): Annotation =
84+
apply(cls, Nil, pos)
8585

86-
def apply(cls: ClassSymbol, arg: Tree)(implicit ctx: Context): Annotation =
87-
apply(cls, arg :: Nil)
86+
def apply(cls: ClassSymbol, arg: Tree, pos: Position)(implicit ctx: Context): Annotation =
87+
apply(cls, arg :: Nil, pos)
8888

89-
def apply(cls: ClassSymbol, arg1: Tree, arg2: Tree)(implicit ctx: Context): Annotation =
90-
apply(cls, arg1 :: arg2 :: Nil)
89+
def apply(cls: ClassSymbol, arg1: Tree, arg2: Tree, pos: Position)(implicit ctx: Context): Annotation =
90+
apply(cls, arg1 :: arg2 :: Nil, pos)
9191

92-
def apply(cls: ClassSymbol, args: List[Tree])(implicit ctx: Context): Annotation =
93-
apply(cls.typeRef, args)
92+
def apply(cls: ClassSymbol, args: List[Tree], pos: Position)(implicit ctx: Context): Annotation =
93+
apply(cls.typeRef, args, pos)
9494

95-
def apply(atp: Type, arg: Tree)(implicit ctx: Context): Annotation =
96-
apply(atp, arg :: Nil)
95+
def apply(atp: Type, arg: Tree, pos: Position)(implicit ctx: Context): Annotation =
96+
apply(atp, arg :: Nil, pos)
9797

98-
def apply(atp: Type, arg1: Tree, arg2: Tree)(implicit ctx: Context): Annotation =
99-
apply(atp, arg1 :: arg2 :: Nil)
98+
def apply(atp: Type, arg1: Tree, arg2: Tree, pos: Position)(implicit ctx: Context): Annotation =
99+
apply(atp, arg1 :: arg2 :: Nil, pos)
100100

101-
def apply(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
102-
apply(New(atp, args))
101+
def apply(atp: Type, args: List[Tree], pos: Position)(implicit ctx: Context): Annotation =
102+
apply(New(atp, args).withPos(pos))
103103

104104
private def resolveConstructor(atp: Type, args:List[Tree])(implicit ctx: Context): Tree = {
105105
val targs = atp.argTypes
106106
tpd.applyOverloaded(New(atp.typeConstructor), nme.CONSTRUCTOR, args, targs, atp, isAnnotConstructor = true)
107107
}
108108

109-
def applyResolve(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation = {
110-
apply(resolveConstructor(atp, args))
109+
def applyResolve(atp: Type, args: List[Tree], pos: Position)(implicit ctx: Context): Annotation = {
110+
apply(resolveConstructor(atp, args).withPos(pos))
111111
}
112112

113113
/** Create an annotation where the tree is computed lazily. */
@@ -132,30 +132,30 @@ object Annotations {
132132
def complete(implicit ctx: Context) = treeFn(ctx)
133133
}
134134

135-
def deferred(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
136-
deferred(atp.classSymbol, implicit ctx => New(atp, args))
135+
def deferred(atp: Type, args: List[Tree], pos: Position)(implicit ctx: Context): Annotation =
136+
deferred(atp.classSymbol, implicit ctx => New(atp, args).withPos(pos))
137137

138-
def deferredResolve(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
139-
deferred(atp.classSymbol, implicit ctx => resolveConstructor(atp, args))
138+
def deferredResolve(atp: Type, args: List[Tree], pos: Position)(implicit ctx: Context): Annotation =
139+
deferred(atp.classSymbol, implicit ctx => resolveConstructor(atp, args).withPos(pos))
140140

141-
def makeAlias(sym: TermSymbol)(implicit ctx: Context) =
141+
def makeAlias(sym: TermSymbol, pos: Position)(implicit ctx: Context) =
142142
apply(defn.AliasAnnot, List(
143-
ref(TermRef(sym.owner.thisType, sym.name, sym))))
143+
ref(TermRef(sym.owner.thisType, sym.name, sym))), pos)
144144

145145
/** Extractor for child annotations */
146146
object Child {
147147

148148
/** A deferred annotation to the result of a given child computation */
149-
def apply(delayedSym: Context => Symbol)(implicit ctx: Context): Annotation = {
149+
def apply(delayedSym: Context => Symbol, pos: Position)(implicit ctx: Context): Annotation = {
150150
def makeChildLater(implicit ctx: Context) = {
151151
val sym = delayedSym(ctx)
152-
New(defn.ChildAnnotType.appliedTo(sym.owner.thisType.select(sym.name, sym)), Nil)
152+
New(defn.ChildAnnotType.appliedTo(sym.owner.thisType.select(sym.name, sym)), Nil).withPos(pos)
153153
}
154154
deferred(defn.ChildAnnot, implicit ctx => makeChildLater(ctx))
155155
}
156156

157157
/** A regular, non-deferred Child annotation */
158-
def apply(sym: Symbol)(implicit ctx: Context): Annotation = apply(_ => sym)
158+
def apply(sym: Symbol, pos: Position)(implicit ctx: Context): Annotation = apply(_ => sym, pos)
159159

160160
def unapply(ann: Annotation)(implicit ctx: Context): Option[Symbol] =
161161
if (ann.symbol == defn.ChildAnnot) {
@@ -165,13 +165,13 @@ object Annotations {
165165
else None
166166
}
167167

168-
def makeSourceFile(path: String)(implicit ctx: Context) =
169-
apply(defn.SourceFileAnnot, Literal(Constant(path)))
168+
def makeSourceFile(path: String, pos: Position)(implicit ctx: Context) =
169+
apply(defn.SourceFileAnnot, Literal(Constant(path)), pos)
170170
}
171171

172-
def ThrowsAnnotation(cls: ClassSymbol)(implicit ctx: Context) = {
172+
def ThrowsAnnotation(cls: ClassSymbol, pos: Position)(implicit ctx: Context) = {
173173
val tref = cls.typeRef
174-
Annotation(defn.ThrowsAnnotType.appliedTo(tref), Ident(tref))
174+
Annotation(defn.ThrowsAnnotType.appliedTo(tref), Ident(tref), pos)
175175
}
176176

177177
/** A decorator that provides queries for specific annotations

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,13 +2872,13 @@ object Types {
28722872
* - add @inlineParam to inline call-by-value parameters
28732873
*/
28742874
def fromSymbols(params: List[Symbol], resultType: Type)(implicit ctx: Context) = {
2875-
def translateInline(tp: Type): Type = tp match {
2875+
def translateInline(tp: Type, pos: Position): Type = tp match {
28762876
case _: ExprType => tp
2877-
case _ => AnnotatedType(tp, Annotation(defn.InlineParamAnnot))
2877+
case _ => AnnotatedType(tp, Annotation(defn.InlineParamAnnot, pos))
28782878
}
28792879
def paramInfo(param: Symbol) = {
28802880
val paramType = param.info.annotatedToRepeated
2881-
if (param.is(Inline)) translateInline(paramType) else paramType
2881+
if (param.is(Inline)) translateInline(paramType, param.pos) else paramType
28822882
}
28832883

28842884
apply(params.map(_.name.asTermName))(

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class ClassfileParser(
278278
ctx.warning(s"no linked class for java enum $sym in ${sym.owner}. A referencing class file might be missing an InnerClasses entry.")
279279
else {
280280
if (!(enumClass is Flags.Sealed)) enumClass.setFlag(Flags.AbstractSealed)
281-
enumClass.addAnnotation(Annotation.Child(sym))
281+
enumClass.addAnnotation(Annotation.Child(sym, enumClass.pos))
282282
}
283283
}
284284
} finally {
@@ -520,7 +520,7 @@ class ClassfileParser(
520520
}
521521
}
522522
if (hasError || skip) None
523-
else Some(Annotation.deferredResolve(attrType, argbuf.toList))
523+
else Some(Annotation.deferredResolve(attrType, argbuf.toList, NoPosition))
524524
} catch {
525525
case f: FatalError => throw f // don't eat fatal errors, they mean a class was not found
526526
case NonFatal(ex) =>
@@ -562,14 +562,14 @@ class ClassfileParser(
562562
case tpnme.DeprecatedATTR =>
563563
val msg = Literal(Constant("see corresponding Javadoc for more information."))
564564
val since = Literal(Constant(""))
565-
sym.addAnnotation(Annotation(defn.DeprecatedAnnot, msg, since))
565+
sym.addAnnotation(Annotation(defn.DeprecatedAnnot, msg, since, sym.pos))
566566
case tpnme.ConstantValueATTR =>
567567
val c = pool.getConstant(in.nextChar)
568568
val c1 = convertTo(c, symtype)
569569
if (c1 ne null) newType = ConstantType(c1)
570570
else println("failure to convert " + c + " to " + symtype); //debug
571571
case tpnme.AnnotationDefaultATTR =>
572-
sym.addAnnotation(Annotation(defn.AnnotationDefaultAnnot, Nil))
572+
sym.addAnnotation(Annotation(defn.AnnotationDefaultAnnot, Nil, sym.pos))
573573
// Java annotations on classes / methods / fields with RetentionPolicy.RUNTIME
574574
case tpnme.RuntimeAnnotationATTR =>
575575
parseAnnotations(attrLen)
@@ -605,7 +605,7 @@ class ClassfileParser(
605605
for (n <- 0 until nClasses) {
606606
// FIXME: this performs an equivalent of getExceptionTypes instead of getGenericExceptionTypes (SI-7065)
607607
val cls = pool.getClassSymbol(in.nextChar.toInt)
608-
sym.addAnnotation(ThrowsAnnotation(cls.asClass))
608+
sym.addAnnotation(ThrowsAnnotation(cls.asClass, sym.pos))
609609
}
610610
}
611611

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
578578
}
579579
}
580580
val alias = readDisambiguatedSymbolRef(disambiguate).asTerm
581-
denot.addAnnotation(Annotation.makeAlias(alias))
581+
denot.addAnnotation(Annotation.makeAlias(alias, denot.symbol.pos))
582582
}
583583
}
584584
// println(s"unpickled ${denot.debugString}, info = ${denot.info}") !!! DEBUG
@@ -830,7 +830,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
830830
readNat() // skip reference for now
831831
target.addAnnotation(
832832
Annotation.Child(implicit ctx =>
833-
atReadPos(start, () => readSymbolRef())))
833+
atReadPos(start, () => readSymbolRef()), target.pos))
834834
}
835835
}
836836

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AugmentScala2Traits extends MiniPhase with IdentityDenotTransformer with F
8787
if (sym.isGetter)
8888
if (sym.is(Lazy)) {
8989
if (!sym.hasAnnotation(defn.VolatileAnnot))
90-
sym.addAnnotation(Annotation(defn.VolatileAnnot, Nil))
90+
sym.addAnnotation(Annotation(defn.VolatileAnnot, Nil, sym.pos))
9191
}
9292
else if (!sym.is(Deferred) && !sym.setter.exists &&
9393
!sym.info.resultType.isInstanceOf[ConstantType])

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
9494
appendOffsetDefs.get(cls) match {
9595
case None => template
9696
case Some(data) =>
97-
data.defs.foreach(_.symbol.addAnnotation(Annotation(defn.ScalaStaticAnnot)))
97+
data.defs.foreach(d => d.symbol.addAnnotation(Annotation(defn.ScalaStaticAnnot, d.symbol.pos)))
9898
cpy.Template(template)(body = addInFront(data.defs, template.body))
9999
}
100100

@@ -362,7 +362,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
362362
.symbol.asTerm
363363
} else { // need to create a new flag
364364
offsetSymbol = ctx.newSymbol(claz, offsetById, Flags.Synthetic, defn.LongType).enteredAfter(this)
365-
offsetSymbol.addAnnotation(Annotation(defn.ScalaStaticAnnot))
365+
offsetSymbol.addAnnotation(Annotation(defn.ScalaStaticAnnot, offsetSymbol.pos))
366366
val flagName = (StdNames.nme.BITMAP_PREFIX + id.toString).toTermName
367367
val flagSymbol = ctx.newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
368368
flag = ValDef(flagSymbol, Literal(Constants.Constant(0L)))
@@ -372,7 +372,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
372372

373373
case None =>
374374
offsetSymbol = ctx.newSymbol(claz, offsetName(0), Flags.Synthetic, defn.LongType).enteredAfter(this)
375-
offsetSymbol.addAnnotation(Annotation(defn.ScalaStaticAnnot))
375+
offsetSymbol.addAnnotation(Annotation(defn.ScalaStaticAnnot, offsetSymbol.pos))
376376
val flagName = (StdNames.nme.BITMAP_PREFIX + "0").toTermName
377377
val flagSymbol = ctx.newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
378378
flag = ValDef(flagSymbol, Literal(Constants.Constant(0L)))

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ class MoveStatics extends MiniPhase with SymTransformer {
4343
val newBodyWithStaticConstr =
4444
if (staticFields.nonEmpty) {
4545
/* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */
46-
val staticCostructor = ctx.newSymbol(orig.symbol, nme.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method | Flags.Private, MethodType(Nil, defn.UnitType))
47-
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot))
48-
staticCostructor.entered
46+
val staticConstructor = ctx.newSymbol(orig.symbol, nme.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method | Flags.Private, MethodType(Nil, defn.UnitType),
47+
coord = orig.pos)
48+
staticConstructor.addAnnotation(Annotation(defn.ScalaStaticAnnot, staticConstructor.pos))
49+
staticConstructor.entered
4950

50-
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
51-
tpd.DefDef(staticCostructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody
51+
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticConstructor)))
52+
tpd.DefDef(staticConstructor, Block(staticAssigns, tpd.unitLiteral)) :: newBody
5253
} else newBody
5354

5455
val oldTemplate = orig.rhs.asInstanceOf[Template]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
251251
if (sym.owner.is(Package) &&
252252
ctx.compilationUnit.source.exists &&
253253
sym != defn.SourceFileAnnot)
254-
sym.addAnnotation(Annotation.makeSourceFile(ctx.compilationUnit.source.file.path))
254+
sym.addAnnotation(Annotation.makeSourceFile(ctx.compilationUnit.source.file.path, sym.pos))
255255
tree
256256
}
257257
super.transform(tree)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class SymUtils(val self: Symbol) extends AnyVal {
144144
def register(child: Symbol, parent: Type) = {
145145
val cls = parent.classSymbol
146146
if (cls.is(Sealed) && (!late || child.isInaccessibleChildOf(cls)))
147-
cls.addAnnotation(Annotation.Child(child))
147+
cls.addAnnotation(Annotation.Child(child, cls.pos))
148148
}
149149
if (self.isClass && !self.isAnonymousClass)
150150
self.asClass.classParents.foreach { parent =>

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ class Namer { typer: Typer =>
469469
case vdef: ValDef if (isEnumConstant(vdef)) =>
470470
val enumClass = sym.owner.linkedClass
471471
if (!(enumClass is Flags.Sealed)) enumClass.setFlag(Flags.AbstractSealed)
472-
enumClass.addAnnotation(Annotation.Child(sym))
472+
enumClass.addAnnotation(Annotation.Child(sym, enumClass.pos))
473473
case _ =>
474474
}
475475

0 commit comments

Comments
 (0)