Skip to content

Commit dfad52b

Browse files
committed
Always add spans to compiler generated anonotations
Achieved by removing the apply methods without a span argument, which would generate Annotation trees with empty spans. One can still put NoSpan as the span argument, so this will not make much of a difference, but will hopefully provide an additional incentive to always add one.
1 parent ed2799a commit dfad52b

16 files changed

+34
-49
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ object desugar {
175175
case Into(tpt1) =>
176176
mods1 = vdef.mods.withAddedAnnotation(
177177
TypedSplice(
178-
Annotation(defn.AllowConversionsAnnot).tree.withSpan(tpt.span.startPos)))
178+
Annotation(defn.AllowConversionsAnnot, tpt.span.startPos).tree))
179179
tpt1
180180
case ByNameTypeTree(tpt1) =>
181181
cpy.ByNameTypeTree(tpt)(dropInto(tpt1))

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -194,36 +194,18 @@ object Annotations {
194194
object Annotation {
195195

196196
def apply(tree: Tree): ConcreteAnnotation = ConcreteAnnotation(tree)
197-
198-
def apply(cls: ClassSymbol)(using Context): Annotation =
199-
apply(cls, Nil)
200197

201198
def apply(cls: ClassSymbol, span: Span)(using Context): Annotation =
202199
apply(cls, Nil, span)
203200

204-
def apply(cls: ClassSymbol, arg: Tree)(using Context): Annotation =
205-
apply(cls, arg :: Nil)
206-
207201
def apply(cls: ClassSymbol, arg: Tree, span: Span)(using Context): Annotation =
208202
apply(cls, arg :: Nil, span)
209203

210-
def apply(cls: ClassSymbol, arg1: Tree, arg2: Tree)(using Context): Annotation =
211-
apply(cls, arg1 :: arg2 :: Nil)
212-
213-
def apply(cls: ClassSymbol, args: List[Tree])(using Context): Annotation =
214-
apply(cls.typeRef, args)
215-
216204
def apply(cls: ClassSymbol, args: List[Tree], span: Span)(using Context): Annotation =
217205
apply(cls.typeRef, args, span)
218206

219-
def apply(atp: Type, arg: Tree)(using Context): Annotation =
220-
apply(atp, arg :: Nil)
221-
222-
def apply(atp: Type, arg1: Tree, arg2: Tree)(using Context): Annotation =
223-
apply(atp, arg1 :: arg2 :: Nil)
224-
225-
def apply(atp: Type, args: List[Tree])(using Context): Annotation =
226-
apply(New(atp, args))
207+
def apply(atp: Type, arg: Tree, span: Span)(using Context): Annotation =
208+
apply(atp, arg :: Nil, span)
227209

228210
def apply(atp: Type, args: List[Tree], span: Span)(using Context): Annotation =
229211
apply(New(atp, args).withSpan(span))
@@ -271,7 +253,7 @@ object Annotations {
271253

272254
def ThrowsAnnotation(cls: ClassSymbol)(using Context): Annotation = {
273255
val tref = cls.typeRef
274-
Annotation(defn.ThrowsAnnot.typeRef.appliedTo(tref), Ident(tref))
256+
Annotation(defn.ThrowsAnnot.typeRef.appliedTo(tref), Ident(tref), cls.span)
275257
}
276258

277259
/** Extracts the type of the thrown exception from an annotation.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ object SymDenotations {
283283

284284
/** Add the given annotation without parameters to the annotations of this denotation */
285285
final def addAnnotation(cls: ClassSymbol)(using Context): Unit =
286-
addAnnotation(Annotation(cls))
286+
addAnnotation(Annotation(cls, symbol.span))
287287

288288
/** Remove annotation with given class from this denotation */
289289
final def removeAnnotation(cls: Symbol)(using Context): Unit =

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,9 +3965,9 @@ object Types {
39653965
* - wrap types of parameters that have an @allowConversions annotation with Into[_]
39663966
*/
39673967
def fromSymbols(params: List[Symbol], resultType: Type)(using Context): MethodType =
3968-
def addAnnotation(tp: Type, cls: ClassSymbol): Type = tp match
3969-
case ExprType(resType) => ExprType(addAnnotation(resType, cls))
3970-
case _ => AnnotatedType(tp, Annotation(cls))
3968+
def addAnnotation(tp: Type, cls: ClassSymbol, param: Symbol): Type = tp match
3969+
case ExprType(resType) => ExprType(addAnnotation(resType, cls, param))
3970+
case _ => AnnotatedType(tp, Annotation(cls, param.span))
39713971

39723972
def wrapConvertible(tp: Type) =
39733973
AppliedType(defn.IntoType.typeRef, tp :: Nil)
@@ -3992,9 +3992,9 @@ object Types {
39923992
def paramInfo(param: Symbol) =
39933993
var paramType = param.info.annotatedToRepeated
39943994
if param.is(Inline) then
3995-
paramType = addAnnotation(paramType, defn.InlineParamAnnot)
3995+
paramType = addAnnotation(paramType, defn.InlineParamAnnot, param)
39963996
if param.is(Erased) then
3997-
paramType = addAnnotation(paramType, defn.ErasedParamAnnot)
3997+
paramType = addAnnotation(paramType, defn.ErasedParamAnnot, param)
39983998
if param.hasAnnotation(defn.AllowConversionsAnnot) then
39993999
paramType = addInto(paramType)
40004000
paramType

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ class ClassfileParser(
764764
res.namedParams += (i -> name.name)
765765

766766
case tpnme.AnnotationDefaultATTR =>
767-
sym.addAnnotation(Annotation(defn.AnnotationDefaultAnnot, Nil))
767+
sym.addAnnotation(Annotation(defn.AnnotationDefaultAnnot, Nil, sym.span))
768768

769769
// Java annotations on classes / methods / fields with RetentionPolicy.RUNTIME
770770
case tpnme.RuntimeVisibleAnnotationATTR

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
110110
yield {
111111
def forwarderSym(flags: FlagSet, info: Type): Symbol { type ThisName = TermName } =
112112
val sym = newSymbol(clazz, enumValue.name.asTermName, flags, info)
113-
sym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot))
113+
sym.addAnnotation(Annotations.Annotation(defn.ScalaStaticAnnot, sym.span))
114114
sym
115115
val body = moduleRef.select(enumValue)
116116
if ctx.settings.scalajs.value then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object ContextFunctionResults:
3939
val count = contextResultCount(mdef.rhs, mdef.tpt.tpe)
4040

4141
if Config.flattenContextFunctionResults && count != 0 && !disabled then
42-
val countAnnot = Annotation(defn.ContextResultCountAnnot, Literal(Constant(count)))
42+
val countAnnot = Annotation(defn.ContextResultCountAnnot, Literal(Constant(count)), mdef.symbol.span)
4343
mdef.symbol.addAnnotation(countAnnot)
4444
end annotateContextResults
4545

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
112112
appendOffsetDefs.get(cls) match {
113113
case None => template
114114
case Some(data) =>
115-
data.defs.foreach(_.symbol.addAnnotation(Annotation(defn.ScalaStaticAnnot)))
115+
data.defs.foreach(defin => defin.symbol.addAnnotation(Annotation(defn.ScalaStaticAnnot, defin.symbol.span)))
116116
cpy.Template(template)(body = addInFront(data.defs, template.body))
117117
}
118118
}
@@ -464,7 +464,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
464464
def offsetName(id: Int) = s"${StdNames.nme.LAZY_FIELD_OFFSET}${if (x.symbol.owner.is(Module)) "_m_" else ""}$id".toTermName
465465
val containerName = LazyLocalName.fresh(x.name.asTermName)
466466
val containerSymbol = newSymbol(claz, containerName, x.symbol.flags &~ containerFlagsMask | containerFlags | Private, defn.ObjectType, coord = x.symbol.coord).enteredAfter(this)
467-
containerSymbol.addAnnotation(Annotation(defn.VolatileAnnot)) // private @volatile var _x: AnyRef
467+
containerSymbol.addAnnotation(Annotation(defn.VolatileAnnot, containerSymbol.span)) // private @volatile var _x: AnyRef
468468
containerSymbol.addAnnotations(x.symbol.annotations) // pass annotations from original definition
469469
val stat = x.symbol.isStatic
470470
if stat then
@@ -482,7 +482,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
482482
newSymbol(claz, offsetName(info.defs.size), Synthetic, defn.LongType).enteredAfter(this)
483483
case None =>
484484
newSymbol(claz, offsetName(0), Synthetic, defn.LongType).enteredAfter(this)
485-
offsetSymbol.nn.addAnnotation(Annotation(defn.ScalaStaticAnnot))
485+
offsetSymbol.nn.addAnnotation(Annotation(defn.ScalaStaticAnnot, offsetSymbol.nn.span))
486486
val fieldTree = thizClass.select(lazyNme.RLazyVals.getDeclaredField).appliedTo(Literal(Constant(containerName.mangledString)))
487487
val offsetTree = ValDef(offsetSymbol.nn, getOffset.appliedTo(fieldTree))
488488
val offsetInfo = appendOffsetDefs.getOrElseUpdate(claz, new OffsetInfo(Nil))
@@ -625,7 +625,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
625625
.symbol.asTerm
626626
else { // need to create a new flag
627627
offsetSymbol = newSymbol(claz, offsetById, Synthetic, defn.LongType).enteredAfter(this)
628-
offsetSymbol.nn.addAnnotation(Annotation(defn.ScalaStaticAnnot))
628+
offsetSymbol.nn.addAnnotation(Annotation(defn.ScalaStaticAnnot, offsetSymbol.nn.span))
629629
val flagName = LazyBitMapName.fresh(id.toString.toTermName)
630630
val flagSymbol = newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
631631
flag = ValDef(flagSymbol, Literal(Constant(0L)))
@@ -636,7 +636,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
636636

637637
case None =>
638638
offsetSymbol = newSymbol(claz, offsetName(0), Synthetic, defn.LongType).enteredAfter(this)
639-
offsetSymbol.nn.addAnnotation(Annotation(defn.ScalaStaticAnnot))
639+
offsetSymbol.nn.addAnnotation(Annotation(defn.ScalaStaticAnnot, offsetSymbol.nn.span))
640640
val flagName = LazyBitMapName.fresh("0".toTermName)
641641
val flagSymbol = newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
642642
flag = ValDef(flagSymbol, Literal(Constant(0L)))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MoveStatics extends MiniPhase with SymTransformer {
4646
if (staticFields.nonEmpty) {
4747
/* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */
4848
val staticCostructor = newSymbol(orig.symbol, nme.STATIC_CONSTRUCTOR, Flags.Synthetic | Flags.Method | Flags.Private, MethodType(Nil, defn.UnitType))
49-
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot))
49+
staticCostructor.addAnnotation(Annotation(defn.ScalaStaticAnnot, staticCostructor.span))
5050
staticCostructor.entered
5151

5252
val staticAssigns = staticFields.map(x => Assign(ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ object PCPCheckAndHeal {
297297
flags = Synthetic,
298298
info = TypeAlias(splicedTree.tpe.select(tpnme.Underlying)),
299299
coord = span).asType
300-
local.addAnnotation(Annotation(defn.QuotedRuntime_SplicedTypeAnnot))
300+
local.addAnnotation(Annotation(defn.QuotedRuntime_SplicedTypeAnnot, span))
301301
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local)
302302
}
303303

0 commit comments

Comments
 (0)