Skip to content

Commit 77401fa

Browse files
authored
Revert recent changes to opaque type proxy generation (#23059)
2 parents 83ffe00 + ae756ad commit 77401fa

File tree

9 files changed

+65
-98
lines changed

9 files changed

+65
-98
lines changed

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

+3-49
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,9 @@ object Inliner:
165165
else Nil
166166
case _ => Nil
167167
val refinements = openOpaqueAliases(cls.givenSelfType)
168-
169-
// Map references in the refinements from the proxied termRef
170-
// to the recursive type of the refined type
171-
// e.g.: Obj.type{type A = Obj.B; type B = Int} -> Obj.type{type A = <recthis>.B; type B = Int}
172-
def mapRecTermRefReferences(recType: RecType, refinedType: Type) =
173-
new TypeMap {
174-
def apply(tp: Type) = tp match
175-
case RefinedType(a: RefinedType, b, info) => RefinedType(apply(a), b, apply(info))
176-
case RefinedType(a, b, info) => RefinedType(a, b, apply(info))
177-
case TypeRef(prefix, des) => TypeRef(apply(prefix), des)
178-
case termRef: TermRef if termRef == ref => recType.recThis
179-
case _ => mapOver(tp)
180-
}.apply(refinedType)
181-
182168
val refinedType = refinements.foldLeft(ref: Type): (parent, refinement) =>
183169
RefinedType(parent, refinement._1, TypeAlias(refinement._2))
184-
185-
val recType = RecType.closeOver ( recType =>
186-
mapRecTermRefReferences(recType, refinedType)
187-
)
188-
189-
val refiningSym = newSym(InlineBinderName.fresh(), Synthetic, recType, span)
170+
val refiningSym = newSym(InlineBinderName.fresh(), Synthetic, refinedType, span)
190171
refiningSym.termRef
191172

192173
def unapply(refiningRef: TermRef)(using Context): Option[TermRef] =
@@ -405,9 +386,6 @@ class Inliner(val call: tpd.Tree)(using Context):
405386
*/
406387
private val opaqueProxies = new mutable.ListBuffer[(TermRef, TermRef)]
407388

408-
/** TermRefs for which we already started synthesising proxies */
409-
private val visitedTermRefs = new mutable.HashSet[TermRef]
410-
411389
protected def hasOpaqueProxies = opaqueProxies.nonEmpty
412390

413391
/** Map first halves of opaqueProxies pairs to second halves, using =:= as equality */
@@ -435,15 +413,12 @@ class Inliner(val call: tpd.Tree)(using Context):
435413
for cls <- ref.widen.baseClasses do
436414
if cls.containsOpaques
437415
&& (forThisProxy || inlinedMethod.isContainedIn(cls))
438-
&& !visitedTermRefs.contains(ref)
416+
&& mapRef(ref).isEmpty
439417
then
440-
visitedTermRefs += ref
441418
val refiningRef = OpaqueProxy(ref, cls, call.span)
442419
val refiningSym = refiningRef.symbol.asTerm
443420
val refinedType = refiningRef.info
444-
val refiningDef = addProxiesForRecurrentOpaques(
445-
ValDef(refiningSym, tpd.ref(ref).cast(refinedType), inferred = true).withSpan(span)
446-
)
421+
val refiningDef = ValDef(refiningSym, tpd.ref(ref).cast(refinedType), inferred = true).withSpan(span)
447422
inlining.println(i"add opaque alias proxy $refiningDef for $ref in $tp")
448423
bindingsBuf += refiningDef
449424
opaqueProxies += ((ref, refiningSym.termRef))
@@ -463,27 +438,6 @@ class Inliner(val call: tpd.Tree)(using Context):
463438
}
464439
)
465440

