Skip to content

Commit 101ce3a

Browse files
committed
Fix regression with Overloaded methods returning Functions
Before the regression, FunctionOf unapply would not try dealiasing, meaning that an aliased function type would be handled by a general case. To fix that, instead of handling Function types separately when filtering overloaded methods in `resolveOverloaded1`, we allow to fallback to the general case if the previous one returns nothing. Along with fixing the regression, this also improves other cases, one of which was added to the test. Readd a separate FunctionOf case, but with a fallback
1 parent 97677cc commit 101ce3a

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,31 +2059,35 @@ trait Applications extends Compatibility {
20592059
if isDetermined(alts2) then alts2
20602060
else resolveMapped(alts1, _.widen.appliedTo(targs1.tpes), pt1)
20612061

2062-
case defn.FunctionOf(args, resultType, _) =>
2063-
narrowByTypes(alts, args, resultType)
2064-
20652062
case pt =>
2066-
val compat = alts.filterConserve(normalizedCompatible(_, pt, keepConstraint = false))
2067-
if (compat.isEmpty)
2068-
/*
2069-
* the case should not be moved to the enclosing match
2070-
* since SAM type must be considered only if there are no candidates
2071-
* For example, the second f should be chosen for the following code:
2072-
* def f(x: String): Unit = ???
2073-
* def f: java.io.OutputStream = ???
2074-
* new java.io.ObjectOutputStream(f)
2075-
*/
2076-
pt match {
2077-
case SAMType(mtp, _) =>
2078-
narrowByTypes(alts, mtp.paramInfos, mtp.resultType)
2079-
case _ =>
2080-
// pick any alternatives that are not methods since these might be convertible
2081-
// to the expected type, or be used as extension method arguments.
2082-
val convertible = alts.filterNot(alt =>
2083-
normalize(alt, IgnoredProto(pt)).widenSingleton.isInstanceOf[MethodType])
2084-
if convertible.length == 1 then convertible else compat
2085-
}
2086-
else compat
2063+
val compat0 = pt match
2064+
case defn.FunctionOf(args, resType, _) =>
2065+
narrowByTypes(alts, args, resType)
2066+
case _ =>
2067+
Nil
2068+
if (compat0.isEmpty) then
2069+
val compat = alts.filterConserve(normalizedCompatible(_, pt, keepConstraint = false))
2070+
if (compat.isEmpty)
2071+
/*
2072+
* the case should not be moved to the enclosing match
2073+
* since SAM type must be considered only if there are no candidates
2074+
* For example, the second f should be chosen for the following code:
2075+
* def f(x: String): Unit = ???
2076+
* def f: java.io.OutputStream = ???
2077+
* new java.io.ObjectOutputStream(f)
2078+
*/
2079+
pt match {
2080+
case SAMType(mtp, _) =>
2081+
narrowByTypes(alts, mtp.paramInfos, mtp.resultType)
2082+
case _ =>
2083+
// pick any alternatives that are not methods since these might be convertible
2084+
// to the expected type, or be used as extension method arguments.
2085+
val convertible = alts.filterNot(alt =>
2086+
normalize(alt, IgnoredProto(pt)).widenSingleton.isInstanceOf[MethodType])
2087+
if convertible.length == 1 then convertible else compat
2088+
}
2089+
else compat
2090+
else compat0
20872091
}
20882092

20892093
/** The type of alternative `alt` after instantiating its first parameter

tests/pos/i17245.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.reflect.ClassTag
2+
3+
trait MockSettings
4+
5+
object Mockito {
6+
def mock[T : ClassTag]: T = ???
7+
def mock[T : ClassTag](settings: MockSettings): T = ???
8+
}
9+
10+
trait Channel
11+
type OnChannel = Channel => Any
12+
13+
@main def Test =
14+
val case1: OnChannel = Mockito.mock[OnChannel]
15+
val case2: OnChannel = Mockito.mock
16+
val case3 = Mockito.mock[OnChannel]
17+
val case4: OnChannel = Mockito.mock[OnChannel](summon[ClassTag[OnChannel]])
18+
19+
// not a regressive case, but an added improvement with the fix for the above
20+
val case5: Channel => Any = Mockito.mock[Channel => Any]

0 commit comments

Comments
 (0)