Skip to content

Commit 9323e4e

Browse files
authored
Merge pull request #12755 from dotty-staging/better-implicit-error
Preserve tvar instantiation in implicit error messages
2 parents 55762c6 + 12c9050 commit 9323e4e

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,12 +1120,20 @@ trait Implicits:
11201120
SearchFailure(new DivergingImplicit(cand.ref, wideProto, argument), span)
11211121
else {
11221122
val history = ctx.searchHistory.nest(cand, pt)
1123-
val result =
1124-
typedImplicit(cand, pt, argument, span)(using nestedContext().setNewTyperState().setFreshGADTBounds.setSearchHistory(history))
1123+
val typingCtx =
1124+
nestedContext().setNewTyperState().setFreshGADTBounds.setSearchHistory(history)
1125+
val result = typedImplicit(cand, pt, argument, span)(using typingCtx)
11251126
result match {
11261127
case res: SearchSuccess =>
11271128
ctx.searchHistory.defineBynameImplicit(wideProto, res)
11281129
case _ =>
1130+
// Since the search failed, the local typerstate will be discarded
1131+
// without being committed, but type variables local to that state
1132+
// might still appear in an error message, so we run `gc()` here to
1133+
// make sure we don't forget their instantiation. This leads to more
1134+
// precise error messages in tests/neg/missing-implicit3.check and
1135+
// tests/neg/implicitSearch.check
1136+
typingCtx.typerState.gc()
11291137
result
11301138
}
11311139
}

tests/neg/implicitSearch.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
| no implicit argument of type Test.Ord[List[List[T]]] was found for parameter o of method sort in object Test.
55
| I found:
66
|
7-
| Test.listOrd[T](Test.listOrd[T](/* missing */summon[Test.Ord[T]]))
7+
| Test.listOrd[List[T]](Test.listOrd[T](/* missing */summon[Test.Ord[T]]))
88
|
99
| But no implicit values were found that match type Test.Ord[T].
1010
-- Error: tests/neg/implicitSearch.scala:15:38 -------------------------------------------------------------------------

tests/neg/missing-implicit3.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
|no implicit argument of type ord.Ord[ord.Foo] was found for an implicit parameter of method sort in package ord.
55
|I found:
66
|
7-
| ord.Ord.ordered[A](/* missing */summon[ord.Foo => Comparable[? >: ord.Foo]])
7+
| ord.Ord.ordered[ord.Foo](/* missing */summon[ord.Foo => Comparable[? >: ord.Foo]])
88
|
99
|But no implicit values were found that match type ord.Foo => Comparable[? >: ord.Foo].

0 commit comments

Comments
 (0)