Skip to content

fix #10247: do not warn deprecated enum cases at declaration #11024

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 2 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,32 @@ object RefChecks {
* in either a deprecated member or a scala bridge method, issue a warning.
*/
private def checkDeprecated(sym: Symbol, pos: SrcPos)(using Context): Unit =

/** is the owner an enum or its companion and also the owner of sym */
def isEnumOwner(owner: Symbol)(using Context) =
// pre: sym is an enumcase
if owner.isEnumClass then owner.companionClass eq sym.owner
else if owner.is(ModuleClass) && owner.companionClass.isEnumClass then owner eq sym.owner
else false

def isDeprecatedOrEnum(owner: Symbol)(using Context) =
// pre: sym is an enumcase
owner.isDeprecated
|| isEnumOwner(owner)

/**Scan the chain of outer declaring scopes from the current context
* a deprecation warning will be skipped if one the following holds
* for a given declaring scope:
* - the symbol associated with the scope is also deprecated.
* - if and only if `sym` is an enum case, the scope is either
* a module that declares `sym`, or the companion class of the
* module that declares `sym`.
*/
def skipWarning(using Context) =
ctx.owner.ownersIterator.exists(if sym.isEnumCase then isDeprecatedOrEnum else _.isDeprecated)

for annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
if !ctx.owner.ownersIterator.exists(_.isDeprecated) then
if !skipWarning then
val msg = annot.argumentConstant(0).map(": " + _.stringValue).getOrElse("")
val since = annot.argumentConstant(1).map(" since " + _.stringValue).getOrElse("")
report.deprecationWarning(s"${sym.showLocated} is deprecated${since}${msg}", pos)
Expand Down
3 changes: 2 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class CompilationTests {
compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16),
compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFile("tests/pos-special/sourcepath/outer/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-feature")),
compileFilesInDir("tests/pos-special/fatal-warnings", defaultOptions.and("-Xfatal-warnings", "-deprecation", "-feature")),
compileFile("tests/pos-special/avoid-warn-deprecation.scala", defaultOptions.and("-Xfatal-warnings", "-feature")),
compileFilesInDir("tests/pos-special/spec-t5545", defaultOptions),
compileFilesInDir("tests/pos-special/strawman-collections", allowDeepSubtypes),
compileFilesInDir("tests/pos-special/isInstanceOf", allowDeepSubtypes.and("-Xfatal-warnings")),
Expand Down
26 changes: 26 additions & 0 deletions tests/neg-custom-args/deprecation/i10247.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def usered = Color.Red // error: value Red is deprecated

object DeprecatedContainer {
@deprecated("no foo", "0.1") val foo = 23
}

enum Day {

@deprecated("no more Mondays!", "0.1") case Monday

}

enum Color {

@deprecated("no Red", "0.1") case Red

@deprecated("no Generic", "0.1") case Generic(rgb: Int)

def useFoo1 = DeprecatedContainer.foo // error // check that only enum cases are avoided
def useMonday = Day.Monday // error // check that enum cases are declared in this enum

}

object Color {
def useFoo2 = DeprecatedContainer.foo // error // check that only enum cases are avoided
}
20 changes: 20 additions & 0 deletions tests/pos-special/fatal-warnings/i10247.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// check that deprecation warnings of Red are not caught in its enclosing scope
enum Color(rgb: Int) {

@deprecated("stop using Red", "0.1")
case Red extends Color(0xff0000)

case Green extends Color(0x00ff00)

case Blue extends Color(0x0000ff)

final def colorCode: Option[Int] = this match {
case Red => None
case _ => Some(rgb)
}

}

object Color {
val deprecatedMembers = Set(Red)
}