Skip to content

Commit 967947f

Browse files
Mark getters and setters of deprecated fields as deprecated
1 parent cb86624 commit 967947f

File tree

4 files changed

+22
-35
lines changed

4 files changed

+22
-35
lines changed

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -206,33 +206,6 @@ object Annotations {
206206
Annotation(defn.ThrowsAnnot.typeRef.appliedTo(tref), Ident(tref))
207207
}
208208

209-
/** A decorator that provides queries for specific annotations
210-
* of a symbol.
211-
*/
212-
implicit class AnnotInfo(val sym: Symbol) extends AnyVal {
213-
214-
def deprecationMessage(using Context): Option[String] =
215-
for {
216-
annot <- sym.getAnnotation(defn.DeprecatedAnnot)
217-
arg <- annot.argumentConstant(0)
218-
}
219-
yield arg.stringValue
220-
221-
def migrationVersion(using Context): Option[Try[ScalaVersion]] =
222-
for {
223-
annot <- sym.getAnnotation(defn.MigrationAnnot)
224-
arg <- annot.argumentConstant(1)
225-
}
226-
yield ScalaVersion.parse(arg.stringValue)
227-
228-
def migrationMessage(using Context): Option[Try[ScalaVersion]] =
229-
for {
230-
annot <- sym.getAnnotation(defn.MigrationAnnot)
231-
arg <- annot.argumentConstant(0)
232-
}
233-
yield ScalaVersion.parse(arg.stringValue)
234-
}
235-
236209
/** Extracts the type of the thrown exception from an annotation.
237210
*
238211
* Supports both "old-style" `@throws(classOf[Exception])`

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,9 +1925,9 @@ object messages {
19251925
def explain = "A sealed class or trait can only be extended in the same file as its declaration"
19261926
}
19271927

1928-
class SymbolHasUnparsableVersionNumber(symbol: Symbol, migrationVersion: String)(using Context)
1928+
class SymbolHasUnparsableVersionNumber(symbol: Symbol, errorMessage: String)(using Context)
19291929
extends SyntaxMsg(SymbolHasUnparsableVersionNumberID) {
1930-
def msg = em"${symbol.showLocated} has an unparsable version number: $migrationVersion"
1930+
def msg = em"${symbol.showLocated} has an unparsable version number: $errorMessage"
19311931
def explain =
19321932
em"""The ${symbol.showLocated} is marked with ${hl("@migration")} indicating it has changed semantics
19331933
|between versions and the ${hl("-Xmigration")} settings is used to warn about constructs
@@ -1940,11 +1940,10 @@ object messages {
19401940
migrationMessage: String
19411941
)(using Context) extends SyntaxMsg(SymbolChangedSemanticsInVersionID) {
19421942
def msg = em"${symbol.showLocated} has changed semantics in version $migrationVersion: $migrationMessage"
1943-
def explain = {
1943+
def explain =
19441944
em"""The ${symbol.showLocated} is marked with ${hl("@migration")} indicating it has changed semantics
19451945
|between versions and the ${hl("-Xmigration")} settings is used to warn about constructs
19461946
|whose behavior may have changed since version change."""
1947-
}
19481947
}
19491948

19501949
class UnableToEmitSwitch(tooFewCases: Boolean)(using Context)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
9696
case _ => ()
9797
}
9898

99-
def removeAnnotations(denot: SymDenotation): Unit =
99+
def removeUnwantedAnnotations(denot: SymDenotation): Unit =
100100
if (sym.annotations.nonEmpty) {
101101
val cpy = sym.copySymDenotation()
102-
cpy.annotations = Nil
102+
// Keep @deprecated annotation so that accessors can
103+
// be marked as deprecated in the bytecode
104+
cpy.filterAnnotations(_.matches(defn.DeprecatedAnnot))
103105
cpy.installAfter(thisPhase)
104106
}
105107

@@ -135,7 +137,7 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
135137
else transformFollowingDeep(ref(field))(using ctx.withOwner(sym))
136138
val getterDef = cpy.DefDef(tree)(rhs = getterRhs)
137139
addAnnotations(fieldDef.denot)
138-
removeAnnotations(sym)
140+
removeUnwantedAnnotations(sym)
139141
Thicket(fieldDef, getterDef)
140142
}
141143
else if (sym.isSetter) {
@@ -145,7 +147,7 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
145147
if (isErasableBottomField(tree.vparamss.head.head.tpt.tpe.classSymbol)) Literal(Constant(()))
146148
else Assign(ref(field), adaptToField(ref(tree.vparamss.head.head.symbol)))
147149
val setterDef = cpy.DefDef(tree)(rhs = transformFollowingDeep(initializer)(using ctx.withOwner(sym)))
148-
removeAnnotations(sym)
150+
removeUnwantedAnnotations(sym)
149151
setterDef
150152
}
151153
else tree // curiously, some accessors from Scala2 have ' ' suffixes. They count as

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,12 @@ class TestBCode extends DottyBytecodeTest {
953953
val code =
954954
"""@deprecated
955955
|class Test {
956+
| @deprecated
957+
| val v = 0
958+
|
959+
| @deprecated
960+
| var x = 0
961+
|
956962
| @deprecated("do not use this function!")
957963
| def f(): Unit = ()
958964
|}
@@ -962,6 +968,13 @@ class TestBCode extends DottyBytecodeTest {
962968
val c = loadClassNode(dir.lookupName("Test.class", directory = false).input)
963969
assert((c.access & Opcodes.ACC_DEPRECATED) != 0)
964970
assert((getMethod(c, "f").access & Opcodes.ACC_DEPRECATED) != 0)
971+
972+
assert((getField(c, "v").access & Opcodes.ACC_DEPRECATED) != 0)
973+
assert((getMethod(c, "v").access & Opcodes.ACC_DEPRECATED) != 0)
974+
975+
assert((getField(c, "x").access & Opcodes.ACC_DEPRECATED) != 0)
976+
assert((getMethod(c, "x").access & Opcodes.ACC_DEPRECATED) != 0)
977+
assert((getMethod(c, "x_$eq").access & Opcodes.ACC_DEPRECATED) != 0)
965978
}
966979
}
967980
}

0 commit comments

Comments
 (0)