Skip to content

Commit

Permalink
Add support for @deprecatedInheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal committed Nov 26, 2023
1 parent 55c2002 commit a67c70a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ class Definitions {
@tu lazy val ProvisionalSuperClassAnnot: ClassSymbol = requiredClass("scala.annotation.internal.ProvisionalSuperClass")
@tu lazy val DeprecatedAnnot: ClassSymbol = requiredClass("scala.deprecated")
@tu lazy val DeprecatedOverridingAnnot: ClassSymbol = requiredClass("scala.deprecatedOverriding")
@tu lazy val deprecatedInheritance: ClassSymbol = requiredClass("scala.deprecatedInheritance")
@tu lazy val ImplicitAmbiguousAnnot: ClassSymbol = requiredClass("scala.annotation.implicitAmbiguous")
@tu lazy val ImplicitNotFoundAnnot: ClassSymbol = requiredClass("scala.annotation.implicitNotFound")
@tu lazy val InlineParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InlineParam")
Expand Down
12 changes: 12 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ object RefChecks {
}
end checkSelfAgainstParents

/** warn if `@deprecatedInheritance` is present */
def warnDeprecatedInheritance(cls: ClassSymbol, parentTrees: List[Tree])(using Context): Unit =
val psyms = cls.parentSyms
for (psym, pos) <- psyms.zip(parentTrees.map(_.srcPos))
annot <- psym.getAnnotation(defn.deprecatedInheritance)
do
val msg = annot.argumentConstantString(0).getOrElse("")
val since = annot.argumentConstantString(1).getOrElse("")
report.deprecationWarning(em"inheritance from $psym is deprecated (since: $since): $msg", pos)

/** Check that self type of this class conforms to self types of parents
* and required classes. Also check that only `enum` constructs extend
* `java.lang.Enum` and no user-written class extends ContextFunctionN.
Expand All @@ -123,6 +133,8 @@ object RefChecks {
val psyms = cls.asClass.parentSyms
checkSelfAgainstParents(cls.asClass, psyms)

warnDeprecatedInheritance(cls.asClass, parentTrees)

def isClassExtendingJavaEnum =
!cls.isOneOf(Enum | Trait) && psyms.contains(defn.JavaEnumClass)

Expand Down
20 changes: 20 additions & 0 deletions tests/warn/i19002.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Deprecation Warning: tests/warn/i19002.scala:5:20 -------------------------------------------------------------------
5 |class TBar1 extends TFoo // warn
| ^^^^
| inheritance from trait TFoo is deprecated (since: FooLib 12.0): this class will be made final
-- Deprecation Warning: tests/warn/i19002.scala:6:20 -------------------------------------------------------------------
6 |trait TBar2 extends TFoo // warn
| ^^^^
| inheritance from trait TFoo is deprecated (since: FooLib 12.0): this class will be made final
-- Deprecation Warning: tests/warn/i19002.scala:10:20 ------------------------------------------------------------------
10 |class CBar1 extends CFoo // warn
| ^^^^
| inheritance from class CFoo is deprecated (since: FooLib 11.0): this class will be made final
-- Deprecation Warning: tests/warn/i19002.scala:14:20 ------------------------------------------------------------------
14 |class ABar1 extends AFoo // warn
| ^^^^
| inheritance from class AFoo is deprecated (since: FooLib 10.0): this class will be made final
-- Deprecation Warning: tests/warn/i19002.scala:15:20 ------------------------------------------------------------------
15 |trait ABar2 extends AFoo // warn
| ^^^^
| inheritance from class AFoo is deprecated (since: FooLib 10.0): this class will be made final
15 changes: 15 additions & 0 deletions tests/warn/i19002.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//> using options -deprecation

@deprecatedInheritance("this class will be made final", "FooLib 12.0")
trait TFoo
class TBar1 extends TFoo // warn
trait TBar2 extends TFoo // warn

@deprecatedInheritance("this class will be made final", "FooLib 11.0")
class CFoo
class CBar1 extends CFoo // warn

@deprecatedInheritance("this class will be made final", "FooLib 10.0")
abstract class AFoo
class ABar1 extends AFoo // warn
trait ABar2 extends AFoo // warn

0 comments on commit a67c70a

Please sign in to comment.