Skip to content

Commit 989c55c

Browse files
authored
Fix regression #17245: Overloaded methods with ClassTags (#18286)
The problem lied with slightly adjusted unapply of FunctionOf in a previous PR, which caused different behavior in `resolveOverloaded`, where due to a pattern match into a FunctionOf `resolveOverloaded1` would return no candidates, causing more issues later on. To keep the new behavior of FunctionOf unapply (which as a side-effect ended up fixing few issues represented with added tests), with the previous behavior of overloaded functions, we allow the method candidate filtering to fallback from the FunctionOf candidate filtering into the previous behavior in case no candidates are kept. This also fixes an additional case, which is not part of the regression, but produces an incorrect error in similar manner. Fixes #17245
2 parents 4b67b34 + 101ce3a commit 989c55c

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)