Skip to content
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

Do not issue deprecation warnings when declaring deprecated case classes #17165

Merged
merged 1 commit into from
May 1, 2023
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ object Flags {
val Scala2Trait: FlagSet = Scala2x | Trait
val SyntheticArtifact: FlagSet = Synthetic | Artifact
val SyntheticCase: FlagSet = Synthetic | Case
val SyntheticMethod: FlagSet = Synthetic | Method
val SyntheticModule: FlagSet = Synthetic | Module
val SyntheticOpaque: FlagSet = Synthetic | Opaque
val SyntheticParam: FlagSet = Synthetic | Param
Expand Down
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@ class CrossVersionChecks extends MiniPhase:
owner.isDeprecated
|| isEnumOwner(owner)

/**Scan the chain of outer declaring scopes from the current context
/**Skip warnings for synthetic members of case classes during declaration and
* 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)
def skipWarning(using Context): Boolean =
(ctx.owner.is(Synthetic) && sym.is(CaseClass))
|| ctx.owner.ownersIterator.exists(if sym.isEnumCase then isDeprecatedOrEnum else _.isDeprecated)

for annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
// Also check for deprecation of the companion class for synthetic methods
val toCheck = sym :: (if sym.isAllOf(SyntheticMethod) then sym.owner.companionClass :: Nil else Nil)
for sym <- toCheck; annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
if !skipWarning then
val msg = annot.argumentConstant(0).map(": " + _.stringValue).getOrElse("")
val since = annot.argumentConstant(1).map(" since " + _.stringValue).getOrElse("")
Expand Down
20 changes: 20 additions & 0 deletions tests/neg-custom-args/deprecation/i11022.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Error: tests/neg-custom-args/deprecation/i11022.scala:8:7 -----------------------------------------------------------
8 |val a: CaseClass = CaseClass(42) // error: deprecated type // error: deprecated apply method
| ^^^^^^^^^
| class CaseClass is deprecated: no CaseClass
-- Error: tests/neg-custom-args/deprecation/i11022.scala:8:19 ----------------------------------------------------------
8 |val a: CaseClass = CaseClass(42) // error: deprecated type // error: deprecated apply method
| ^^^^^^^^^
| class CaseClass is deprecated: no CaseClass
-- Error: tests/neg-custom-args/deprecation/i11022.scala:9:7 -----------------------------------------------------------
9 |val b: CaseClass = new CaseClass(42) // error: deprecated type // error: deprecated class
| ^^^^^^^^^
| class CaseClass is deprecated: no CaseClass
-- Error: tests/neg-custom-args/deprecation/i11022.scala:9:23 ----------------------------------------------------------
9 |val b: CaseClass = new CaseClass(42) // error: deprecated type // error: deprecated class
| ^^^^^^^^^
| class CaseClass is deprecated: no CaseClass
-- Error: tests/neg-custom-args/deprecation/i11022.scala:10:14 ---------------------------------------------------------
10 |val c: Unit = CaseClass(42).magic() // error: deprecated apply method
| ^^^^^^^^^
| class CaseClass is deprecated: no CaseClass
11 changes: 11 additions & 0 deletions tests/neg-custom-args/deprecation/i11022.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@deprecated("no CaseClass")
case class CaseClass(rgb: Int):
def magic(): Unit = ()

object CaseClass:
def notDeprecated(): Unit = ()

val a: CaseClass = CaseClass(42) // error: deprecated type // error: deprecated apply method
val b: CaseClass = new CaseClass(42) // error: deprecated type // error: deprecated class
val c: Unit = CaseClass(42).magic() // error: deprecated apply method
val d: Unit = CaseClass.notDeprecated() // compiles
3 changes: 3 additions & 0 deletions tests/pos/i11022.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// scalac: -Werror -deprecation
@deprecated("no CaseClass")
case class CaseClass(rgb: Int)