Skip to content

Commit 4873728

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 744d5ca commit 4873728

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
@@ -1564,7 +1564,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
15641564
case x => x
15651565
}
15661566

1567-
def sizeFits(alt: TermRef): Boolean = alt.widen.stripPoly match {
1567+
def sizeFits(alt: TermRef, followApply: Boolean): Boolean = alt.widen.stripPoly match {
15681568
case tp: MethodType =>
15691569
val ptypes = tp.paramInfos
15701570
val numParams = ptypes.length
@@ -1575,11 +1575,12 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
15751575
else if (numParams > numArgs + 1) hasDefault
15761576
else isVarArgs || hasDefault
15771577
case tp =>
1578-
numArgs == 0 || onMethod(tp, followApply = true)(sizeFits)
1578+
numArgs == 0 ||
1579+
followApply && onMethod(tp, followApply = true)(sizeFits(_, followApply = false))
15791580
}
15801581

15811582
def narrowBySize(alts: List[TermRef]): List[TermRef] =
1582-
alts.filter(sizeFits)
1583+
alts.filter(sizeFits(_, followApply = true))
15831584

15841585
def narrowByShapes(alts: List[TermRef]): List[TermRef] = {
15851586
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)