Skip to content

Commit 5758ebc

Browse files
authored
Merge pull request #618 from scala/backport-lts-3.3-24078
Backport "Tweak tryParameterless to use readapt" to 3.3 LTS
2 parents 382e31f + 6dbd5a1 commit 5758ebc

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
@@ -3750,16 +3750,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
37503750
typr.println(i"adapt overloaded $ref with alternatives ${altDenots map (_.info)}%\n\n %")
37513751

37523752
/** Search for an alternative that does not take parameters.
3753-
* If there is one, return it, otherwise emit an error.
3753+
* If there is one, return it, otherwise return the error tree.
37543754
*/
37553755
def tryParameterless(alts: List[TermRef])(error: => tpd.Tree): Tree =
37563756
alts.filter(_.info.isParameterless) match
3757-
case alt :: Nil => readaptSimplified(tree.withType(alt))
3758-
case _ =>
3759-
if altDenots.exists(_.info.paramInfoss == ListOfNil) then
3760-
typed(untpd.Apply(untpd.TypedSplice(tree), Nil), pt, locked)
3761-
else
3762-
error
3757+
case alt :: Nil => readaptSimplified(tree.withType(alt))
3758+
case _ =>
3759+
altDenots.find(_.info.paramInfoss == ListOfNil) match
3760+
case Some(alt) => readaptSimplified(tree.withType(alt.symbol.denot.termRef))
3761+
case _ => error
37633762

37643763
def altRef(alt: SingleDenotation) = TermRef(ref.prefix, ref.name, alt)
37653764
val alts = altDenots.map(altRef)
@@ -4438,6 +4437,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
44384437
case _ => NoType
44394438
case _ => NoType
44404439

4440+
// begin adapt1
44414441
tree match {
44424442
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[?] | _: Closure => tree
44434443
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)