Skip to content

Commit a5980f7

Browse files
committed
gadt: Make externalize handle all types
... by keeping an memory-optimised path & extracting a ExternalizeMap. Also a small fixup in ContainsNoInternalTypesAccumulator using itself.
1 parent 62df571 commit a5980f7

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

compiler/src/dotty/tools/dotc/core/GadtConstraint.scala

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,7 @@ final class ProperGadtConstraint private(
239239
override protected def isSame(tp1: Type, tp2: Type)(using Context): Boolean = TypeComparer.isSameType(tp1, tp2)
240240

241241
override def nonParamBounds(param: TypeParamRef)(using Context): TypeBounds =
242-
val externalizeMap = new TypeMap {
243-
def apply(tp: Type): Type = tp match {
244-
case tpr: TypeParamRef => externalize(tpr)
245-
case tp => mapOver(tp)
246-
}
247-
}
248-
externalizeMap(constraint.nonParamBounds(param)).bounds
242+
externalize(constraint.nonParamBounds(param)).bounds
249243

250244
override def fullLowerBound(param: TypeParamRef)(using Context): Type =
251245
constraint.minLower(param).foldLeft(nonParamBounds(param).lo) {
@@ -262,27 +256,27 @@ final class ProperGadtConstraint private(
262256

263257
// ---- Private ----------------------------------------------------------
264258

265-
private def externalize(param: TypeParamRef)(using Context): Type =
266-
reverseMapping(param) match {
259+
private def externalize(tp: Type, theMap: TypeMap | Null = null)(using Context): Type = tp match
260+
case param: TypeParamRef => reverseMapping(param) match
267261
case sym: Symbol => sym.typeRef
268-
case null => param
269-
}
262+
case null => param
263+
case tp => (if theMap == null then ExternalizeMap() else theMap).mapOver(tp)
264+
265+
private class ExternalizeMap(using Context) extends TypeMap:
266+
def apply(tp: Type): Type = externalize(tp, this)(using mapCtx)
270267

271268
private def tvarOrError(sym: Symbol)(using Context): TypeVar =
272269
mapping(sym).ensuring(_ != null, i"not a constrainable symbol: $sym").uncheckedNN
273270

274-
private def containsNoInternalTypes(
275-
tp: Type,
276-
acc: TypeAccumulator[Boolean] | Null = null
277-
)(using Context): Boolean = tp match {
271+
private def containsNoInternalTypes(tp: Type, theAcc: TypeAccumulator[Boolean] | Null = null)(using Context): Boolean = tp match {
278272
case tpr: TypeParamRef => !reverseMapping.contains(tpr)
279273
case tv: TypeVar => !reverseMapping.contains(tv.origin)
280274
case tp =>
281-
(if (acc != null) acc else new ContainsNoInternalTypesAccumulator()).foldOver(true, tp)
275+
(if (theAcc != null) theAcc else new ContainsNoInternalTypesAccumulator()).foldOver(true, tp)
282276
}
283277

284278
private class ContainsNoInternalTypesAccumulator(using Context) extends TypeAccumulator[Boolean] {
285-
override def apply(x: Boolean, tp: Type): Boolean = x && containsNoInternalTypes(tp)
279+
override def apply(x: Boolean, tp: Type): Boolean = x && containsNoInternalTypes(tp, this)
286280
}
287281

288282
// ---- Debug ------------------------------------------------------------

0 commit comments

Comments
 (0)