From f8eb853d0462ee31f7fda5401c72d4424778ce82 Mon Sep 17 00:00:00 2001 From: Hamza Remmal Date: Thu, 11 Sep 2025 18:59:11 +0200 Subject: [PATCH] fix: emit deprecation warnings when a symbol is annotated by a deprecated annotation [Cherry-picked 8bd8f38d86bb5546ab61b3eb670704bef28dcd0f] --- .../tools/dotc/typer/CrossVersionChecks.scala | 8 ++++++++ tests/warn/i23905.check | 17 +++++++++++++++++ tests/warn/i23905/Annot_1.scala | 2 ++ tests/warn/i23905/Test_2.scala | 13 +++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 tests/warn/i23905.check create mode 100644 tests/warn/i23905/Annot_1.scala create mode 100644 tests/warn/i23905/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala b/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala index 2122c10ad625..e4b0d7506319 100644 --- a/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala @@ -37,6 +37,11 @@ class CrossVersionChecks extends MiniPhase: for annot <- sym.annotations if annot.symbol.isExperimental do Feature.checkExperimentalDef(annot.symbol, annot.tree) + private def checkDeprecatedAnnots(sym: Symbol)(using Context): Unit = + if sym.exists then + for annot <- sym.annotations if annot.symbol.isDeprecated do + checkDeprecatedRef(annot.symbol, annot.tree.srcPos) + /** If @migration is present (indicating that the symbol has changed semantics between versions), * emit a warning. */ @@ -85,16 +90,19 @@ class CrossVersionChecks extends MiniPhase: override def transformValDef(tree: ValDef)(using Context): ValDef = checkDeprecatedOvers(tree) checkExperimentalAnnots(tree.symbol) + checkDeprecatedAnnots(tree.symbol) tree override def transformDefDef(tree: DefDef)(using Context): DefDef = checkDeprecatedOvers(tree) checkExperimentalAnnots(tree.symbol) + checkDeprecatedAnnots(tree.symbol) tree override def transformTypeDef(tree: TypeDef)(using Context): TypeDef = // TODO do we need to check checkDeprecatedOvers(tree)? checkExperimentalAnnots(tree.symbol) + checkDeprecatedAnnots(tree.symbol) tree override def transformTemplate(tree: tpd.Template)(using Context): tpd.Tree = diff --git a/tests/warn/i23905.check b/tests/warn/i23905.check new file mode 100644 index 000000000000..9ca2ca0fb200 --- /dev/null +++ b/tests/warn/i23905.check @@ -0,0 +1,17 @@ + +-- Deprecation Warning: tests/warn/i23905/Test_2.scala:3:0 ------------------------------------------------------------- +3 |@DeprecatedAnnotation // warn + |^^^^^^^^^^^^^^^^^^^^^ + |class DeprecatedAnnotation is deprecated +-- Deprecation Warning: tests/warn/i23905/Test_2.scala:6:0 ------------------------------------------------------------- +6 |@DeprecatedAnnotation // warn + |^^^^^^^^^^^^^^^^^^^^^ + |class DeprecatedAnnotation is deprecated +-- Deprecation Warning: tests/warn/i23905/Test_2.scala:9:0 ------------------------------------------------------------- +9 |@DeprecatedAnnotation // warn + |^^^^^^^^^^^^^^^^^^^^^ + |class DeprecatedAnnotation is deprecated +-- Deprecation Warning: tests/warn/i23905/Test_2.scala:12:0 ------------------------------------------------------------ +12 |@DeprecatedAnnotation // warn + |^^^^^^^^^^^^^^^^^^^^^ + |class DeprecatedAnnotation is deprecated diff --git a/tests/warn/i23905/Annot_1.scala b/tests/warn/i23905/Annot_1.scala new file mode 100644 index 000000000000..e13664c67ebd --- /dev/null +++ b/tests/warn/i23905/Annot_1.scala @@ -0,0 +1,2 @@ +@deprecated +class DeprecatedAnnotation extends annotation.StaticAnnotation diff --git a/tests/warn/i23905/Test_2.scala b/tests/warn/i23905/Test_2.scala new file mode 100644 index 000000000000..248e923a5d83 --- /dev/null +++ b/tests/warn/i23905/Test_2.scala @@ -0,0 +1,13 @@ +//> using options -deprecation + +@DeprecatedAnnotation // warn +object Test1 + +@DeprecatedAnnotation // warn +class Test2 + +@DeprecatedAnnotation // warn +def Test3 = ??? + +@DeprecatedAnnotation // warn +val Test4 = ???