Skip to content

Commit 1051563

Browse files
committed
Fix BiTypeMap#inverse
1 parent 4290e61 commit 1051563

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

compiler/src/dotty/tools/dotc/cc/CaptureSet.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ object CaptureSet:
633633
*/
634634
override def computeApprox(origin: CaptureSet)(using Context): CaptureSet =
635635
val supApprox = super.computeApprox(this)
636-
if source eq origin then supApprox.map(bimap.inverseTypeMap)
636+
if source eq origin then supApprox.map(bimap.inverse)
637637
else source.upperApprox(this).map(bimap) ** supApprox
638638

639639
override def toString = s"BiMapped$id($source, elems = $elems)"

compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ object CheckCaptures:
9797

9898
final class SubstParamsBiMap(from: LambdaType, to: List[Type])(using Context)
9999
extends BiTypeMap:
100+
thisMap =>
100101

101102
def apply(tp: Type): Type = tp match
102103
case tp: ParamRef =>
@@ -109,20 +110,22 @@ object CheckCaptures:
109110
case _ =>
110111
mapOver(tp)
111112

112-
def inverse(tp: Type): Type = tp match
113-
case tp: NamedType =>
114-
var idx = 0
115-
var to1 = to
116-
while idx < to.length && (tp ne to(idx)) do
117-
idx += 1
118-
to1 = to1.tail
119-
if idx < to.length then from.paramRefs(idx)
120-
else if tp.prefix `eq` NoPrefix then tp
121-
else tp.derivedSelect(apply(tp.prefix))
122-
case _: ThisType =>
123-
tp
124-
case _ =>
125-
mapOver(tp)
113+
lazy val inverse = new BiTypeMap:
114+
def apply(tp: Type): Type = tp match
115+
case tp: NamedType =>
116+
var idx = 0
117+
var to1 = to
118+
while idx < to.length && (tp ne to(idx)) do
119+
idx += 1
120+
to1 = to1.tail
121+
if idx < to.length then from.paramRefs(idx)
122+
else if tp.prefix `eq` NoPrefix then tp
123+
else tp.derivedSelect(apply(tp.prefix))
124+
case _: ThisType =>
125+
tp
126+
case _ =>
127+
mapOver(tp)
128+
def inverse = thisMap
126129
end SubstParamsBiMap
127130

128131
/** Check that a @retains annotation only mentions references that can be tracked.

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ extends tpd.TreeTraverser:
238238
*/
239239
private class SubstParams(from: List[List[Symbol]], to: List[LambdaType])(using Context)
240240
extends DeepTypeMap, BiTypeMap:
241+
thisMap =>
241242

242243
def apply(t: Type): Type = t match
243244
case t: NamedType =>
@@ -253,15 +254,17 @@ extends tpd.TreeTraverser:
253254
case _ =>
254255
mapOver(t)
255256

256-
def inverse(t: Type): Type = t match
257-
case t: ParamRef =>
258-
def recur(from: List[LambdaType], to: List[List[Symbol]]): Type =
259-
if from.isEmpty then t
260-
else if t.binder eq from.head then to.head(t.paramNum).namedType
261-
else recur(from.tail, to.tail)
262-
recur(to, from)
263-
case _ =>
264-
mapOver(t)
257+
lazy val inverse = new BiTypeMap:
258+
def apply(t: Type): Type = t match
259+
case t: ParamRef =>
260+
def recur(from: List[LambdaType], to: List[List[Symbol]]): Type =
261+
if from.isEmpty then t
262+
else if t.binder eq from.head then to.head(t.paramNum).namedType
263+
else recur(from.tail, to.tail)
264+
recur(to, from)
265+
case _ =>
266+
mapOver(t)
267+
def inverse = thisMap
265268
end SubstParams
266269

267270
/** Update info of `sym` for CheckCaptures phase only */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ object Substituters:
165165

166166
final class SubstBindingMap(from: BindingType, to: BindingType)(using Context) extends DeepTypeMap, BiTypeMap {
167167
def apply(tp: Type): Type = subst(tp, from, to, this)(using mapCtx)
168-
def inverse(tp: Type): Type = tp.subst(to, from)
168+
def inverse = SubstBindingMap(to, from)
169169
}
170170

171171
final class Subst1Map(from: Symbol, to: Type)(using Context) extends DeepTypeMap {
@@ -182,7 +182,7 @@ object Substituters:
182182

183183
final class SubstSymMap(from: List[Symbol], to: List[Symbol])(using Context) extends DeepTypeMap, BiTypeMap {
184184
def apply(tp: Type): Type = substSym(tp, from, to, this)(using mapCtx)
185-
def inverse(tp: Type) = tp.substSym(to, from) // implicitly requires that `to` contains no duplicates.
185+
def inverse = SubstSymMap(to, from) // implicitly requires that `to` contains no duplicates.
186186
}
187187

188188
final class SubstThisMap(from: ClassSymbol, to: Type)(using Context) extends DeepTypeMap {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5716,15 +5716,8 @@ object Types {
57165716
trait BiTypeMap extends TypeMap:
57175717
thisMap =>
57185718

5719-
/** The inverse of the type map as a function */
5720-
def inverse(tp: Type): Type
5721-
5722-
/** The inverse of the type map as a BiTypeMap map, which
5723-
* has the original type map as its own inverse.
5724-
*/
5725-
def inverseTypeMap(using Context) = new BiTypeMap:
5726-
def apply(tp: Type) = thisMap.inverse(tp)
5727-
def inverse(tp: Type) = thisMap.apply(tp)
5719+
/** The inverse of the type map */
5720+
def inverse: BiTypeMap
57285721

57295722
/** A restriction of this map to a function on tracked CaptureRefs */
57305723
def forward(ref: CaptureRef): CaptureRef = this(ref) match

0 commit comments

Comments
 (0)