Skip to content

Commit ed2799a

Browse files
committed
Set span for top level annotations generated in PostTyper
This should help avoid macros throwing assertion errors in the case of annotations not having assigned Spans/positions. Since the annotation is generated and does not have a position corresponding to the source file, it inherits Span from the tree to which it is assigned.
1 parent ef653b6 commit ed2799a

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,24 @@ object Annotations {
197197

198198
def apply(cls: ClassSymbol)(using Context): Annotation =
199199
apply(cls, Nil)
200+
201+
def apply(cls: ClassSymbol, span: Span)(using Context): Annotation =
202+
apply(cls, Nil, span)
200203

201204
def apply(cls: ClassSymbol, arg: Tree)(using Context): Annotation =
202205
apply(cls, arg :: Nil)
206+
207+
def apply(cls: ClassSymbol, arg: Tree, span: Span)(using Context): Annotation =
208+
apply(cls, arg :: Nil, span)
203209

204210
def apply(cls: ClassSymbol, arg1: Tree, arg2: Tree)(using Context): Annotation =
205211
apply(cls, arg1 :: arg2 :: Nil)
206212

207213
def apply(cls: ClassSymbol, args: List[Tree])(using Context): Annotation =
208214
apply(cls.typeRef, args)
215+
216+
def apply(cls: ClassSymbol, args: List[Tree], span: Span)(using Context): Annotation =
217+
apply(cls.typeRef, args, span)
209218

210219
def apply(atp: Type, arg: Tree)(using Context): Annotation =
211220
apply(atp, arg :: Nil)
@@ -215,6 +224,9 @@ object Annotations {
215224

216225
def apply(atp: Type, args: List[Tree])(using Context): Annotation =
217226
apply(New(atp, args))
227+
228+
def apply(atp: Type, args: List[Tree], span: Span)(using Context): Annotation =
229+
apply(New(atp, args).withSpan(span))
218230

219231
/** Create an annotation where the tree is computed lazily. */
220232
def deferred(sym: Symbol)(treeFn: Context ?=> Tree): Annotation =
@@ -251,8 +263,8 @@ object Annotations {
251263
else None
252264
}
253265

254-
def makeSourceFile(path: String)(using Context): Annotation =
255-
apply(defn.SourceFileAnnot, Literal(Constant(path)))
266+
def makeSourceFile(path: String, span: Span)(using Context): Annotation =
267+
apply(defn.SourceFileAnnot, Literal(Constant(path)), span)
256268
}
257269

258270
@sharable val EmptyAnnotation = Annotation(EmptyTree)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
395395
if ctx.compilationUnit.source.exists && sym != defn.SourceFileAnnot then
396396
val reference = ctx.settings.sourceroot.value
397397
val relativePath = util.SourceFile.relativePath(ctx.compilationUnit.source, reference)
398-
sym.addAnnotation(Annotation.makeSourceFile(relativePath))
398+
sym.addAnnotation(Annotation.makeSourceFile(relativePath, tree.span))
399399
if Feature.pureFunsEnabled && sym != defn.WithPureFunsAnnot then
400-
sym.addAnnotation(Annotation(defn.WithPureFunsAnnot))
400+
sym.addAnnotation(Annotation(defn.WithPureFunsAnnot, tree.span))
401401
else
402402
if !sym.is(Param) && !sym.owner.isOneOf(AbstractOrTrait) then
403403
Checking.checkGoodBounds(tree.symbol)
@@ -499,8 +499,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
499499

500500
private def annotateExperimental(sym: Symbol)(using Context): Unit =
501501
if sym.is(Module) && sym.companionClass.hasAnnotation(defn.ExperimentalAnnot) then
502-
sym.addAnnotation(defn.ExperimentalAnnot)
503-
sym.companionModule.addAnnotation(defn.ExperimentalAnnot)
502+
sym.addAnnotation(Annotation(defn.ExperimentalAnnot, sym.span))
503+
sym.companionModule.addAnnotation(Annotation(defn.ExperimentalAnnot, sym.span))
504504

505505
}
506506
}

tests/pos-macros/i16318/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.quoted.*
2+
3+
final case class Record(a: String, b: Int)
4+
5+
transparent inline def ann[T]: List[Any] = ${ annsImpl[T] }
6+
7+
def annsImpl[T: Type](using Quotes): Expr[List[Any]] = {
8+
import quotes.reflect.*
9+
val annExpr = TypeRepr.of[T].typeSymbol.annotations.head.asExpr
10+
'{ List($annExpr) }
11+
}

tests/pos-macros/i16318/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def Test =
2+
val a = ann[Record]

0 commit comments

Comments
 (0)