Skip to content

Commit 32ac2e6

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 f7259cf commit 32ac2e6

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Applications.scala

+10-3
Original file line numberDiff line numberDiff line change
@@ -2122,16 +2122,23 @@ trait Applications extends Compatibility {
21222122
* section conforms to the expected type `resultType`? If `resultType`
21232123
* is a `IgnoredProto`, pick the underlying type instead.
21242124
*
2125-
* Using approximated result types is necessary to avoid false negatives
2126-
* due to incomplete type inference such as in tests/pos/i21410.scala.
2125+
* Using an approximated result type is necessary to avoid false negatives
2126+
* due to incomplete type inference such as in tests/pos/i21410.scala and tests/pos/i21410b.scala.
21272127
*/
21282128
def resultConforms(altSym: Symbol, altType: Type, resultType: Type)(using Context): Boolean =
21292129
resultType.revealIgnored match {
21302130
case resultType: ValueType =>
21312131
altType.widen match {
21322132
case tp: PolyType => resultConforms(altSym, tp.resultType, resultType)
21332133
case tp: MethodType =>
2134-
constrainResult(altSym, wildApprox(tp.resultType), resultType)
2134+
val wildRes = wildApprox(tp.resultType)
2135+
2136+
class ResultApprox extends AvoidWildcardsMap:
2137+
// Avoid false negatives by approximating to a lower bound
2138+
variance = -1
2139+
2140+
val approx = ResultApprox()(wildRes)
2141+
constrainResult(altSym, approx, resultType)
21352142
case _ => true
21362143
}
21372144
case _ => true

Diff for: 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)