Skip to content

Commit b8878c8

Browse files
committed
fix #10247: do not warn deprecated enum cases at declaration
1 parent 0686ed1 commit b8878c8

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,32 @@ object RefChecks {
888888
* in either a deprecated member or a scala bridge method, issue a warning.
889889
*/
890890
private def checkDeprecated(sym: Symbol, pos: SrcPos)(using Context): Unit =
891+
892+
/** is the owner an enum or its companion and also the owner of sym */
893+
def isEnumOwner(owner: Symbol)(using Context) =
894+
// pre: sym is an enumcase
895+
if owner.isEnumClass then owner.companionClass eq sym.owner
896+
else if owner.is(ModuleClass) && owner.companionClass.isEnumClass then owner eq sym.owner
897+
else false
898+
899+
def isDeprecatedOrEnum(owner: Symbol)(using Context) =
900+
// pre: sym is an enumcase
901+
owner.isDeprecated
902+
|| isEnumOwner(owner)
903+
904+
/**Scan the chain of outer declaring scopes from the current context
905+
* a deprecation warning will be skipped if one the following holds
906+
* for a given declaring scope:
907+
* - the symbol associated with the scope is also deprecated.
908+
* - if and only if `sym` is an enum case, the scope is either
909+
* a module that declares `sym`, or the companion class of the
910+
* module that declares `sym`.
911+
*/
912+
def skipWarning(using Context) =
913+
ctx.owner.ownersIterator.exists(if sym.isEnumCase then isDeprecatedOrEnum else _.isDeprecated)
914+
891915
for annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
892-
if !ctx.owner.ownersIterator.exists(_.isDeprecated) then
916+
if !skipWarning then
893917
val msg = annot.argumentConstant(0).map(": " + _.stringValue).getOrElse("")
894918
val since = annot.argumentConstant(1).map(" since " + _.stringValue).getOrElse("")
895919
report.deprecationWarning(s"${sym.showLocated} is deprecated${since}${msg}", pos)

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class CompilationTests {
3232
compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16),
3333
compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
3434
compileFile("tests/pos-special/sourcepath/outer/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
35-
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-feature")),
35+
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-deprecation", "-feature")),
36+
compileFile("tests/pos-special/avoid-warn-deprecation.scala", defaultOptions.and("-Xfatal-warnings", "-feature")),
3637
compileFilesInDir("tests/pos-special/spec-t5545", defaultOptions),
3738
compileFilesInDir("tests/pos-special/strawman-collections", allowDeepSubtypes),
3839
compileFilesInDir("tests/pos-special/isInstanceOf", allowDeepSubtypes.and("-Xfatal-warnings")),
@@ -55,7 +56,6 @@ class CompilationTests {
5556
compileFile("tests/pos-special/kind-projector.scala", defaultOptions.and("-Ykind-projector")),
5657
compileFile("tests/run/i5606.scala", defaultOptions.and("-Yretain-trees")),
5758
compileFile("tests/pos-custom-args/i5498-postfixOps.scala", defaultOptions withoutLanguageFeature "postfixOps"),
58-
compileFilesInDir("tests/pos-custom-args/deprecation", defaultOptions.and("-deprecation", "-Xfatal-warnings")),
5959
compileFile("tests/pos-custom-args/i8875.scala", defaultOptions.and("-Xprint:getters")),
6060
compileFile("tests/pos-custom-args/i9267.scala", defaultOptions.and("-Ystop-after:erasure")),
6161
compileFile("tests/pos-special/extend-java-enum.scala", defaultOptions.and("-source", "3.0-migration")),
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
def usered = Color.Red // error: value Red is deprecated
22

3+
object DeprecatedContainer {
4+
@deprecated("no foo", "0.1") val foo = 23
5+
}
6+
7+
enum Day {
8+
9+
@deprecated("no more Mondays!", "0.1") case Monday
10+
11+
}
12+
313
enum Color {
14+
415
@deprecated("no Red", "0.1") case Red
16+
17+
@deprecated("no Generic", "0.1") case Generic(rgb: Int)
18+
19+
def useFoo1 = DeprecatedContainer.foo // error // check that only enum cases are avoided
20+
def useMonday = Day.Monday // error // check that enum cases are declared in this enum
21+
22+
}
23+
24+
object Color {
25+
def useFoo2 = DeprecatedContainer.foo // error // check that only enum cases are avoided
526
}

0 commit comments

Comments
 (0)