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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class RepeatableAnnotations extends MiniPhase:
4545
Nil
4646
else
4747
val aggregated = JavaSeqLiteral(anns.map(_.tree).toList, TypeTree(sym.typeRef))
48-
Annotation(containerTpe, NamedArg("value".toTermName, aggregated)) :: Nil
48+
Annotation(containerTpe, NamedArg("value".toTermName, aggregated), sym.span) :: Nil
4949
case _ =>
5050
val pos = anns.head.tree.srcPos
5151
report.error("Not repeatable annotation repeated", pos)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ object SymUtils:
381381
if original.hasAnnotation(defn.TargetNameAnnot) then
382382
self.addAnnotation(
383383
Annotation(defn.TargetNameAnnot,
384-
Literal(Constant(nameFn(original.targetName).toString)).withSpan(original.span)))
384+
Literal(Constant(nameFn(original.targetName).toString)).withSpan(original.span), original.span))
385385

386386
/** The return type as seen from the body of this definition. It is
387387
* computed from the symbol's type by replacing param refs by param symbols.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ast.untpd
1313
import ValueClasses.isDerivedValueClass
1414
import SymUtils._
1515
import util.Property
16+
import util.Spans.Span
1617
import config.Printers.derive
1718
import NullOpsDecorator._
1819

@@ -155,7 +156,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
155156
case nme.hashCode_ => chooseHashcode
156157
case nme.toString_ => toStringBody(vrefss)
157158
case nme.equals_ => equalsBody(vrefss.head.head)
158-
case nme.canEqual_ => canEqualBody(vrefss.head.head)
159+
case nme.canEqual_ => canEqualBody(vrefss.head.head, synthetic.span)
159160
case nme.ordinal => ordinalRef
160161
case nme.productArity => Literal(Constant(accessors.length))
161162
case nme.productPrefix if isEnumValue => nameRef
@@ -260,7 +261,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
260261
def equalsBody(that: Tree)(using Context): Tree = {
261262
val thatAsClazz = newSymbol(ctx.owner, nme.x_0, SyntheticCase, clazzType, coord = ctx.owner.span) // x$0
262263
def wildcardAscription(tp: Type) = Typed(Underscore(tp), TypeTree(tp))
263-
val pattern = Bind(thatAsClazz, wildcardAscription(AnnotatedType(clazzType, Annotation(defn.UncheckedAnnot)))) // x$0 @ (_: C @unchecked)
264+
val pattern = Bind(thatAsClazz, wildcardAscription(AnnotatedType(clazzType, Annotation(defn.UncheckedAnnot, thatAsClazz.span)))) // x$0 @ (_: C @unchecked)
264265
// compare primitive fields first, slow equality checks of non-primitive fields can be skipped when primitives differ
265266
val sortedAccessors = accessors.sortBy(accessor => if (accessor.info.typeSymbol.isPrimitiveValueClass) 0 else 1)
266267
val comparisons = sortedAccessors.map { accessor =>
@@ -390,7 +391,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
390391
*
391392
* `@unchecked` is needed for parametric case classes.
392393
*/
393-
def canEqualBody(that: Tree): Tree = that.isInstance(AnnotatedType(clazzType, Annotation(defn.UncheckedAnnot)))
394+
def canEqualBody(that: Tree, span: Span): Tree = that.isInstance(AnnotatedType(clazzType, Annotation(defn.UncheckedAnnot, span)))
394395

395396
symbolsToSynthesize.flatMap(syntheticDefIfMissing)
396397
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class Namer { typer: Typer =>
461461
val isProvisional = parents.exists(!_.baseType(defn.AnyClass).exists)
462462
if isProvisional then
463463
typr.println(i"provisional superclass $first for $cls")
464-
first = AnnotatedType(first, Annotation(defn.ProvisionalSuperClassAnnot))
464+
first = AnnotatedType(first, Annotation(defn.ProvisionalSuperClassAnnot, cls.span))
465465
checkFeasibleParent(first, cls.srcPos, i" in inferred superclass $first") :: parents
466466
end ensureFirstIsClass
467467

@@ -1883,7 +1883,7 @@ class Namer { typer: Typer =>
18831883
// larger choice of overrides (see `default-getter.scala`).
18841884
// For justification on the use of `@uncheckedVariance`, see
18851885
// `default-getter-variance.scala`.
1886-
AnnotatedType(defaultTp, Annotation(defn.UncheckedVarianceAnnot))
1886+
AnnotatedType(defaultTp, Annotation(defn.UncheckedVarianceAnnot, sym.span))
18871887
else
18881888
// don't strip @uncheckedVariance annot for default getters
18891889
TypeOps.simplify(tp.widenTermRefExpr,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
16631663

16641664
// skip exhaustivity check in later phase
16651665
// TODO: move the check above to patternMatcher phase
1666-
val uncheckedTpe = AnnotatedType(sel.tpe.widen, Annotation(defn.UncheckedAnnot))
1666+
val uncheckedTpe = AnnotatedType(sel.tpe.widen, Annotation(defn.UncheckedAnnot, tree.selector.span))
16671667
tpd.cpy.Match(result)(
16681668
selector = tpd.Typed(sel, tpd.TypeTree(uncheckedTpe)),
16691669
cases = result.cases
@@ -1898,7 +1898,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
18981898
Typed(res,
18991899
TypeTree(
19001900
AnnotatedType(res.tpe,
1901-
Annotation(defn.RequiresCapabilityAnnot, cap))))
1901+
Annotation(defn.RequiresCapabilityAnnot, cap, tree.span))))
19021902
else res
19031903

19041904
def typedSeqLiteral(tree: untpd.SeqLiteral, pt: Type)(using Context): SeqLiteral = {
@@ -4297,7 +4297,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42974297
// this is needed for -Ycheck. Without the annotation Ycheck will
42984298
// skolemize the result type which will lead to different types before
42994299
// and after checking. See i11955.scala.
4300-
AnnotatedType(conj, Annotation(defn.UncheckedStableAnnot))
4300+
AnnotatedType(conj, Annotation(defn.UncheckedStableAnnot, tree.symbol.span))
43014301
else conj
43024302
else pt
43034303
gadts.println(i"insert GADT cast from $tree to $target")

compiler/test/dotty/tools/dotc/transform/TypeTestsCastsTest.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import core.*
66
import Contexts.*, Decorators.*, Denotations.*, SymDenotations.*, Symbols.*, Types.*
77
import Annotations.*
88

9+
import dotty.tools.dotc.util.Spans.Span
10+
911
import org.junit.Test
1012
import org.junit.Assert.*
1113

@@ -15,7 +17,7 @@ class TypeTestsCastsTest extends DottyTest:
1517
@Test def orL = checkFound(List(StringType, LongType), OrType(LongType, StringType, false))
1618
@Test def orR = checkFound(List(LongType, StringType), OrType(StringType, LongType, false))
1719

18-
@Test def annot = checkFound(List(StringType, LongType), AnnotatedType(OrType(LongType, StringType, false), Annotation(defn.UncheckedAnnot)))
20+
@Test def annot = checkFound(List(StringType, LongType), AnnotatedType(OrType(LongType, StringType, false), Annotation(defn.UncheckedAnnot, Span(0))))
1921

2022
@Test def andL = checkFound(List(StringType), AndType(StringType, AnyType))
2123
@Test def andR = checkFound(List(StringType), AndType(AnyType, StringType))

0 commit comments

Comments
 (0)