Skip to content

Commit 178bfa8

Browse files
Move getter/setter annotation to the underlying field
This is nessary for interactiong with java libraries such as JMH. Matches scalac's behavior.
1 parent 5093809 commit 178bfa8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ object SymDenotations {
277277
}
278278

279279
/** Update the annotations of this denotation */
280-
private[core] final def annotations_=(annots: List[Annotation]): Unit =
280+
final def annotations_=(annots: List[Annotation]): Unit =
281281
myAnnotations = annots
282282

283283
/** Does this denotation have an annotation matching the given class symbol? */

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import DenotTransformers._
66
import Phases.Phase
77
import Contexts.Context
88
import SymDenotations.SymDenotation
9+
import Denotations._
910
import Types._
1011
import Symbols._
1112
import SymUtils._
@@ -84,12 +85,21 @@ import Decorators._
8485
.enteredAfter(thisTransform)
8586
}
8687

87-
/** Can be used to filter annotations on getters and setters; not used yet */
88-
def keepAnnotations(denot: SymDenotation, meta: ClassSymbol) = {
89-
val cpy = sym.copySymDenotation()
90-
cpy.filterAnnotations(_.symbol.derivesFrom(meta))
91-
if (cpy.annotations ne denot.annotations) cpy.installAfter(thisTransform)
92-
}
88+
def addAnnotations(denot: Denotation): Unit =
89+
denot match {
90+
case fieldDenot: SymDenotation if sym.annotations.nonEmpty =>
91+
val cpy = fieldDenot.copySymDenotation()
92+
cpy.annotations = sym.annotations
93+
cpy.installAfter(thisTransform)
94+
case _ => ()
95+
}
96+
97+
def removeAnnotations(denot: SymDenotation): Unit =
98+
if (sym.annotations.nonEmpty) {
99+
val cpy = sym.copySymDenotation()
100+
cpy.annotations = Nil
101+
cpy.installAfter(thisTransform)
102+
}
93103

94104
lazy val field = sym.field.orElse(newField).asTerm
95105

@@ -125,14 +135,18 @@ import Decorators._
125135
if (isErasableBottomField(rhsClass)) erasedBottomTree(rhsClass)
126136
else transformFollowingDeep(ref(field))(ctx.withOwner(sym), info)
127137
val getterDef = cpy.DefDef(tree)(rhs = getterRhs)
138+
addAnnotations(fieldDef.denot)
139+
removeAnnotations(sym)
128140
Thicket(fieldDef, getterDef)
129141
} else if (sym.isSetter) {
130142
if (!sym.is(ParamAccessor)) { val Literal(Constant(())) = tree.rhs } // This is intended as an assertion
131143
field.setFlag(Mutable) // Necessary for vals mixed in from Scala2 traits
132144
if (isErasableBottomField(tree.vparamss.head.head.tpt.tpe.classSymbol)) tree
133145
else {
134146
val initializer = Assign(ref(field), adaptToField(ref(tree.vparamss.head.head.symbol)))
135-
cpy.DefDef(tree)(rhs = transformFollowingDeep(initializer)(ctx.withOwner(sym), info))
147+
val setterDef = cpy.DefDef(tree)(rhs = transformFollowingDeep(initializer)(ctx.withOwner(sym), info))
148+
removeAnnotations(sym)
149+
setterDef
136150
}
137151
}
138152
else tree // curiously, some accessors from Scala2 have ' ' suffixes. They count as

0 commit comments

Comments
 (0)