From c8c8cf5f1d87fceb1713af6d81ac9a689a90a4ac Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Wed, 29 Jan 2025 15:04:55 +0100 Subject: [PATCH] Fix stack overflow errors when generating opaque type proxies Before the regressive PR, we would check via the generated opaqueProxies list whether one was already generated. In that PR, we tried allowing generating proxies for rhs of currently generated proxy. Since we have to add the generated proxy to opaqueProxies only after that step, this could cause infinite recursion (and adding the proxies earlier could cause another infinite loop). To fix that, we add another collection for termrefs which we already visited this way, but which is not used in the `mapOpaques` function. --- compiler/src/dotty/tools/dotc/inlines/Inliner.scala | 6 +++++- tests/pos/i22468.scala | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i22468.scala diff --git a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala index 982d15f8bcf7..27d95d055f40 100644 --- a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala @@ -384,6 +384,9 @@ class Inliner(val call: tpd.Tree)(using Context): */ private val opaqueProxies = new mutable.ListBuffer[(TermRef, TermRef)] + /** TermRefs for which we already started synthesising proxies */ + private val visitedTermRefs = new mutable.HashSet[TermRef] + protected def hasOpaqueProxies = opaqueProxies.nonEmpty /** Map first halves of opaqueProxies pairs to second halves, using =:= as equality */ @@ -401,8 +404,9 @@ class Inliner(val call: tpd.Tree)(using Context): for cls <- ref.widen.baseClasses do if cls.containsOpaques && (forThisProxy || inlinedMethod.isContainedIn(cls)) - && mapRef(ref).isEmpty + && !visitedTermRefs.contains(ref) then + visitedTermRefs += ref val refiningRef = OpaqueProxy(ref, cls, call.span) val refiningSym = refiningRef.symbol.asTerm val refinedType = refiningRef.info diff --git a/tests/pos/i22468.scala b/tests/pos/i22468.scala new file mode 100644 index 000000000000..265d6d3a89b7 --- /dev/null +++ b/tests/pos/i22468.scala @@ -0,0 +1,10 @@ +import Result.* +opaque type Result[+E, +A] = Success[A] | Error[E] + +object Result: + opaque type Success[+A] = A + sealed abstract class Error[+E] + + extension [E, A](self: Result[E, A]) + inline def transform[B]: B = ??? + def problem: Boolean = transform[Boolean]