From 820c0caa43314c78b220738a5db6b6f6889a5718 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Aug 2024 22:35:19 +0100 Subject: [PATCH] Remove tvars introduced while testing normalizedCompatible --- .../src/dotty/tools/dotc/typer/ProtoTypes.scala | 4 ++++ compiler/src/dotty/tools/dotc/typer/Typer.scala | 1 + tests/pos/interleaving-overload.cleanup.scala | 10 ++++++++++ tests/pos/zipped.min.scala | 15 +++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 tests/pos/interleaving-overload.cleanup.scala create mode 100644 tests/pos/zipped.min.scala diff --git a/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala b/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala index a69a63d1ceef..b9ef7e5627a6 100644 --- a/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala +++ b/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala @@ -71,6 +71,10 @@ object ProtoTypes { |constraint was: ${ctx.typerState.constraint} |constraint now: ${newctx.typerState.constraint}""") if result && (ctx.typerState.constraint ne newctx.typerState.constraint) then + val tvars = (newctx.typerState.ownedVars -- ctx.typerState.ownedVars).toList + inContext(newctx): + Inferencing.instantiateSelected(tp, tvars) + for tvar <- tvars do if !tvar.isInstantiated then tvar.instantiate(fromBelow = false) newctx.typerState.commit() result case _ => testCompat diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 1125e09539b6..b56cc5bcb7a4 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3557,6 +3557,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer protected def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): tree.type = if !tree.denot.isOverloaded then // for overloaded trees: resolve overloading before simplifying if !tree.tpe.widen.isInstanceOf[MethodOrPoly] // wait with simplifying until method is fully applied + && !tree.tpe.match { case defn.PolyFunctionOf(_) => true case _ => false } // ... or polyfunction is fully applied || tree.isDef // ... unless tree is a definition then interpolateTypeVars(tree, pt, locked) diff --git a/tests/pos/interleaving-overload.cleanup.scala b/tests/pos/interleaving-overload.cleanup.scala new file mode 100644 index 000000000000..e7f7c2cfe3bd --- /dev/null +++ b/tests/pos/interleaving-overload.cleanup.scala @@ -0,0 +1,10 @@ +// Justifies the need to add defn.PolyFunctionOf in simplify +// Without, the TypeVar for the U in fn's lambda +// replaces the TypeParamRef U, in simplify. +class B[U] +class Test(): + def fn[T]: [U] => Int => B[U] = [U] => (x: Int) => new B[U]() + def test(): Unit = + fn(1) + fn(2) + () diff --git a/tests/pos/zipped.min.scala b/tests/pos/zipped.min.scala new file mode 100644 index 000000000000..5d15b3fae240 --- /dev/null +++ b/tests/pos/zipped.min.scala @@ -0,0 +1,15 @@ +// Justifies the need for TypeApply in tryInsertImplicitOnQualifier +// after failing ys.map[?B, C] using Zipped2's map +// we want to try ys.map[?B] using Coll's map, after toColl +final class Coll[+A]: + def map[B](f: A => B): Coll[B] = new Coll[B] + def lazyZip[B](that: Coll[B]): Zipped2[A, B] = new Zipped2[A, B](this, that) +final class Zipped2[+X, +Y](xs: Coll[X], ys: Coll[Y]): + def map[B, C](f: (X, Y) => B): Coll[C] = new Coll[C] +object Zipped2: + import scala.language.implicitConversions + implicit def toColl[X, Y](zipped2: Zipped2[X, Y]): Coll[(X, Y)] = new Coll[(X, Y)] +class Test: + def test(xs: Coll[Int]): Unit = + val ys = xs.lazyZip(xs) + ys.map((x: (Int, Int)) => x._1 + x._2)