Skip to content

Commit

Permalink
More careful type variable instance improvements (scala#19659)
Browse files Browse the repository at this point in the history
The previous code tried to recursively apply the current instance of
FullyDefinedAccumulator to the prospective instance type. This can have
unforeseen side-effects, as i19637 shows. We now are more conservative:
We check that the prospective instance type is already fully defined
without the possibility to instantiate more type variables. This still
passes the test cases that type variable improvement solves and avoids
the problem with scala#19637.

Fixes scala#19637
  • Loading branch information
odersky authored Feb 9, 2024
2 parents 5a5ea06 + 4700e3f commit 067ec20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ object Inferencing {
private def instantiate(tvar: TypeVar, fromBelow: Boolean): Boolean =
if fromBelow && force.canImprove(tvar) then
val inst = tvar.typeToInstantiateWith(fromBelow = true)
if apply(true, inst) then
if isFullyDefined(inst, ForceDegree.none) then
// need to recursively check before improving, since improving adds type vars
// which should not be instantiated at this point
val better = improve(tvar)(inst)
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i19637.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import java.util.stream.*
import java.util.function.*

val map: java.util.Map[String, String] = Stream.of("1", "2", "3").collect(Collectors.toMap(
(s: String) => s,
Function.identity(),
{
case ("1", "1") => "1"
case (_, l) => l
}
))

0 comments on commit 067ec20

Please sign in to comment.