Skip to content

Commit 1aa3372

Browse files
authored
Do not issue deprecation warnings when declaring deprecated case classes (#17165)
2 parents 6e2b576 + 351f602 commit 1aa3372

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ object Flags {
604604
val Scala2Trait: FlagSet = Scala2x | Trait
605605
val SyntheticArtifact: FlagSet = Synthetic | Artifact
606606
val SyntheticCase: FlagSet = Synthetic | Case
607+
val SyntheticMethod: FlagSet = Synthetic | Method
607608
val SyntheticModule: FlagSet = Synthetic | Module
608609
val SyntheticOpaque: FlagSet = Synthetic | Opaque
609610
val SyntheticParam: FlagSet = Synthetic | Param

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,22 @@ class CrossVersionChecks extends MiniPhase:
4949
owner.isDeprecated
5050
|| isEnumOwner(owner)
5151

52-
/**Scan the chain of outer declaring scopes from the current context
52+
/**Skip warnings for synthetic members of case classes during declaration and
53+
* scan the chain of outer declaring scopes from the current context
5354
* a deprecation warning will be skipped if one the following holds
5455
* for a given declaring scope:
5556
* - the symbol associated with the scope is also deprecated.
5657
* - if and only if `sym` is an enum case, the scope is either
5758
* a module that declares `sym`, or the companion class of the
5859
* module that declares `sym`.
5960
*/
60-
def skipWarning(using Context) =
61-
ctx.owner.ownersIterator.exists(if sym.isEnumCase then isDeprecatedOrEnum else _.isDeprecated)
61+
def skipWarning(using Context): Boolean =
62+
(ctx.owner.is(Synthetic) && sym.is(CaseClass))
63+
|| ctx.owner.ownersIterator.exists(if sym.isEnumCase then isDeprecatedOrEnum else _.isDeprecated)
6264

63-
for annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
65+
// Also check for deprecation of the companion class for synthetic methods
66+
val toCheck = sym :: (if sym.isAllOf(SyntheticMethod) then sym.owner.companionClass :: Nil else Nil)
67+
for sym <- toCheck; annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
6468
if !skipWarning then
6569
val msg = annot.argumentConstant(0).map(": " + _.stringValue).getOrElse("")
6670
val since = annot.argumentConstant(1).map(" since " + _.stringValue).getOrElse("")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Error: tests/neg-custom-args/deprecation/i11022.scala:8:7 -----------------------------------------------------------
2+
8 |val a: CaseClass = CaseClass(42) // error: deprecated type // error: deprecated apply method
3+
| ^^^^^^^^^
4+
| class CaseClass is deprecated: no CaseClass
5+
-- Error: tests/neg-custom-args/deprecation/i11022.scala:8:19 ----------------------------------------------------------
6+
8 |val a: CaseClass = CaseClass(42) // error: deprecated type // error: deprecated apply method
7+
| ^^^^^^^^^
8+
| class CaseClass is deprecated: no CaseClass
9+
-- Error: tests/neg-custom-args/deprecation/i11022.scala:9:7 -----------------------------------------------------------
10+
9 |val b: CaseClass = new CaseClass(42) // error: deprecated type // error: deprecated class
11+
| ^^^^^^^^^
12+
| class CaseClass is deprecated: no CaseClass
13+
-- Error: tests/neg-custom-args/deprecation/i11022.scala:9:23 ----------------------------------------------------------
14+
9 |val b: CaseClass = new CaseClass(42) // error: deprecated type // error: deprecated class
15+
| ^^^^^^^^^
16+
| class CaseClass is deprecated: no CaseClass
17+
-- Error: tests/neg-custom-args/deprecation/i11022.scala:10:14 ---------------------------------------------------------
18+
10 |val c: Unit = CaseClass(42).magic() // error: deprecated apply method
19+
| ^^^^^^^^^
20+
| class CaseClass is deprecated: no CaseClass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@deprecated("no CaseClass")
2+
case class CaseClass(rgb: Int):
3+
def magic(): Unit = ()
4+
5+
object CaseClass:
6+
def notDeprecated(): Unit = ()
7+
8+
val a: CaseClass = CaseClass(42) // error: deprecated type // error: deprecated apply method
9+
val b: CaseClass = new CaseClass(42) // error: deprecated type // error: deprecated class
10+
val c: Unit = CaseClass(42).magic() // error: deprecated apply method
11+
val d: Unit = CaseClass.notDeprecated() // compiles

tests/pos/i11022.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// scalac: -Werror -deprecation
2+
@deprecated("no CaseClass")
3+
case class CaseClass(rgb: Int)

0 commit comments

Comments
 (0)