Skip to content

Tune usage of resultConforms to prefer the chosen alternative #22730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2125,19 +2125,19 @@ trait Applications extends Compatibility {
* Using an approximated result type is necessary to avoid false negatives
* due to incomplete type inference such as in tests/pos/i21410.scala and tests/pos/i21410b.scala.
*/
def resultConforms(altSym: Symbol, altType: Type, resultType: Type)(using Context): Boolean =
def resultConforms(altSym: Symbol, altType: Type, resultType: Type, avoidFalseNegatives: Boolean)(using Context): Boolean =
resultType.revealIgnored match {
case resultType: ValueType =>
altType.widen match {
case tp: PolyType => resultConforms(altSym, tp.resultType, resultType)
case tp: PolyType => resultConforms(altSym, tp.resultType, resultType, avoidFalseNegatives)
case tp: MethodType =>
val wildRes = wildApprox(tp.resultType)

class ResultApprox extends AvoidWildcardsMap:
// Avoid false negatives by approximating to a lower bound
class UnderApprox extends AvoidWildcardsMap:
// Prefer false positives to false negatives by approximating to a lower bound
variance = -1

val approx = ResultApprox()(wildRes)
val approx = if avoidFalseNegatives then UnderApprox()(wildRes) else wildRes
constrainResult(altSym, approx, resultType)
case _ => true
}
Expand All @@ -2157,9 +2157,9 @@ trait Applications extends Compatibility {
* do they prune much, on average.
*/
def adaptByResult(chosen: TermRef, alts: List[TermRef]) = pt match {
case pt: FunProto if !explore(resultConforms(chosen.symbol, chosen, pt.resultType)) =>
case pt: FunProto if !explore(resultConforms(chosen.symbol, chosen, pt.resultType, avoidFalseNegatives = true)) =>
val conformingAlts = alts.filterConserve(alt =>
(alt ne chosen) && explore(resultConforms(alt.symbol, alt, pt.resultType)))
(alt ne chosen) && explore(resultConforms(alt.symbol, alt, pt.resultType, avoidFalseNegatives = false)))
conformingAlts match {
case Nil => chosen
case alt2 :: Nil => alt2
Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i22713.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait Invariant[T]

trait Foo
trait Bar[T]

trait Worker:
def schedule(command: Foo): Invariant[?]
def schedule[V](callable: Bar[V]): Invariant[V]

object Test:
def test(worker: Worker, foo: Foo): Option[Unit] =
Some(worker.schedule(foo))
// error: Found: Foo
// Required: Bar[V]
Loading