Skip to content

Commit 95b8702

Browse files
smartertgodzik
authored andcommitted
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. [Cherry-picked 32ac2e6]
1 parent 73aaf2a commit 95b8702

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,16 +1953,23 @@ trait Applications extends Compatibility {
19531953
* section conforms to the expected type `resultType`? If `resultType`
19541954
* is a `IgnoredProto`, pick the underlying type instead.
19551955
*
1956-
* Using approximated result types is necessary to avoid false negatives
1957-
* due to incomplete type inference such as in tests/pos/i21410.scala.
1956+
* Using an approximated result type is necessary to avoid false negatives
1957+
* due to incomplete type inference such as in tests/pos/i21410.scala and tests/pos/i21410b.scala.
19581958
*/
19591959
def resultConforms(altSym: Symbol, altType: Type, resultType: Type)(using Context): Boolean =
19601960
resultType.revealIgnored match {
19611961
case resultType: ValueType =>
19621962
altType.widen match {
19631963
case tp: PolyType => resultConforms(altSym, tp.resultType, resultType)
19641964
case tp: MethodType =>
1965-
constrainResult(altSym, wildApprox(tp.resultType), resultType)
1965+
val wildRes = wildApprox(tp.resultType)
1966+
1967+
class ResultApprox extends AvoidWildcardsMap:
1968+
// Avoid false negatives by approximating to a lower bound
1969+
variance = -1
1970+
1971+
val approx = ResultApprox()(wildRes)
1972+
constrainResult(altSym, approx, resultType)
19661973
case _ => true
19671974
}
19681975
case _ => true

tests/pos/i21410b.scala

Lines changed: 10 additions & 0 deletions
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)