Skip to content

Commit e1b3d1a

Browse files
committed
Do not copy annotations from a class to its synthetic companion object.
Normally, annotations applied to a class have no business being replicated on a synthetic companion object. One exception was the `@alpha` method, which is supposed to apply as well. Instead of trying to identify those annotations without the symbols during `Desugar`, we change `erasedName` to go look on the companion class of synthetic module classes.
1 parent b3f908d commit e1b3d1a

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ object desugar {
365365
val companionMods = mods
366366
.withFlags((mods.flags & (AccessFlags | Final)).toCommonFlags)
367367
.withMods(Nil)
368+
.withAnnotations(Nil)
368369

369370
var defaultGetters: List[Tree] = Nil
370371

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -491,21 +491,26 @@ object SymDenotations {
491491

492492
/** The name given in an `@alpha` annotation if one is present, `name` otherwise */
493493
final def erasedName(using Context): Name =
494-
getAnnotation(defn.AlphaAnnot) match {
495-
case Some(ann) =>
496-
ann.arguments match {
497-
case Literal(Constant(str: String)) :: Nil =>
498-
if (isType)
499-
if (is(ModuleClass))
500-
str.toTypeName.moduleClassName
501-
else
502-
str.toTypeName
494+
def fromAnnot(ann: Annotation): Name =
495+
ann.arguments match
496+
case Literal(Constant(str: String)) :: Nil =>
497+
if (isType)
498+
if (is(ModuleClass))
499+
str.toTypeName.moduleClassName
503500
else
504-
str.toTermName
505-
case _ => name
506-
}
507-
case _ => name
508-
}
501+
str.toTypeName
502+
else
503+
str.toTermName
504+
case _ => name
505+
getAnnotation(defn.AlphaAnnot) match
506+
case Some(ann) => fromAnnot(ann)
507+
case _ =>
508+
if isAllOf(ModuleClass | Synthetic) then
509+
companionClass.getAnnotation(defn.AlphaAnnot) match
510+
case Some(ann) => fromAnnot(ann)
511+
case _ => name
512+
else
513+
name
509514

510515
// ----- Tests -------------------------------------------------
511516

0 commit comments

Comments
 (0)