Skip to content

Commit ade3ae3

Browse files
committed
Optimize stripTypeVar, stripAnnots combinations
1 parent 18dc149 commit ade3ae3

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,11 @@ object Types {
163163
* Like in isStableMember, "stability" means idempotence.
164164
* Rationale: If an expression has a stable type, the expression must be idempotent, so stable types
165165
* must be singleton types of stable expressions. */
166-
final def isStable(using Context): Boolean = stripTypeVar match {
166+
final def isStable(using Context): Boolean = stripped match {
167167
case tp: TermRef => tp.symbol.isStableMember && tp.prefix.isStable || tp.info.isStable
168168
case _: SingletonType | NoPrefix => true
169169
case tp: RefinedOrRecType => tp.parent.isStable
170170
case tp: ExprType => tp.resultType.isStable
171-
case tp: AnnotatedType => tp.parent.isStable
172171
case tp: AndType =>
173172
tp.tp1.isStable && (realizability(tp.tp2) eq Realizable) ||
174173
tp.tp2.isStable && (realizability(tp.tp1) eq Realizable)
@@ -181,7 +180,7 @@ object Types {
181180
* It makes no sense for it to be an alias type because isRef would always
182181
* return false in that case.
183182
*/
184-
def isRef(sym: Symbol, skipRefined: Boolean = true)(using Context): Boolean = stripAnnots.stripTypeVar match {
183+
def isRef(sym: Symbol, skipRefined: Boolean = true)(using Context): Boolean = stripped match {
185184
case this1: TypeRef =>
186185
this1.info match { // see comment in Namer#typeDefSig
187186
case TypeAlias(tp) => tp.isRef(sym, skipRefined)
@@ -196,7 +195,7 @@ object Types {
196195
case _ => false
197196
}
198197

199-
/** Is this type a (neither aliased nor applied) reference to class `sym`? */
198+
/** Is this type a (neither aliased nor applied nor annotated) reference to class `sym`? */
200199
def isDirectRef(sym: Symbol)(using Context): Boolean = stripTypeVar match {
201200
case this1: TypeRef =>
202201
this1.name == sym.name && // avoid forcing info if names differ
@@ -214,7 +213,7 @@ object Types {
214213
/** Does this type refer exactly to class symbol `sym`, instead of to a subclass of `sym`?
215214
* Implemented like `isRef`, but follows more types: all type proxies as well as and- and or-types
216215
*/
217-
private[Types] def isTightPrefix(sym: Symbol)(using Context): Boolean = stripTypeVar match {
216+
private[Types] def isTightPrefix(sym: Symbol)(using Context): Boolean = stripped match {
218217
case tp: NamedType => tp.info.isTightPrefix(sym)
219218
case tp: ClassInfo => tp.cls eq sym
220219
case tp: Types.ThisType => tp.cls eq sym
@@ -369,7 +368,7 @@ object Types {
369368

370369
/** Is this a match type or a higher-kinded abstraction of one?
371370
*/
372-
def isMatch(using Context): Boolean = stripTypeVar.stripAnnots match {
371+
def isMatch(using Context): Boolean = stripped match {
373372
case _: MatchType => true
374373
case tp: HKTypeLambda => tp.resType.isMatch
375374
case _ => false
@@ -1070,9 +1069,12 @@ object Types {
10701069
def stripTypeVar(using Context): Type = this
10711070

10721071
/** Remove all AnnotatedTypes wrapping this type.
1073-
*/
1072+
*/
10741073
def stripAnnots(using Context): Type = this
10751074

1075+
/** Strip TypeVars and Annotation wrappers */
1076+
def stripped(using Context): Type = this
1077+
10761078
def rewrapAnnots(tp: Type)(using Context): Type = tp.stripTypeVar match {
10771079
case AnnotatedType(tp1, annot) => AnnotatedType(rewrapAnnots(tp1), annot)
10781080
case _ => this
@@ -1105,7 +1107,7 @@ object Types {
11051107
case tp: SingletonType => tp.underlying.widen
11061108
case tp: ExprType => tp.resultType.widen
11071109
case tp =>
1108-
val tp1 = tp.stripTypeVar.stripAnnots
1110+
val tp1 = tp.stripped
11091111
if tp1 eq tp then tp
11101112
else
11111113
val tp2 = tp1.widen
@@ -1114,7 +1116,7 @@ object Types {
11141116
/** Widen from singleton type to its underlying non-singleton
11151117
* base type by applying one or more `underlying` dereferences.
11161118
*/
1117-
final def widenSingleton(using Context): Type = stripTypeVar.stripAnnots match {
1119+
final def widenSingleton(using Context): Type = stripped match {
11181120
case tp: SingletonType if !tp.isOverloaded => tp.underlying.widenSingleton
11191121
case _ => this
11201122
}
@@ -1805,6 +1807,9 @@ object Types {
18051807
if (hash == NotCached) System.identityHashCode(this) else hash
18061808
}
18071809

1810+
abstract class StrippableProxyType extends CachedProxyType:
1811+
override def stripped(using Context): Type = stripAnnots.stripTypeVar.stripped
1812+
18081813
/** Instances of this class are uncached and are not proxies. */
18091814
abstract class UncachedGroundType extends Type {
18101815
final def hash: Int = NotCached
@@ -4198,7 +4203,7 @@ object Types {
41984203
* @param origin The parameter that's tracked by the type variable.
41994204
* @param creatorState The typer state in which the variable was created.
42004205
*/
4201-
final class TypeVar private(initOrigin: TypeParamRef, creatorState: TyperState, nestingLevel: Int) extends CachedProxyType with ValueType {
4206+
final class TypeVar private(initOrigin: TypeParamRef, creatorState: TyperState, nestingLevel: Int) extends StrippableProxyType with ValueType {
42024207

42034208
private var currentOrigin = initOrigin
42044209

@@ -4692,7 +4697,7 @@ object Types {
46924697
// ----- Annotated and Import types -----------------------------------------------
46934698

46944699
/** An annotated type tpe @ annot */
4695-
abstract case class AnnotatedType(parent: Type, annot: Annotation) extends CachedProxyType with ValueType {
4700+
abstract case class AnnotatedType(parent: Type, annot: Annotation) extends StrippableProxyType with ValueType {
46964701

46974702
override def underlying(using Context): Type = parent
46984703

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
691691
case _ => tp.show
692692
}
693693

694-
def refine(tp: Type): String = tp.stripAnnots.stripTypeVar match {
694+
def refine(tp: Type): String = tp.stripped match {
695695
case tp: RefinedType => refine(tp.parent)
696696
case tp: AppliedType =>
697697
refine(tp.typeConstructor) + (

0 commit comments

Comments
 (0)