Skip to content

Commit a938fdd

Browse files
authored
Merge pull request #11024 from dotty-staging/fix-10247
fix #10247: do not warn deprecated enum cases at declaration
2 parents 71c760b + b8878c8 commit a938fdd

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

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

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

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

Lines changed: 2 additions & 1 deletion
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")),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def usered = Color.Red // error: value Red is deprecated
2+
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+
13+
enum Color {
14+
15+
@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
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check that deprecation warnings of Red are not caught in its enclosing scope
2+
enum Color(rgb: Int) {
3+
4+
@deprecated("stop using Red", "0.1")
5+
case Red extends Color(0xff0000)
6+
7+
case Green extends Color(0x00ff00)
8+
9+
case Blue extends Color(0x0000ff)
10+
11+
final def colorCode: Option[Int] = this match {
12+
case Red => None
13+
case _ => Some(rgb)
14+
}
15+
16+
}
17+
18+
object Color {
19+
val deprecatedMembers = Set(Red)
20+
}

0 commit comments

Comments
 (0)