Skip to content

Emit "deprecated" bytecode attribute #9350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions compiler/src/dotty/tools/dotc/core/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,33 +206,6 @@ object Annotations {
Annotation(defn.ThrowsAnnot.typeRef.appliedTo(tref), Ident(tref))
}

/** A decorator that provides queries for specific annotations
* of a symbol.
*/
implicit class AnnotInfo(val sym: Symbol) extends AnyVal {

def deprecationMessage(using Context): Option[String] =
for {
annot <- sym.getAnnotation(defn.DeprecatedAnnot)
arg <- annot.argumentConstant(0)
}
yield arg.stringValue

def migrationVersion(using Context): Option[Try[ScalaVersion]] =
for {
annot <- sym.getAnnotation(defn.MigrationAnnot)
arg <- annot.argumentConstant(1)
}
yield ScalaVersion.parse(arg.stringValue)

def migrationMessage(using Context): Option[Try[ScalaVersion]] =
for {
annot <- sym.getAnnotation(defn.MigrationAnnot)
arg <- annot.argumentConstant(0)
}
yield ScalaVersion.parse(arg.stringValue)
}

/** Extracts the type of the thrown exception from an annotation.
*
* Supports both "old-style" `@throws(classOf[Exception])`
Expand Down
7 changes: 3 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1925,9 +1925,9 @@ object messages {
def explain = "A sealed class or trait can only be extended in the same file as its declaration"
}

class SymbolHasUnparsableVersionNumber(symbol: Symbol, migrationVersion: String)(using Context)
class SymbolHasUnparsableVersionNumber(symbol: Symbol, errorMessage: String)(using Context)
extends SyntaxMsg(SymbolHasUnparsableVersionNumberID) {
def msg = em"${symbol.showLocated} has an unparsable version number: $migrationVersion"
def msg = em"${symbol.showLocated} has an unparsable version number: $errorMessage"
def explain =
em"""The ${symbol.showLocated} is marked with ${hl("@migration")} indicating it has changed semantics
|between versions and the ${hl("-Xmigration")} settings is used to warn about constructs
Expand All @@ -1940,11 +1940,10 @@ object messages {
migrationMessage: String
)(using Context) extends SyntaxMsg(SymbolChangedSemanticsInVersionID) {
def msg = em"${symbol.showLocated} has changed semantics in version $migrationVersion: $migrationMessage"
def explain = {
def explain =
em"""The ${symbol.showLocated} is marked with ${hl("@migration")} indicating it has changed semantics
|between versions and the ${hl("-Xmigration")} settings is used to warn about constructs
|whose behavior may have changed since version change."""
}
}

class UnableToEmitSwitch(tooFewCases: Boolean)(using Context)
Expand Down
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/Memoize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
case _ => ()
}

def removeAnnotations(denot: SymDenotation): Unit =
def removeUnwantedAnnotations(denot: SymDenotation): Unit =
if (sym.annotations.nonEmpty) {
val cpy = sym.copySymDenotation()
cpy.annotations = Nil
// Keep @deprecated annotation so that accessors can
// be marked as deprecated in the bytecode
cpy.filterAnnotations(_.matches(defn.DeprecatedAnnot))
cpy.installAfter(thisPhase)
}

Expand Down Expand Up @@ -135,7 +137,7 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
else transformFollowingDeep(ref(field))(using ctx.withOwner(sym))
val getterDef = cpy.DefDef(tree)(rhs = getterRhs)
addAnnotations(fieldDef.denot)
removeAnnotations(sym)
removeUnwantedAnnotations(sym)
Thicket(fieldDef, getterDef)
}
else if (sym.isSetter) {
Expand All @@ -145,7 +147,7 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
if (isErasableBottomField(tree.vparamss.head.head.tpt.tpe.classSymbol)) Literal(Constant(()))
else Assign(ref(field), adaptToField(ref(tree.vparamss.head.head.symbol)))
val setterDef = cpy.DefDef(tree)(rhs = transformFollowingDeep(initializer)(using ctx.withOwner(sym)))
removeAnnotations(sym)
removeUnwantedAnnotations(sym)
setterDef
}
else tree // curiously, some accessors from Scala2 have ' ' suffixes. They count as
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,12 @@ class TestBCode extends DottyBytecodeTest {
val code =
"""@deprecated
|class Test {
| @deprecated
| val v = 0
|
| @deprecated
| var x = 0
|
| @deprecated("do not use this function!")
| def f(): Unit = ()
|}
Expand All @@ -962,6 +968,13 @@ class TestBCode extends DottyBytecodeTest {
val c = loadClassNode(dir.lookupName("Test.class", directory = false).input)
assert((c.access & Opcodes.ACC_DEPRECATED) != 0)
assert((getMethod(c, "f").access & Opcodes.ACC_DEPRECATED) != 0)

assert((getField(c, "v").access & Opcodes.ACC_DEPRECATED) != 0)
assert((getMethod(c, "v").access & Opcodes.ACC_DEPRECATED) != 0)

assert((getField(c, "x").access & Opcodes.ACC_DEPRECATED) != 0)
assert((getMethod(c, "x").access & Opcodes.ACC_DEPRECATED) != 0)
assert((getMethod(c, "x_$eq").access & Opcodes.ACC_DEPRECATED) != 0)
}
}
}
Expand Down