466-
/** Transforms proxies that reference other opaque types, like for:
467-
* object Obj1 { opaque type A = Int }
468-
* object Obj2 { opaque type B = A }
469-
* and proxy$1 of type Obj2.type{type B = Obj1.A}
470-
* creates proxy$2 of type Obj1.type{type A = Int}
471-
* and transforms proxy$1 into Obj2.type{type B = proxy$2.A}
472-
*/
473-
private def addProxiesForRecurrentOpaques(binding: ValDef)(using Context): ValDef =
474-
def fixRefinedTypes(ref: Type): Unit =
475-
ref match
476-
case recType: RecType => fixRefinedTypes(recType.underlying)
477-
case RefinedType(parent, name, info) =>
478-
addOpaqueProxies(info.widen, binding.span, true)
479-
fixRefinedTypes(parent)
480-
case _ =>
481-
fixRefinedTypes(binding.symbol.info)
482-
binding.symbol.info = mapOpaques.typeMap(binding.symbol.info)
483-
mapOpaques.transform(binding).asInstanceOf[ValDef]
484-
.showing(i"transformed this binding exposing opaque aliases: $result", inlining)
485-
end addProxiesForRecurrentOpaques
486-
487441
/** If `binding` contains TermRefs that refer to objects with opaque
488442
* type aliases, add proxy definitions that expose these aliases
489443
* and substitute such TermRefs with theproxies. Example from pos/opaque-inline1.scala:

tests/pos/22359a.scala

-15
This file was deleted.

tests/pos/22359b.scala

-17
This file was deleted.

tests/pos/i17243.scala

-17
This file was deleted.

tests/pos/i22974a/Maybe_1.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package pack
2+
import Maybe._
3+
opaque type Maybe[+A] >: (Absent | Present[A]) = Absent | Present[A]
4+
object Maybe:
5+
sealed abstract class Absent
6+
case object Absent extends Absent
7+
object internal:
8+
case class PresentAbsent(val depth: Int)
9+
opaque type Present[+A] = A | internal.PresentAbsent
10+
11+
extension [A](self: Maybe[A]) {
12+
inline def flatten[B]: Maybe[B] = ???
13+
inline def isDefined: Boolean = ???
14+
}

tests/pos/i22974a/macro_1.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted._
2+
3+
inline def passThorugh(inline condition: Boolean): Any =
4+
${ passThorughImpl('{condition}) }
5+
6+
def passThorughImpl(condition: Expr[Boolean])(using Quotes): Expr[Any] = condition

tests/pos/i22974a/main_2.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
def main(): Unit =
3+
import pack.Maybe
4+
val res: Maybe[Maybe[Int]] = ???
5+
passThorugh(res.flatten.isDefined)
6+
}

tests/pos/i22974b.scala

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object outer:
2+
opaque type Queue = Queue.Unsafe
3+
object Queue:
4+
abstract class Unsafe
5+
opaque type Unbounded = Queue
6+
object Unbounded:
7+
inline def initWith()(f: Unbounded => Unit): Unit =
8+
f(Unsafe.init())
9+
10+
opaque type Unsafe <: Queue.Unsafe = Queue
11+
object Unsafe:
12+
def init[A](): Unsafe = ???
13+
14+
object Resource:
15+
def run: Unit =
16+
outer.Queue.Unbounded.initWith() { q =>
17+
???
18+
}

tests/pos/i22974c.scala

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object other:
2+
sealed abstract class Absent
3+
case object Absent extends Absent
4+
case class PresentAbsent(val depth: Int)
5+
opaque type Present[+A] = A | PresentAbsent
6+
opaque type Maybe[+A] >: (Absent | Present[A]) = Absent | Present[A]
7+
8+
extension [A](self: Maybe[A]) {
9+
inline def flatten[B]: Maybe[B] = if self.isEmpty then Absent else ???
10+
def isEmpty: Boolean = self.isInstanceOf[Absent]
11+
}
12+
13+
class Test {
14+
def main(): Unit =
15+
import other.Maybe
16+
val res: Maybe[Maybe[Int]] = ???
17+
res.flatten
18+
}

0 commit comments

Comments
 (0)