Skip to content

Commit d897678

Browse files
committed
Base deskolemize on ApproximatingTypeMap
1 parent 78545bb commit d897678

File tree

1 file changed

+9
-88
lines changed

1 file changed

+9
-88
lines changed

src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -122,100 +122,21 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
122122
def currentVariance = variance
123123
}
124124

125-
/** Approximate a type `tp` with a type that does not contain skolem types.
126-
*/
127-
final def deskolemize(tp: Type): Type = deskolemize(tp, 1, Set())
128-
129-
private def deskolemize(tp: Type, variance: Int, seen: Set[SkolemType]): Type = {
130-
def approx(lo: Type = defn.NothingType, hi: Type = defn.AnyType, newSeen: Set[SkolemType] = seen) =
131-
if (variance == 0) NoType
132-
else deskolemize(if (variance < 0) lo else hi, variance, newSeen)
133-
tp match {
125+
/** Approximate a type `tp` with a type that does not contain skolem types. */
126+
object deskolemize extends ApproximatingTypeMap {
127+
private var seen: Set[SkolemType] = Set()
128+
def apply(tp: Type) = tp match {
134129
case tp: SkolemType =>
135130
if (seen contains tp) NoType
136-
else approx(hi = tp.info, newSeen = seen + tp)
137-
case tp: NamedType =>
138-
val sym = tp.symbol
139-
if (sym.isStatic) tp
140131
else {
141-
val pre1 = deskolemize(tp.prefix, variance, seen)
142-
if (pre1 eq tp.prefix) tp
143-
else {
144-
val d = tp.prefix.member(tp.name)
145-
d.info match {
146-
case TypeAlias(alias) => deskolemize(alias, variance, seen)
147-
case _ =>
148-
if (pre1.exists && !pre1.isRef(defn.NothingClass)) tp.derivedSelect(pre1)
149-
else {
150-
ctx.log(s"deskolem: $tp: ${tp.info}")
151-
tp.info match {
152-
case TypeBounds(lo, hi) => approx(lo, hi)
153-
case info => NoType
154-
}
155-
}
156-
}
157-
}
132+
val saved = seen
133+
seen += tp
134+
try approx(hi = tp.info)
135+
finally seen = saved
158136
}
159-
case _: ThisType | _: BoundType | _: SuperType | NoType | NoPrefix =>
160-
tp
161-
case tp: RefinedType =>
162-
val parent1 = deskolemize(tp.parent, variance, seen)
163-
if (parent1.exists) {
164-
val refinedInfo1 = deskolemize(tp.refinedInfo, variance, seen)
165-
if (refinedInfo1.exists)
166-
tp.derivedRefinedType(parent1, tp.refinedName, refinedInfo1)
167-
else
168-
approx(hi = parent1)
169-
}
170-
else approx()
171-
case tp: TypeAlias =>
172-
val alias1 = deskolemize(tp.alias, variance * tp.variance, seen)
173-
if (alias1.exists) tp.derivedTypeAlias(alias1)
174-
else approx(hi = TypeBounds.empty)
175-
case tp: TypeBounds =>
176-
val lo1 = deskolemize(tp.lo, -variance, seen)
177-
val hi1 = deskolemize(tp.hi, variance, seen)
178-
if (lo1.exists && hi1.exists) tp.derivedTypeBounds(lo1, hi1)
179-
else approx(hi =
180-
if (lo1.exists) TypeBounds.lower(lo1)
181-
else if (hi1.exists) TypeBounds.upper(hi1)
182-
else TypeBounds.empty)
183-
case tp: ClassInfo =>
184-
val pre1 = deskolemize(tp.prefix, variance, seen)
185-
if (pre1.exists) tp.derivedClassInfo(pre1)
186-
else NoType
187-
case tp: AndOrType =>
188-
val tp1d = deskolemize(tp.tp1, variance, seen)
189-
val tp2d = deskolemize(tp.tp2, variance, seen)
190-
if (tp1d.exists && tp2d.exists)
191-
tp.derivedAndOrType(tp1d, tp2d)
192-
else if (tp.isAnd)
193-
approx(hi = tp1d & tp2d) // if one of tp1d, tp2d exists, it is the result of tp1d & tp2d
194-
else
195-
approx(lo = tp1d & tp2d)
196-
case tp: WildcardType =>
197-
val bounds1 = deskolemize(tp.optBounds, variance, seen)
198-
if (bounds1.exists) tp.derivedWildcardType(bounds1)
199-
else WildcardType
200137
case _ =>
201138
if (tp.isInstanceOf[MethodicType]) assert(variance != 0, tp)
202-
deskolemizeMap.mapOver(tp, variance, seen)
203-
}
204-
}
205-
206-
object deskolemizeMap extends TypeMap {
207-
private var seen: Set[SkolemType] = _
208-
def apply(tp: Type) = deskolemize(tp, variance, seen)
209-
def mapOver(tp: Type, variance: Int, seen: Set[SkolemType]) = {
210-
val savedVariance = this.variance
211-
val savedSeen = this.seen
212-
this.variance = variance
213-
this.seen = seen
214-
try super.mapOver(tp)
215-
finally {
216-
this.variance = savedVariance
217-
this.seen = savedSeen
218-
}
139+
mapOver(tp)
219140
}
220141
}
221142

0 commit comments

Comments
 (0)