Skip to content

Commit 2b5cf1e

Browse files
committed
Avoid even more false negatives in overload pruning
The changes two commits ago were not enough to handle i21410b.scala because we end up checking: Tuple.Map[WildcardType(...), List] <: (List[Int], List[String]) which fails because a match type with a wildcard argument apparently only gets reduced when the match type case is not parameterized. To handle this more generally we use AvoidWildcardsMap to remove wildcards from the result type, but since we want to prevent false negatives we start with `variance = -1` to get a lower-bound instead of an upper-bound.
1 parent fbd55a4 commit 2b5cf1e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -2080,16 +2080,23 @@ trait Applications extends Compatibility {
20802080
* section conforms to the expected type `resultType`? If `resultType`
20812081
* is a `IgnoredProto`, pick the underlying type instead.
20822082
*
2083-
* Using approximated result types is necessary to avoid false negatives
2084-
* due to incomplete type inference such as in tests/pos/i21410.scala.
2083+
* Using an approximated result types is necessary to avoid false negatives
2084+
* due to incomplete type inference such as in tests/pos/i21410.scala and tests/pos/i21410b.scala.
20852085
*/
20862086
def resultConforms(altSym: Symbol, altType: Type, resultType: Type)(using Context): Boolean =
20872087
resultType.revealIgnored match {
20882088
case resultType: ValueType =>
20892089
altType.widen match {
20902090
case tp: PolyType => resultConforms(altSym, tp.resultType, resultType)
20912091
case tp: MethodType =>
2092-
constrainResult(altSym, wildApprox(tp.resultType), resultType)
2092+
val wildRes = wildApprox(tp.resultType)
2093+
2094+
class ResultApprox extends AvoidWildcardsMap:
2095+
// Avoid false negatives by approximating to a lower bound
2096+
variance = -1
2097+
2098+
val approx = ResultApprox()(wildRes)
2099+
constrainResult(altSym, approx, resultType)
20932100
case _ => true
20942101
}
20952102
case _ => true

tests/pos/i21410b.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test:
2+
def foo[T](x: Option[T]): T = ???
3+
def foo[T <: Tuple](x: T): Tuple.Map[T, List] = ???
4+
5+
val tup: (Int, String) = (1, "")
6+
7+
val x = foo(tup)
8+
val y: (List[Int], List[String]) = x
9+
10+
val x2: (List[Int], List[String]) = foo(tup) // error

0 commit comments

Comments
 (0)