Skip to content

Commit 41944a6

Browse files
committed
Faster bottomType and phantomness checks.
1 parent e5f50ec commit 41944a6

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
565565
// note that the only phantom classes are PhantomAnyClass and PhantomNothingClass
566566
val sym = tp.typeSymbol
567567
(sym eq PhantomAnyClass) || (sym eq PhantomNothingClass) ||
568-
(!sym.isClass && (tp.topType.classSymbol eq PhantomAnyClass))
568+
(!sym.isClass && tp.phantomLatticeType.exists)
569569
}
570570
val sym1 = tp1.symbol
571571
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda && !isPhantom(tp2) ||

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,35 @@ object Types {
175175
loop(this)
176176
}
177177

178-
/** Is phantom if upper bounded by XYZ.Any where XYZ extends scala.Phantom */
179-
final def isPhantom(implicit ctx: Context): Boolean = {
180-
// note that the only phantom classes are PhantomAnyClass and PhantomNothingClass
181-
val sym = typeSymbol
182-
(sym eq defn.PhantomAny) || (sym eq defn.PhantomNothing) ||
183-
(!sym.isClass && (topType.classSymbol eq defn.PhantomAny))
184-
}
185-
186178
/** Returns the top type of the lattice
187179
* - XYX.Any if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
188180
* - scala.Any otherwise
189181
*/
190-
final def topType(implicit ctx: Context): TypeRef = widen match {
191-
case tp: ClassInfo if isPhantomClass(tp.classSymbol) => tp.prefix.select(tpnme.Any).asInstanceOf[TypeRef]
192-
case tp: TypeProxy if tp.superType ne this => tp.superType.topType
193-
case tp: AndOrType => tp.tp1.topType
194-
case _ => defn.AnyType
182+
final def topType(implicit ctx: Context): TypeRef = {
183+
val lattice = phantomLatticeType
184+
if (lattice.exists) lattice.select(tpnme.Any).asInstanceOf[TypeRef]
185+
else defn.AnyType
195186
}
196187

197188
/** Returns the bottom type of the lattice
198189
* - XYZ.Nothing if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
199190
* - scala.Nothing otherwise
200191
*/
201-
final def bottomType(implicit ctx: Context): Type = topType match {
202-
case top: TypeRef if top.prefix.termSymbol ne defn.ScalaPackageVal =>
203-
top.prefix.select(tpnme.Nothing)
204-
case _ => defn.NothingType
192+
final def bottomType(implicit ctx: Context): Type = {
193+
val lattice = phantomLatticeType
194+
if (lattice.exists) lattice.select(tpnme.Nothing).asInstanceOf[TypeRef]
195+
else defn.NothingType
196+
}
197+
198+
/** Returns the type of the lattice
199+
* - XYZ if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
200+
* - NoType otherwise
201+
*/
202+
final def phantomLatticeType(implicit ctx: Context): Type = widen match {
203+
case tp: ClassInfo if isPhantomClass(tp.classSymbol) => tp.prefix
204+
case tp: TypeProxy if tp.superType ne this => tp.superType.phantomLatticeType
205+
case tp: AndOrType => tp.tp1.phantomLatticeType
206+
case _ => NoType
205207
}
206208

207209
/** If the symbol is of the class scala.Phantom.Any or scala.Phantom.Nothing */
@@ -3279,8 +3281,8 @@ object Types {
32793281
/** Type bounds >: lo <: hi */
32803282
abstract case class TypeBounds(lo: Type, hi: Type) extends CachedProxyType with TypeType {
32813283

3282-
assert(lo.isInstanceOf[TermType], lo)
3283-
assert(hi.isInstanceOf[TermType], hi)
3284+
assert(lo.isInstanceOf[TermType])
3285+
assert(hi.isInstanceOf[TermType])
32843286

32853287
def variance: Int = 0
32863288

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ trait TypeAssigner {
431431
tree.withType(body.tpe)
432432

433433
def assignType(tree: untpd.Match, cases: List[CaseDef])(implicit ctx: Context) = {
434-
if (tree.selector.typeOpt.isPhantom)
434+
if (tree.selector.typeOpt.phantomLatticeType.exists)
435435
ctx.error("Cannot pattern match on phantoms", tree.selector.pos)
436436
if (cases.nonEmpty) {
437437
val head = cases.head

0 commit comments

Comments
 (0)