Skip to content

Commit 23089ad

Browse files
committed
Avoid deep recursion in sizeFits
Only a single `apply` is ever inserted, so `apply` members of `apply` methods never should count as applicable.
1 parent 767b4b8 commit 23089ad

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
15661566
case x => x
15671567
}
15681568

1569-
def sizeFits(alt: TermRef): Boolean = alt.widen.stripPoly match {
1569+
def sizeFits(alt: TermRef, followApply: Boolean): Boolean = alt.widen.stripPoly match {
15701570
case tp: MethodType =>
15711571
val ptypes = tp.paramInfos
15721572
val numParams = ptypes.length
@@ -1577,11 +1577,12 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
15771577
else if (numParams > numArgs + 1) hasDefault
15781578
else isVarArgs || hasDefault
15791579
case tp =>
1580-
numArgs == 0 || onMethod(tp, followApply = true)(sizeFits)
1580+
numArgs == 0 ||
1581+
followApply && onMethod(tp, followApply = true)(sizeFits(_, followApply = false))
15811582
}
15821583

15831584
def narrowBySize(alts: List[TermRef]): List[TermRef] =
1584-
alts.filter(sizeFits)
1585+
alts.filter(sizeFits(_, followApply = true))
15851586

15861587
def narrowByShapes(alts: List[TermRef]): List[TermRef] = {
15871588
if (normArgs exists untpd.isFunctionWithUnknownParamType)

tests/neg/i6450.scala

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,62 @@ object B {
66
def f(x: Any): Int = 2
77
lazy val f = new A {}
88
val x = f(null) // error: ambiguous
9+
}
10+
11+
object Test {
12+
def f(x: Any) = 10
13+
val f: Foo123 = f(1) // error: Not found: type Foo123
14+
15+
}
16+
17+
object Test2 {
18+
trait A {
19+
def apply(x: AnyRef): Int
20+
}
21+
object B {
22+
def f(e: Any): A = ???
23+
val f: A = f(null)
24+
}
25+
}
26+
27+
object Test3 {
28+
trait A {
29+
def apply(x: String): Int
30+
}
31+
object B {
32+
def f(e: CharSequence): A = ???
33+
val f: A = f(null)
34+
}
35+
}
36+
37+
38+
object Test4 {
39+
trait A {
40+
def apply(x: Any): Int
41+
}
42+
object B {
43+
def f(e: Any): A = ???
44+
val f: A = f(null)
45+
}
46+
}
47+
48+
object Test5 {
49+
trait A {
50+
def apply(x: Any): Int
51+
}
52+
object B {
53+
def f(e: AnyRef): A = ???
54+
val f: A = f(null)
55+
}
56+
}
57+
58+
59+
object Test6 {
60+
trait A {
61+
def apply(x: CharSequence): Int
62+
}
63+
object B {
64+
def f(e: String): A = ???
65+
val f: A = f(null)
66+
}
967
}

0 commit comments

Comments
 (0)