Skip to content

Commit 66a0e28

Browse files
authored
Merge pull request #3287 from dotty-staging/fix-#3250
Fix #3250: Make HKLambda a cached type
2 parents badc855 + 8d0109a commit 66a0e28

14 files changed

+105
-86
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dotc
33
package core
44

55
import Types._, Contexts._, Symbols._
6-
import util.SimpleMap
76
import collection.mutable
87
import printing.{Printer, Showable}
98
import printing.Texts._

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Comments._
1717
import util.Positions._
1818
import ast.Trees._
1919
import ast.untpd
20-
import util.{FreshNameCreator, SimpleMap, SourceFile, NoSource}
20+
import util.{FreshNameCreator, SimpleIdentityMap, SourceFile, NoSource}
2121
import typer.{Implicits, ImplicitRunInfo, ImportInfo, Inliner, NamerContextOps, SearchHistory, TypeAssigner, Typer}
2222
import Implicits.ContextualImplicits
2323
import config.Settings._
@@ -694,14 +694,14 @@ object Contexts {
694694
implicit val ctx: Context = initctx
695695
}
696696

697-
class GADTMap(initBounds: SimpleMap[Symbol, TypeBounds]) extends util.DotClass {
697+
class GADTMap(initBounds: SimpleIdentityMap[Symbol, TypeBounds]) extends util.DotClass {
698698
private var myBounds = initBounds
699699
def setBounds(sym: Symbol, b: TypeBounds): Unit =
700700
myBounds = myBounds.updated(sym, b)
701701
def bounds = myBounds
702702
}
703703

704-
@sharable object EmptyGADTMap extends GADTMap(SimpleMap.Empty) {
704+
@sharable object EmptyGADTMap extends GADTMap(SimpleIdentityMap.Empty) {
705705
override def setBounds(sym: Symbol, b: TypeBounds) = unsupported("EmptyGADTMap.setBounds")
706706
}
707707
}

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ import StdNames.str
1212
import Designators._
1313
import util.Chars.isIdentifierStart
1414
import collection.IndexedSeqOptimized
15-
import collection.generic.CanBuildFrom
16-
import collection.mutable.{ Builder, StringBuilder, AnyRefMap }
17-
import collection.immutable.WrappedString
18-
import collection.generic.CanBuildFrom
19-
import util.{DotClass, SimpleMap}
15+
import collection.immutable
16+
import util.{DotClass}
2017
import config.Config
2118
import java.util.HashMap
2219

@@ -193,24 +190,24 @@ object Names {
193190
def underlying: TermName = unsupported("underlying")
194191

195192
@sharable // because of synchronized block in `and`
196-
private var derivedNames: AnyRef /* SimpleMap | j.u.HashMap */ =
197-
SimpleMap.Empty[NameInfo]
193+
private var derivedNames: AnyRef /* immutable.Map[NameInfo, DerivedName] | j.u.HashMap */ =
194+
immutable.Map.empty[NameInfo, DerivedName]
198195

199196
private def getDerived(info: NameInfo): DerivedName /* | Null */= derivedNames match {
200-
case derivedNames: SimpleMap[NameInfo, DerivedName] @unchecked =>
201-
derivedNames(info)
197+
case derivedNames: immutable.AbstractMap[NameInfo, DerivedName] @unchecked =>
198+
if (derivedNames.contains(info)) derivedNames(info) else null
202199
case derivedNames: HashMap[NameInfo, DerivedName] @unchecked =>
203200
derivedNames.get(info)
204201
}
205202

206203
private def putDerived(info: NameInfo, name: DerivedName): name.type = {
207204
derivedNames match {
208-
case derivedNames: SimpleMap[NameInfo, DerivedName] @unchecked =>
205+
case derivedNames: immutable.Map[NameInfo, DerivedName] @unchecked =>
209206
if (derivedNames.size < 4)
210207
this.derivedNames = derivedNames.updated(info, name)
211208
else {
212209
val newMap = new HashMap[NameInfo, DerivedName]
213-
derivedNames.foreachBinding(newMap.put(_, _))
210+
derivedNames.foreach { case (k, v) => newMap.put(k, v) }
214211
newMap.put(info, name)
215212
this.derivedNames = newMap
216213
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package core
44

55
import Types._, Contexts._, Symbols._, Decorators._
6-
import util.SimpleMap
6+
import util.SimpleIdentityMap
77
import collection.mutable
88
import printing.{Printer, Showable}
99
import printing.Texts._
@@ -14,7 +14,7 @@ import annotation.tailrec
1414

1515
object OrderingConstraint {
1616

17-
type ArrayValuedMap[T] = SimpleMap[TypeLambda, Array[T]]
17+
type ArrayValuedMap[T] = SimpleIdentityMap[TypeLambda, Array[T]]
1818

1919
/** The type of `OrderingConstraint#boundsMap` */
2020
type ParamBounds = ArrayValuedMap[Type]

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Decorators.SymbolIteratorDecorator
1313
import ast._
1414
import annotation.tailrec
1515
import CheckRealizable._
16-
import util.SimpleMap
16+
import util.SimpleIdentityMap
1717
import util.Stats
1818
import java.util.WeakHashMap
1919
import config.Config
@@ -1254,7 +1254,7 @@ object SymDenotations {
12541254
// ----- caches -------------------------------------------------------
12551255

12561256
private[this] var myTypeParams: List[TypeSymbol] = null
1257-
private[this] var fullNameCache: SimpleMap[QualifiedNameKind, Name] = SimpleMap.Empty
1257+
private[this] var fullNameCache: SimpleIdentityMap[QualifiedNameKind, Name] = SimpleIdentityMap.Empty
12581258

12591259
private[this] var myMemberCache: LRUCache[Name, PreDenotation] = null
12601260
private[this] var myMemberCachePeriod: Period = Nowhere
@@ -2041,7 +2041,7 @@ object SymDenotations {
20412041
}
20422042

20432043
private class MemberNamesImpl(createdAt: Period) extends InheritedCacheImpl(createdAt) with MemberNames {
2044-
private[this] var cache: SimpleMap[NameFilter, Set[Name]] = SimpleMap.Empty
2044+
private[this] var cache: SimpleIdentityMap[NameFilter, Set[Name]] = SimpleIdentityMap.Empty
20452045

20462046
final def isValid(implicit ctx: Context): Boolean =
20472047
cache != null && isValidAt(ctx.phase)
@@ -2054,7 +2054,7 @@ object SymDenotations {
20542054
*/
20552055
def invalidate(): Unit =
20562056
if (cache != null)
2057-
if (locked) cache = SimpleMap.Empty
2057+
if (locked) cache = SimpleIdentityMap.Empty
20582058
else {
20592059
cache = null
20602060
invalidateDependents()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Types._, Contexts._, Symbols._, Flags._, Names._, NameOps._, Denotations.
66
import Decorators._
77
import StdNames.{nme, tpnme}
88
import collection.mutable
9-
import util.{Stats, DotClass, SimpleMap}
9+
import util.{Stats, DotClass}
1010
import config.Config
1111
import config.Printers.{typr, constr, subtyping, noPrinter}
1212
import TypeErasure.{erasedLub, erasedGlb}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import StdNames._
1313
import Annotations._
1414
import annotation.tailrec
1515
import config.Config
16-
import util.{SimpleMap, Property}
16+
import util.Property
1717
import collection.mutable
1818
import ast.tpd._
1919

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package core
55
import Types._
66
import Flags._
77
import Contexts._
8-
import util.{SimpleMap, DotClass}
8+
import util.{SimpleIdentityMap, DotClass}
99
import reporting._
1010
import printing.{Showable, Printer}
1111
import printing.Texts._
@@ -24,7 +24,7 @@ class TyperState(previous: TyperState /* | Null */) extends DotClass with Showab
2424
def setReporter(reporter: Reporter): this.type = { myReporter = reporter; this }
2525

2626
private var myConstraint: Constraint =
27-
if (previous == null) new OrderingConstraint(SimpleMap.Empty, SimpleMap.Empty, SimpleMap.Empty)
27+
if (previous == null) new OrderingConstraint(SimpleIdentityMap.Empty, SimpleIdentityMap.Empty, SimpleIdentityMap.Empty)
2828
else previous.constraint
2929

3030
def constraint = myConstraint

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

+28-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Periods._
1919
import Designators._
2020
import util.Positions.{Position, NoPosition}
2121
import util.Stats._
22-
import util.{DotClass, SimpleMap}
22+
import util.DotClass
2323
import reporting.diagnostic.Message
2424
import reporting.diagnostic.messages.CyclicReferenceInvolving
2525
import ast.tpd._
@@ -2602,7 +2602,32 @@ object Types {
26022602
final override def toString = s"$prefixString($paramNames, $paramInfos, $resType)"
26032603
}
26042604

2605-
trait HKLambda extends LambdaType
2605+
abstract class HKLambda extends CachedProxyType with LambdaType {
2606+
final override def underlying(implicit ctx: Context) = resType
2607+
2608+
final override def computeHash = doHash(paramNames, resType, paramInfos)
2609+
2610+
final override def equals(that: Any) = that match {
2611+
case that: HKLambda =>
2612+
paramNames == that.paramNames &&
2613+
paramInfos == that.paramInfos &&
2614+
resType == that.resType &&
2615+
companion.eq(that.companion)
2616+
case _ =>
2617+
false
2618+
}
2619+
2620+
final override def eql(that: Type) = that match {
2621+
case that: HKLambda =>
2622+
paramNames.equals(that.paramNames) &&
2623+
paramInfos.equals(that.paramInfos) &&
2624+
resType.equals(that.resType) &&
2625+
companion.eq(that.companion)
2626+
case _ =>
2627+
false
2628+
}
2629+
}
2630+
26062631
trait MethodOrPoly extends LambdaType with MethodicType
26072632

26082633
trait TermLambda extends LambdaType { thisLambdaType =>
@@ -2893,7 +2918,7 @@ object Types {
28932918
*/
28942919
class HKTypeLambda(val paramNames: List[TypeName])(
28952920
paramInfosExp: HKTypeLambda => List[TypeBounds], resultTypeExp: HKTypeLambda => Type)
2896-
extends UncachedProxyType with HKLambda with TypeLambda {
2921+
extends HKLambda with TypeLambda {
28972922
type This = HKTypeLambda
28982923
def companion = HKTypeLambda
28992924

@@ -2903,8 +2928,6 @@ object Types {
29032928
assert(resType.isInstanceOf[TermType], this)
29042929
assert(paramNames.nonEmpty)
29052930

2906-
final override def underlying(implicit ctx: Context) = resType
2907-
29082931
protected def prefixString = "HKTypeLambda"
29092932
}
29102933

compiler/src/dotty/tools/dotc/typer/Checking.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import ErrorReporting.errorTree
2121

2222
import annotation.unchecked
2323
import util.Positions._
24-
import util.{SimpleMap, Stats}
24+
import util.Stats
2525
import util.common._
2626
import transform.SymUtils._
2727
import Decorators._

compiler/src/dotty/tools/dotc/typer/ImportInfo.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ast.{tpd, untpd}
66
import ast.Trees._
77
import core._
88
import printing.{Printer, Showable}
9-
import util.SimpleMap
9+
import util.SimpleIdentityMap
1010
import Symbols._, Names._, Denotations._, Types._, Contexts._, StdNames._, Flags._
1111
import Decorators.StringInterpolators
1212

@@ -52,7 +52,7 @@ class ImportInfo(symf: Context => Symbol, val selectors: List[untpd.Tree],
5252
def excluded: Set[TermName] = { ensureInitialized(); myExcluded }
5353

5454
/** A mapping from renamed to original names */
55-
def reverseMapping: SimpleMap[TermName, TermName] = { ensureInitialized(); myMapped }
55+
def reverseMapping: SimpleIdentityMap[TermName, TermName] = { ensureInitialized(); myMapped }
5656

5757
/** The original names imported by-name before renaming */
5858
def originals: Set[TermName] = { ensureInitialized(); myOriginals }
@@ -61,14 +61,14 @@ class ImportInfo(symf: Context => Symbol, val selectors: List[untpd.Tree],
6161
def isWildcardImport = { ensureInitialized(); myWildcardImport }
6262

6363
private var myExcluded: Set[TermName] = null
64-
private var myMapped: SimpleMap[TermName, TermName] = null
64+
private var myMapped: SimpleIdentityMap[TermName, TermName] = null
6565
private var myOriginals: Set[TermName] = null
6666
private var myWildcardImport: Boolean = false
6767

6868
/** Compute info relating to the selector list */
6969
private def ensureInitialized(): Unit = if (myExcluded == null) {
7070
myExcluded = Set()
71-
myMapped = SimpleMap.Empty
71+
myMapped = SimpleIdentityMap.Empty
7272
myOriginals = Set()
7373
def recur(sels: List[untpd.Tree]): Unit = sels match {
7474
case sel :: sels1 =>

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Scopes._
1111
import ProtoTypes._
1212
import annotation.unchecked
1313
import util.Positions._
14-
import util.{Stats, SimpleMap}
14+
import util.{Stats, SimpleIdentityMap}
1515
import util.common._
1616
import Decorators._
1717
import Uniques._
@@ -286,7 +286,7 @@ object Inferencing {
286286
result
287287
}
288288

289-
type VarianceMap = SimpleMap[TypeVar, Integer]
289+
type VarianceMap = SimpleIdentityMap[TypeVar, Integer]
290290

291291
/** All occurrences of type vars in this type that satisfy predicate
292292
* `include` mapped to their variances (-1/0/1) in this type, where
@@ -350,7 +350,7 @@ object Inferencing {
350350
if (vmap1 eq vmap) vmap else propagate(vmap1)
351351
}
352352

353-
propagate(accu(SimpleMap.Empty, tp))
353+
propagate(accu(SimpleIdentityMap.Empty, tp))
354354
}
355355
}
356356

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Constants._
1111
import Scopes._
1212
import annotation.unchecked
1313
import util.Positions._
14-
import util.{Stats, SimpleMap}
14+
import util.{Stats, SimpleIdentityMap}
1515
import util.common._
1616
import Decorators._
1717
import Uniques._
@@ -179,10 +179,10 @@ object ProtoTypes {
179179
override def resultType(implicit ctx: Context) = resType
180180

181181
/** A map in which typed arguments can be stored to be later integrated in `typedArgs`. */
182-
private var myTypedArg: SimpleMap[untpd.Tree, Tree] = SimpleMap.Empty
182+
private var myTypedArg: SimpleIdentityMap[untpd.Tree, Tree] = SimpleIdentityMap.Empty
183183

184184
/** A map recording the typer states in which arguments stored in myTypedArg were typed */
185-
private var evalState: SimpleMap[untpd.Tree, TyperState] = SimpleMap.Empty
185+
private var evalState: SimpleIdentityMap[untpd.Tree, TyperState] = SimpleIdentityMap.Empty
186186

187187
def isMatchedBy(tp: Type)(implicit ctx: Context) =
188188
typer.isApplicable(tp, Nil, typedArgs, resultType)

0 commit comments

Comments
 (0)