Skip to content

Commit 6dfe0fb

Browse files
committed
Take type bounds into account for overloading resolution
When resolving overloaded polymorphic variants with explicit type arguments, discard those variants where the given argument does not fit the type parameter bounds. Fixes #11015
1 parent 5672999 commit 6dfe0fb

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,13 @@ trait Applications extends Compatibility {
18841884
case pt @ PolyProto(targs1, pt1) =>
18851885
val alts1 = alts.filter(pt.canInstantiate)
18861886
if isDetermined(alts1) then alts1
1887-
else resolveMapped(alts1, _.widen.appliedTo(targs1.tpes), pt1)
1887+
else
1888+
def withinBounds(alt: TermRef) = alt.widen match
1889+
case tp: PolyType =>
1890+
TypeOps.boundsViolations(targs1, tp.paramInfos, _.substParams(tp, _), NoType).isEmpty
1891+
val alts2 = alts1.filter(withinBounds)
1892+
if isDetermined(alts2) then alts2
1893+
else resolveMapped(alts1, _.widen.appliedTo(targs1.tpes), pt1)
18881894

18891895
case defn.FunctionOf(args, resultType, _, _) =>
18901896
narrowByTypes(alts, args, resultType)

tests/neg/tcpoly_overloaded.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ trait Test {
2121
def flatMap[S]
2222
(f: T => List[S], foo: Int): List[S] = sys.error("foo")
2323
}
24-
val l: MList[String] = moo.flatMap[String, List, Any, MList]((x: Int) => new MList("String"))
24+
val l: MList[String] = moo.flatMap[String, List, Any, MList]((x: Int) => new MList("String")) // error: no alternative matches
2525
}

tests/pos/i11015.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import annotation.targetName
2+
object Foo:
3+
def apply[A <: Int]: Int = 0
4+
@targetName("applyS") def apply[B <: String]: String = "0"
5+
6+
def test = Foo[Int]

0 commit comments

Comments
 (0)