Skip to content

Commit 0eee3b0

Browse files
authored
Tweak tryParameterless to use readapt (#24078)
Readapt will autoapply if permitted and also warn on migration. Fixes #21207 ### Why was the ticket worth tackling? I started to look at tickets about overloading, so this is a first step on the journey of understanding the parts involved. It happens that I backported `tryParameterless` to Scala 2. ### How I fixed it There are just two places where it adds parens. My first attempt was more direct, since I want only a possible warning and parens: ``` alt.info match case wtp: MethodType => adaptNoArgsUnappliedMethod(wtp, functionExpected = false, arity = -1) ``` but the tree has an overloaded type. ### Why is this PR worth reviewing? It's a one-line change, so now I'm on pace for 1 LOC/day, which is net zero. ### What's the worse that could happen? The fix is a bit indirect, so it's easy to imagine it breaking under strain.
2 parents e95afce + c5184de commit 0eee3b0

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,16 +4274,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42744274
typr.println(i"adapt overloaded $ref with alternatives ${altDenots map (_.info)}%\n\n %")
42754275

42764276
/** Search for an alternative that does not take parameters.
4277-
* If there is one, return it, otherwise emit an error.
4277+
* If there is one, return it, otherwise return the error tree.
42784278
*/
42794279
def tryParameterless(alts: List[TermRef])(error: => tpd.Tree): Tree =
42804280
alts.filter(_.info.isParameterless) match
4281-
case alt :: Nil => readaptSimplified(tree.withType(alt))
4282-
case _ =>
4283-
if altDenots.exists(_.info.paramInfoss == ListOfNil) then
4284-
typed(untpd.Apply(untpd.TypedSplice(tree), Nil), pt, locked)
4285-
else
4286-
error
4281+
case alt :: Nil => readaptSimplified(tree.withType(alt))
4282+
case _ =>
4283+
altDenots.find(_.info.paramInfoss == ListOfNil) match
4284+
case Some(alt) => readaptSimplified(tree.withType(alt.symbol.denot.termRef))
4285+
case _ => error
42874286

42884287
def altRef(alt: SingleDenotation) = TermRef(ref.prefix, ref.name, alt)
42894288
val alts = altDenots.map(altRef)
@@ -4976,6 +4975,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
49764975
case _ => NoType
49774976
case _ => NoType
49784977

4978+
// begin adapt1
49794979
tree match {
49804980
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[?] | _: Closure => tree
49814981
case _ => tree.tpe.widen match {

tests/neg/i21207.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
abstract class P {
3+
def foo1(): Int = 1
4+
def foo2(x: Int): Int = 22
5+
def g4(s: String): String = s * 4
6+
}
7+
8+
class C extends P {
9+
def foo1(x: Int): Int = 11
10+
def foo2(): Int = 2
11+
12+
def foo3(): Int = 3
13+
def foo3(x: Int): Int = 33
14+
15+
def g4(): Int = 4
16+
}
17+
18+
object Test extends App {
19+
val c = new C
20+
println(c.foo1) // error was omitted because of nullary fallback during ambiguous overload resolution
21+
println(c.foo2) // error, add parens
22+
println(c.foo3) // error
23+
println(c.g4) // error
24+
val s: String = c.g4 // error missing arg, expected type picks method
25+
println(s)
26+
}
27+
28+
/* Scala 2 warns for all three selections:
29+
warning: Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method foo1,
30+
or remove the empty argument list from its definition (Java-defined methods are exempt).
31+
In Scala 3, an unapplied method like this will be eta-expanded into a function. [quickfixable]
32+
*/

tests/warn/i21207/Over.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
interface I {
3+
default int f() { return 42; }
4+
}
5+
6+
public class Over implements I {
7+
public int f(int i) { return 42 + i; }
8+
}

tests/warn/i21207/usage.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//> using options -source:3.0-migration
2+
3+
// Hope to see a migration warning! with quickfix.
4+
5+
class P {
6+
def g(): Int = 42
7+
}
8+
class C extends P {
9+
def g(i: Int): Int = 42 + i
10+
}
11+
12+
object Test extends App {
13+
val over = Over()
14+
println(over.f) // nowarn Java
15+
val c = C()
16+
println(c.g) // warn migration
17+
}

0 commit comments

Comments
 (0)