Skip to content

Commit 631f7c1

Browse files
committed
Refine result type checking
New test case: array-overload.scala. Failed before, succeeds now.
1 parent aef3d57 commit 631f7c1

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,9 @@ trait Applications extends Compatibility { self: Typer =>
915915
}}
916916

917917
def narrowMostSpecific(alts: List[TermRef])(implicit ctx: Context): List[TermRef] = track("narrowMostSpecific") {
918-
(alts: @unchecked) match {
918+
alts match {
919+
case Nil => alts
920+
case _ :: Nil => alts
919921
case alt :: alts1 =>
920922
def winner(bestSoFar: TermRef, alts: List[TermRef]): TermRef = alts match {
921923
case alt :: alts1 =>
@@ -993,9 +995,7 @@ trait Applications extends Compatibility { self: Typer =>
993995
* probability of pruning the search. result type comparisons are neither cheap nor
994996
* do they prune much, on average.
995997
*/
996-
def adaptByResult(alts: List[TermRef], chosen: TermRef) =
997-
if (ctx.isAfterTyper) chosen
998-
else {
998+
def adaptByResult(alts: List[TermRef], chosen: TermRef) = {
999999
def nestedCtx = ctx.fresh.setExploreTyperState
10001000
pt match {
10011001
case pt: FunProto if !resultConforms(chosen, pt.resultType)(nestedCtx) =>
@@ -1074,14 +1074,19 @@ trait Applications extends Compatibility { self: Typer =>
10741074
case pt =>
10751075
alts filter (normalizedCompatible(_, pt))
10761076
}
1077-
if (isDetermined(candidates)) candidates
1078-
else narrowMostSpecific(candidates) match {
1077+
narrowMostSpecific(candidates) match {
10791078
case Nil => Nil
1080-
case alt :: Nil => adaptByResult(candidates, alt) :: Nil
1079+
case alt :: Nil =>
1080+
adaptByResult(alts, alt) :: Nil
1081+
// why `alts` and not `candidates`? pos/array-overload.scala gives a test case.
1082+
// Here, only the Int-apply is a candidate, but it is not compatible with the result
1083+
// type. Picking the Byte-apply as the only result-compatible solution then forces
1084+
// the arguments (which are constants) to be adapted to Byte. If we had picked
1085+
// `candidates` instead, no solution would have been found.
10811086
case alts =>
10821087
// overload.println(i"ambiguous $alts%, %")
10831088
val deepPt = pt.deepenProto
1084-
if (deepPt ne pt) resolveOverloaded(candidates, deepPt, targs)
1089+
if (deepPt ne pt) resolveOverloaded(alts, deepPt, targs)
10851090
else alts
10861091
}
10871092
}

0 commit comments

Comments
 (0)