From 12c90507f772a6d4554a928747081b40c238187e Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Tue, 8 Jun 2021 21:41:14 +0200 Subject: [PATCH] Preserve tvar instantiation in implicit error messages --- compiler/src/dotty/tools/dotc/typer/Implicits.scala | 12 ++++++++++-- tests/neg/implicitSearch.check | 2 +- tests/neg/missing-implicit3.check | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index ae3b04d37657..ed8b347679b5 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -1120,12 +1120,20 @@ trait Implicits: SearchFailure(new DivergingImplicit(cand.ref, wideProto, argument), span) else { val history = ctx.searchHistory.nest(cand, pt) - val result = - typedImplicit(cand, pt, argument, span)(using nestedContext().setNewTyperState().setFreshGADTBounds.setSearchHistory(history)) + val typingCtx = + nestedContext().setNewTyperState().setFreshGADTBounds.setSearchHistory(history) + val result = typedImplicit(cand, pt, argument, span)(using typingCtx) result match { case res: SearchSuccess => ctx.searchHistory.defineBynameImplicit(wideProto, res) case _ => + // Since the search failed, the local typerstate will be discarded + // without being committed, but type variables local to that state + // might still appear in an error message, so we run `gc()` here to + // make sure we don't forget their instantiation. This leads to more + // precise error messages in tests/neg/missing-implicit3.check and + // tests/neg/implicitSearch.check + typingCtx.typerState.gc() result } } diff --git a/tests/neg/implicitSearch.check b/tests/neg/implicitSearch.check index efc3ac002739..9ff67e2c2638 100644 --- a/tests/neg/implicitSearch.check +++ b/tests/neg/implicitSearch.check @@ -4,7 +4,7 @@ | no implicit argument of type Test.Ord[List[List[T]]] was found for parameter o of method sort in object Test. | I found: | - | Test.listOrd[T](Test.listOrd[T](/* missing */summon[Test.Ord[T]])) + | Test.listOrd[List[T]](Test.listOrd[T](/* missing */summon[Test.Ord[T]])) | | But no implicit values were found that match type Test.Ord[T]. -- Error: tests/neg/implicitSearch.scala:15:38 ------------------------------------------------------------------------- diff --git a/tests/neg/missing-implicit3.check b/tests/neg/missing-implicit3.check index dec05cecdad6..d58216b9173e 100644 --- a/tests/neg/missing-implicit3.check +++ b/tests/neg/missing-implicit3.check @@ -4,6 +4,6 @@ |no implicit argument of type ord.Ord[ord.Foo] was found for an implicit parameter of method sort in package ord. |I found: | - | ord.Ord.ordered[A](/* missing */summon[ord.Foo => Comparable[? >: ord.Foo]]) + | ord.Ord.ordered[ord.Foo](/* missing */summon[ord.Foo => Comparable[? >: ord.Foo]]) | |But no implicit values were found that match type ord.Foo => Comparable[? >: ord.Foo].