Skip to content

Commit 67462b5

Browse files
committed
Fixes x.toString causing auto-application warnings under -Xsource:2.14
Fixes scala/bug#11639 Fixes scala/bug#11657 The compiler gets confused about the lineage of `toString` apparently, so instead of trying to look for the Java flag, I am just going to see if the name exists under `AnyTpe`.
1 parent 8948439 commit 67462b5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
959959
doIt
960960
}
961961

962+
def isAnyDerived(sym: Symbol): Boolean = AnyTpe.nonPrivateMember(sym.name) != NoSymbol
962963

963964
// (4.2) condition for auto-application by -Xsource level
964965
//
@@ -971,10 +972,11 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
971972
// > val vparamSymssOrEmptyParamsFromOverride =
972973
// This means an accessor that overrides a Java-defined method gets a MethodType instead of a NullaryMethodType, which breaks lots of assumptions about accessors)
973974
def checkCanAutoApply(): Boolean = {
974-
if (sourceLevel2_14 && !meth.isJavaDefined)
975+
if (sourceLevel2_14 && !meth.isJavaDefined && !isAnyDerived(meth)) {
975976
context.deprecationWarning(tree.pos, NoSymbol, s"Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method ${meth.decodedName},\n" +
976977
s"or remove the empty argument list from its definition (Java-defined methods are exempt).\n"+
977978
s"In Scala 3, an unapplied method like this will be eta-expanded into a function.", "2.14.0")
979+
}
978980
true
979981
}
980982

test/files/pos/auto-application.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// scalac: -Werror -deprecation -Xsource:2.14
2+
//
3+
class Test {
4+
def a1(xs: List[String]): Int = xs.hashCode
5+
def a2(xs: List[String]): Int = xs.hashCode()
6+
def a3(xs: List[String]): String = xs.toString
7+
def a4(xs: List[String]): String = xs.toString()
8+
def a5(xs: List[String]): Class[_] = xs.getClass
9+
def a6(xs: List[String]): Class[_] = xs.getClass()
10+
def a7(xs: List[String]): Int = xs.##
11+
def a8(xs: List[String]): Int = xs.##()
12+
def a9(x: Address): String = x.toString
13+
def a10(x: Address): String = x.toString()
14+
def a11(x: A): String = x.toString
15+
def a12(x: A): String = x.toString()
16+
def a13(x: B): String = x.toString
17+
def a14(x: B): String = x.toString()
18+
}
19+
20+
case class Address()
21+
22+
class A() {
23+
override def toString(): String = "A()"
24+
}
25+
26+
class B() {
27+
override def toString: String = "B()"
28+
}
29+
30+
case class C(c: Int) extends AnyVal
31+

0 commit comments

Comments
 (0)