Skip to content

Commit 65095d1

Browse files
committed
In case apply rewrite, only issue deprecation when using companion
When a case apply is rewritten to the constructor, only warn about the class being deprecated if the companion object was used directly. ``` @deprecated case class C(x: Int) C(1) // warn val Alias = C Alias(1) // no warn ``` The warning was introduced in PRs 4955 and 5646. Scala 2.12 doesn't perform the case apply rewrite if the receiver is an alias, but that changed in 2.13 in PR 10560, where the workaround for fixing issue 5626 was narrowed down.
1 parent 41c795e commit 65095d1

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

src/compiler/scala/tools/nsc/typechecker/RefChecks.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,15 +1233,16 @@ abstract class RefChecks extends Transform {
12331233
// Transformation ------------------------------------------------------------
12341234

12351235
/* Convert a reference to a case factory of type `tpe` to a new of the class it produces. */
1236-
def toConstructor(pos: Position, tpe: Type): Tree = {
1236+
def toConstructor(pos: Position, tpe: Type, checkTarget: Boolean): Tree = {
12371237
val rtpe = tpe.finalResultType
12381238
assert(rtpe.typeSymbol hasFlag CASE, tpe)
12391239
val tree = localTyper.typedOperator {
12401240
atPos(pos) {
12411241
Select(New(TypeTree(rtpe)), rtpe.typeSymbol.primaryConstructor)
12421242
}
12431243
}
1244-
checkUndesiredProperties(rtpe.typeSymbol, tree.pos)
1244+
if (checkTarget)
1245+
checkUndesiredProperties(rtpe.typeSymbol, tree.pos)
12451246
checkUndesiredProperties(rtpe.typeSymbol.primaryConstructor, tree.pos)
12461247
tree
12471248
}
@@ -1667,8 +1668,7 @@ abstract class RefChecks extends Transform {
16671668
case x => throw new MatchError(x)
16681669
}
16691670
sym.name == nme.apply &&
1670-
!sym.hasStableFlag && // ???
1671-
sym.isCase &&
1671+
sym.isCase && // only synthetic case apply methods
16721672
isClassTypeAccessible(tree) &&
16731673
!tree.tpe.finalResultType.typeSymbol.primaryConstructor.isLessAccessibleThan(tree.symbol)
16741674
}
@@ -1689,7 +1689,12 @@ abstract class RefChecks extends Transform {
16891689
case _ =>
16901690
}
16911691
loop(tree)
1692-
toConstructor(tree.pos, tree.tpe)
1692+
def qualIsModule(t: Tree): Boolean = t match {
1693+
case TypeApply(qual, _) => qualIsModule(qual)
1694+
case Select(qual, _) => qual.symbol == tree.symbol.owner.module
1695+
case _ => true
1696+
}
1697+
toConstructor(tree.pos, tree.tpe, qualIsModule(tree))
16931698
}
16941699

16951700
private def transformApply(tree: Apply): Tree = tree match {

test/files/neg/t13006.check

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
t13006.scala:7: warning: class C is deprecated
2+
type X[T] = C[T] // warn
3+
^
4+
t13006.scala:17: warning: class C is deprecated
5+
def t3 = C.apply(20) // warn
6+
^
7+
t13006.scala:18: warning: class C is deprecated
8+
def t4 = new C(10) // warn
9+
^
10+
t13006.scala:21: warning: constructor D in class D is deprecated
11+
def u1 = A.Y.apply(10) // warn
12+
^
13+
t13006.scala:22: warning: constructor D in class D is deprecated
14+
def u2 = new A.Y(10) // warn
15+
^
16+
t13006.scala:23: warning: constructor D in class D is deprecated
17+
def u3 = D.apply(10) // warn
18+
^
19+
t13006.scala:24: warning: constructor D in class D is deprecated
20+
def u4 = new D(10) // warn
21+
^
22+
error: No warnings can be incurred under -Werror.
23+
7 warnings
24+
1 error

test/files/neg/t13006.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//> using options -deprecation -Werror
2+
3+
@deprecated case class C[T](x: T)
4+
case class D @deprecated() (x: Int)
5+
6+
object A {
7+
type X[T] = C[T] // warn
8+
val X = C // no warn
9+
10+
type Y = D // no warn
11+
val Y = D // no warn
12+
}
13+
14+
class T {
15+
def t1 = A.X.apply(10) // no warn
16+
def t2 = new A.X(10) // no warn
17+
def t3 = C.apply(20) // warn
18+
def t4 = new C(10) // warn
19+
def t5 = C.hashCode // no warn
20+
21+
def u1 = A.Y.apply(10) // warn
22+
def u2 = new A.Y(10) // warn
23+
def u3 = D.apply(10) // warn
24+
def u4 = new D(10) // warn
25+
def u5(d: D) = 10 // no warn
26+
}

0 commit comments

Comments
 (0)