Skip to content

Commit fa88828

Browse files
committed
Optimize stripTypeVar, stripAnnots combinations without creating abstract class
1 parent 18dc149 commit fa88828

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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

Lines changed: 14 additions & 9 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,15 @@ 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 match
1077+
case tp: TypeVar => tp.stripTypeVar.stripped
1078+
case tp: AnnotatedType => tp.parent.stripped
1079+
case _ => this
1080+
10761081
def rewrapAnnots(tp: Type)(using Context): Type = tp.stripTypeVar match {
10771082
case AnnotatedType(tp1, annot) => AnnotatedType(rewrapAnnots(tp1), annot)
10781083
case _ => this
@@ -1105,7 +1110,7 @@ object Types {
11051110
case tp: SingletonType => tp.underlying.widen
11061111
case tp: ExprType => tp.resultType.widen
11071112
case tp =>
1108-
val tp1 = tp.stripTypeVar.stripAnnots
1113+
val tp1 = tp.stripped
11091114
if tp1 eq tp then tp
11101115
else
11111116
val tp2 = tp1.widen
@@ -1114,7 +1119,7 @@ object Types {
11141119
/** Widen from singleton type to its underlying non-singleton
11151120
* base type by applying one or more `underlying` dereferences.
11161121
*/
1117-
final def widenSingleton(using Context): Type = stripTypeVar.stripAnnots match {
1122+
final def widenSingleton(using Context): Type = stripped match {
11181123
case tp: SingletonType if !tp.isOverloaded => tp.underlying.widenSingleton
11191124
case _ => this
11201125
}

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)