Skip to content

Commit 351f602

Browse files
committed
Fix deprecation check and add tests
1 parent 824295e commit 351f602

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
@@ -52,18 +52,22 @@ class CrossVersionChecks extends MiniPhase:
5252
owner.isDeprecated
5353
|| isEnumOwner(owner)
5454

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

66-
for annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
68+
// Also check for deprecation of the companion class for synthetic methods
69+
val toCheck = sym :: (if sym.isAllOf(SyntheticMethod) then sym.owner.companionClass :: Nil else Nil)
70+
for sym <- toCheck; annot <- sym.getAnnotation(defn.DeprecatedAnnot) do
6771
if !skipWarning then
6872
val msg = annot.argumentConstant(0).map(": " + _.stringValue).getOrElse("")
6973
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)